Consistency Models in Azure Cosmos DB

Azure Cosmos DB offers a comprehensive set of well-defined consistency levels that allow you to choose the trade-off between consistency, availability, and latency for your application.

Understanding these levels is crucial for designing robust and performant distributed applications. Cosmos DB guarantees five distinct consistency levels, ranging from the strongest (Strong) to the weakest (Eventual).

Important: The consistency level impacts both the read operations and the application's ability to write data. Choosing the right level is a critical design decision.

Available Consistency Levels

The following table lists the five consistency levels offered by Azure Cosmos DB, ordered from strongest to weakest:

1. Strong

Guarantee: All reads are guaranteed to return the most up-to-date data. Writes are globally consistent.

  • Pros: Highest data consistency. Simplest programming model.
  • Cons: Highest latency for reads and writes. Lower availability during network partitions.
  • Use Cases: Applications requiring absolute data freshness, e.g., financial transactions, critical inventory management.

2. Bounded Staleness

Guarantee: Reads are guaranteed to be no older than a specified number of versions (k) or time interval (t).

  • Pros: Good balance between consistency and performance.
  • Cons: Latency is higher than Session consistency.
  • Use Cases: Applications where data staleness up to a certain bound is acceptable, e.g., social media feeds, collaborative editing with a slight delay.

3. Session

Guarantee: Within a single client session, all reads are guaranteed to be consistent. Reads from other sessions might be stale.

  • Pros: Excellent balance of performance and consistency for typical applications. Low latency.
  • Cons: Data can be stale across different client sessions.
  • Use Cases: Most general-purpose applications where clients operate within their own sessions, e.g., user profile management, shopping carts.

4. Consistent Prefix

Guarantee: Reads will return a prefix of the writes, meaning that if a write has occurred, all subsequent reads will see that write and all preceding writes. However, reads may not be up-to-date.

  • Pros: Guarantees no "missing" writes, just potential staleness.
  • Cons: Can still see stale data. Less common than Session consistency.
  • Use Cases: Applications that need to ensure that no writes are ever skipped by subsequent reads.

5. Eventual

Guarantee: Reads may be stale. There is no guarantee on the order or freshness of data.

  • Pros: Highest availability and lowest latency.
  • Cons: Data can be significantly stale. Programming model needs to account for potential inconsistencies.
  • Use Cases: Applications where eventual consistency is acceptable, e.g., reporting, analytics, non-critical data replication.

Choosing the Right Consistency Level

The choice of consistency level depends on your application's specific requirements:

Tip: Most applications benefit from the Session consistency level. Consider stronger levels only when absolutely necessary, as they come with performance trade-offs.

How Consistency is Implemented

Azure Cosmos DB achieves high availability and low latency by replicating data across multiple Azure regions. The consistency model dictates how these replicas are synchronized. For example, with Strong consistency, every write operation must be acknowledged by a quorum of replicas before it is considered successful.

You can configure the desired consistency level at the account level. This setting applies to all database operations performed against your Cosmos DB account.

For more in-depth information on consistency guarantees, trade-offs, and implementation details, please refer to the official consistency guarantees documentation.

Example using Azure SDK (Node.js):

const cosmosClient = new CosmosClient({ endpoint, key, consistencyLevel: ConsistencyLevel.Session });