Cosmos DB Consistency Models

Cosmos DB Consistency Models

Watch the video to deepen your understanding.

Subscribe

Complete the full lesson to earn 25 points

Work through each section, then tap “Mark as Complete” on the last one.

Section 1 of 3

✦ Skip the page breaks and see fewer ads — read each lesson on a single page with Pro

Lesson: Azure Cosmos DB Consistency Models

Introduction: The CAP Theorem in Practice

In distributed database systems, we often encounter the CAP Theorem, which states that a system can only simultaneously provide two of three guarantees: Consistency, Availability, and Partition Tolerance. Because cloud databases like Azure Cosmos DB must be partition-tolerant to scale globally, developers are left with a trade-off: Consistency vs. Availability.

Cosmos DB offers a unique "spectrum" of five well-defined consistency models. Unlike traditional databases that offer only "Strong" or "Eventual" consistency, Cosmos DB allows you to choose the exact balance that fits your application’s specific requirements. Understanding these models is critical for optimizing performance, latency, and data integrity.


The Five Consistency Models

1. Strong Consistency

Strong consistency guarantees that a read will always return the most recent committed version of an item. A client will never see an uncommitted or partial write.

  • Use Case: Financial transactions, inventory management where stock levels must be exact.
  • Trade-off: Higher latency and lower availability if the global replication process is interrupted.

2. Bounded Staleness

Data reads may lag behind writes by a user-specified "window" (either by time or by the number of operations).

  • Use Case: Applications where a slight delay is acceptable, but the order of operations must be preserved (e.g., social media feeds or sensor data streams).

3. Session Consistency (Default)

This is the most popular model. It provides "read-your-own-writes" guarantees within a single client session. Other sessions might see slightly older data, but the current user always sees their own latest updates.

  • Use Case: User profiles, shopping carts, or any application where the user expects their immediate actions to be reflected.

4. Consistent Prefix

Updates are returned in the order in which they were committed. You might not see the latest update, but you will never see an out-of-order update.

  • Use Case: Activity logs or messaging apps where the sequence of events is more important than real-time absolute freshness.

5. Eventual Consistency

There is no ordering guarantee. Eventually, all replicas will converge, but reads may return stale data or data out of sequence.

  • Use Case: Counting likes on a post, analytics dashboards, or non-critical logging.
  • Trade-off: Highest performance and lowest latency.

Section 1 of 3
PrevNext