Cosmos DB Consistency Levels
Azure Cosmos DB offers a comprehensive set of five distinct consistency levels. These levels provide developers with the flexibility to choose the trade-off between consistency, availability, and latency that best suits their application's specific needs.
Understanding the Trade-offs
The choice of consistency level impacts how read operations behave with respect to writes. Generally, stronger consistency levels offer higher guarantees but may introduce higher latency, while weaker consistency levels prioritize lower latency and higher availability at the cost of potentially stale reads.
The Five Consistency Levels
Level | Description | Read Region | Latency | Availability | Strongest |
---|---|---|---|---|---|
Strong | All reads are guaranteed to return the most up-to-date data. The system waits for all replicas to acknowledge a write before responding. | Any | Highest | Lowest | ●●●●● |
Bounded Staleness | Reads are guaranteed to be no older than a specified version or time interval behind the primary. | Any | Low | High | ●●●●■ |
Session | Within a single client session, all reads are consistent. Cross-session reads might see slightly older data. | Any | Lower | Higher | ●●●■■ |
Consistent Prefix | Reads will always return a prefix of writes. If a read returns data written at version 'X', then any subsequent read will return data written at version 'Y' where Y >= X. | Any | Lowest | Highest | ●●■■■ |
Eventual | Reads may return stale data. There's no guarantee on how soon writes will propagate to all replicas. | Any | Lowest | Highest | ●■■■■ |
Choosing the Right Consistency Level
The default consistency level for Cosmos DB is Session consistency, which offers a good balance for most applications. However, you might consider other levels based on your application's requirements:
- Strong Consistency: Use when every read must be up-to-date, such as financial transactions or critical inventory management.
- Bounded Staleness: Ideal for scenarios where a slight delay in data visibility is acceptable, but you want to limit how stale the data can become.
- Session Consistency: A good default for most web and mobile applications where users expect to see their own writes immediately within their session.
- Consistent Prefix: Suitable for applications that need to ensure that if they see a piece of data, they've seen all preceding data for that item.
- Eventual Consistency: Best for applications where the absolute latest data isn't critical and high availability and low latency are paramount, such as social media feeds or IoT data ingestion.
Configuring Consistency Levels
You can configure the consistency level for your Cosmos DB account through the Azure portal or programmatically using the Cosmos DB SDKs. The chosen consistency level applies to all requests made to your Cosmos DB account.
For example, using the .NET SDK:
using Microsoft.Azure.Cosmos;
// ...
// Initialize the CosmosClient with a specific consistency level
var cosmosClientOptions = new CosmosClientOptions
{
ConsistencyLevel = ConsistencyLevel.Session // Or Strong, BoundedStaleness, ConsistentPrefix, Eventual
};
// If using BoundedStaleness, you can configure it further:
// cosmosClientOptions.ConsistencyLevel = ConsistencyLevel.BoundedStaleness;
// cosmosClientOptions.MaxStalenessPrefix = 1000; // Max stale events or time in milliseconds
using (var client = new CosmosClient("YOUR_COSMOS_DB_CONNECTION_STRING", cosmosClientOptions))
{
// Use the client...
}