Cosmos DB consistency levels aren’t just about choosing a dial; they’re about deciding how much you trust your data’s eventual arrival.

Let’s see this in action. Imagine a simple blog application. When a user posts a comment, we want to display it immediately to them, but we don’t necessarily need it to be instantly visible to everyone else around the world.

Here’s a snippet of how you might configure your Cosmos DB client for a specific consistency level, in this case, Session consistency, using the .NET SDK:

using Microsoft.Azure.Cosmos;

public class CosmosClientFactory
{
    public CosmosClient CreateClient(string endpointUri, string primaryKey)
    {
        CosmosClientOptions options = new CosmosClientOptions
        {
            ConsistencyLevel = ConsistencyLevel.Session
        };
        return new CosmosClient(endpointUri, primaryKey, options);
    }
}

Now, let’s break down what’s happening under the hood. Cosmos DB offers five distinct consistency levels: Strong, Bounded Staleness, Session, Consistent Prefix, and Eventual. Each level represents a trade-off between consistency (all reads see the most recent write) and availability/performance (lower latency, higher throughput).

  • Strong Consistency: Every read operation is guaranteed to return the most up-to-date data. This is like having a single, authoritative ledger for everything. However, it incurs the highest latency because the request must be acknowledged by a quorum of replicas across different regions before it can return. This is often overkill for many web applications.

  • Bounded Staleness: Reads are guaranteed to be no more than a certain number of versions (e.g., up to 5 versions) or a certain time window (e.g., up to 30 seconds) behind the latest write. This offers a good balance for scenarios where you need relatively fresh data but can tolerate minor lag. The staleness bound is configured per account.

  • Session Consistency: This is the default and often the sweet spot. Within a single client session (defined by the CosmosClient instance), all reads are guaranteed to be consistent. Once a write occurs, subsequent reads within that same session will see that write. However, reads from different client sessions might see a slightly older version of the data for a short period. This is ideal for most user-facing applications where immediate consistency for a single user’s interaction is key.

  • Consistent Prefix: Reads are guaranteed to return a prefix of the data, meaning that if a write has occurred, you’ll never see writes that happened after it without also seeing the write itself. You won’t see writes that happened before it. This is a weaker guarantee than Session but stronger than Eventual.

  • Eventual Consistency: Reads might return stale data. There’s no guarantee on how long it will take for a write to propagate to all replicas. This offers the highest availability and lowest latency, suitable for scenarios where seeing slightly older data is acceptable (e.g., analytics, non-critical counters).

The choice hinges on your application’s read patterns and tolerance for stale data. If your application requires every single user to see the absolute latest data at all times, regardless of geographical location or latency implications, you’d opt for Strong consistency. For a typical web application where a user updates their profile, and you want them to see their own update immediately but don’t mind if another user sees the old version for a few seconds, Session consistency is perfect.

Here’s how the staleness bound is configured on your Cosmos DB account itself. You’d typically do this via the Azure portal or ARM templates. For example, to set a maximum staleness of 30 seconds:

{
    "name": "myCosmosAccount",
    "properties": {
        "consistencyPolicy": {
            "defaultConsistencyLevel": "BoundedStaleness",
            "maxStalenessPrefix": 10000, // This value is in operations, not seconds.
            "maxIntervalInSeconds": 30 // This is the value for time-based staleness
        }
        // ... other properties
    }
}

The maxStalenessPrefix is a count of operations, while maxIntervalInSeconds is a time duration. When either threshold is met, a read is guaranteed to be up-to-date.

The one aspect that often trips people up is how Session consistency works across distributed clients. If you have multiple instances of your application server, each creating its own CosmosClient instance, they each establish their own session. A write from Client A will be visible to Client A immediately. However, Client B might not see that write until its internal session state is updated, which happens automatically but not instantaneously. To ensure a write is seen across all clients immediately, you’d need to either use Strong consistency or explicitly refresh the session token for other clients.

Understanding these trade-offs allows you to tune Cosmos DB for optimal performance and user experience, rather than defaulting to the most restrictive option.

The next logical step is to explore how to manage and optimize queries across these different consistency models.

Want structured learning?

Take the full Azure course →