MSDN Documentation

Azure Cosmos DB

Understanding Cosmos DB Consistency Levels

Azure Cosmos DB offers five well-defined consistency levels, providing developers with the flexibility to choose the right balance between consistency, availability, and latency for their applications. This article delves into each consistency level, explaining their characteristics, trade-offs, and when to use them.

The Five Consistency Levels

Strong

Guarantees: All reads receive the most up-to-date data, reflecting all prior writes.

Trade-offs: Highest latency, lowest availability (especially in geo-replicated scenarios).

Bounded Staleness

Guarantees: Reads are guaranteed to be no more than k updates or t time interval behind the primary region's writes.

Trade-offs: Good balance between consistency and performance.

Session

Guarantees: Within a single client session, reads are consistent. Reads may lag behind writes in other sessions or regions.

Trade-offs: Default for Cosmos DB. Offers a good balance for many applications.

Consistent Prefix

Guarantees: Reads are guaranteed to return a prefix of all writes. A read will never return a later write without also returning all prior writes.

Trade-offs: Less strict than Session, but still provides ordered results.

Eventual

Guarantees: Reads may return stale data. There's no guarantee of read order or lag time.

Trade-offs: Lowest latency, highest availability. Suitable for scenarios where data staleness is acceptable.

Choosing the Right Consistency Level

The selection of a consistency level is a critical design decision. Here's a guideline:

Impact on Latency and Throughput

Consistency levels have a direct impact on the performance characteristics of your Cosmos DB account:

Consistency Levels Trade-offs

Diagram showing trade-offs between consistency levels, latency, and availability

Diagram illustrating the relationship between consistency levels, read latency, and write latency.

Configuring Consistency Levels

You can configure the default consistency level for your Azure Cosmos DB account at the account level. This setting applies to all requests unless overridden by client SDKs for specific operations.

When using the Azure Cosmos DB SDKs, you can also specify the consistency level per operation or per client instance. For example, in .NET:


using Microsoft.Azure.Cosmos;

// Assuming 'client' is an initialized CosmosClient
CosmosClient client = new CosmosClient("YOUR_COSMOS_DB_CONNECTION_STRING");

// To set consistency for a specific operation:
ItemResponse<MyObject> response = await container.ReadItemAsync<MyObject>(
    "itemId",
    new PartitionKey("partitionKeyValue"),
    new ItemRequestOptions { ConsistencyLevel = ConsistencyLevel.Strong });

// To set consistency for the entire client instance (not recommended for fine-grained control):
CosmosClient clientWithSessionConsistency = new CosmosClient(
    "YOUR_COSMOS_DB_CONNECTION_STRING",
    new CosmosClientOptions { ConsistencyLevel = ConsistencyLevel.Session });
            

Conclusion

Understanding and correctly applying Cosmos DB's consistency levels is fundamental to building robust, scalable, and performant NoSQL applications. By carefully considering the needs of your application, you can make informed decisions that optimize for both data integrity and user experience.