Set the appropriate consistency level for Cosmos DB operations
5 minutes
5 Questions
Azure Cosmos DB offers five consistency levels that provide a spectrum between strong consistency and eventual consistency, allowing developers to make precise tradeoffs between read consistency, availability, latency, and throughput.
**Strong Consistency**: Guarantees linearizability, meaning rea…Azure Cosmos DB offers five consistency levels that provide a spectrum between strong consistency and eventual consistency, allowing developers to make precise tradeoffs between read consistency, availability, latency, and throughput.
**Strong Consistency**: Guarantees linearizability, meaning reads always return the most recent committed version of an item. This level offers the highest consistency but may impact latency and availability across regions.
**Bounded Staleness**: Guarantees that reads lag behind writes by at most K versions or T time interval. This is ideal for applications requiring strong consistency with some tolerance for staleness, particularly in multi-region scenarios.
**Session Consistency**: The default and most widely used level. It guarantees read-your-writes, monotonic reads, and monotonic writes within a single client session. Perfect for user-centric applications where each user needs to see their own updates.
**Consistent Prefix**: Ensures reads never see out-of-order writes. If writes occur in order A, B, C, reads will see A, then A-B, then A-B-C, never seeing B before A.
**Eventual Consistency**: Provides the weakest consistency but offers the lowest latency and highest availability. Reads may return stale data, but eventually all replicas converge.
**Setting Consistency Level**:
You can configure the default consistency at the account level through Azure Portal or programmatically. For individual operations, you can override using RequestOptions:
csharp
ItemRequestOptions options = new ItemRequestOptions
{
ConsistencyLevel = ConsistencyLevel.Strong
};
await container.ReadItemAsync<MyItem>(id, partitionKey, options);
**Best Practices**:
- Use Session consistency for most scenarios as it balances performance and consistency
- Choose Strong when financial transactions require absolute accuracy
- Select Eventual for high-throughput scenarios where temporary inconsistency is acceptable
- Consider Bounded Staleness for global applications needing predictable consistency guarantees
Remember that relaxing consistency improves performance and availability while reducing RU consumption.
Cosmos DB Consistency Levels: Complete Guide for AZ-204
Why Consistency Levels Matter
In distributed database systems like Azure Cosmos DB, data is replicated across multiple regions for high availability and low latency. Consistency levels determine how and when data changes become visible across these replicas. Choosing the right consistency level is crucial because it directly impacts your application's performance, availability, and data accuracy.
What Are Cosmos DB Consistency Levels?
Azure Cosmos DB offers five consistency levels, ordered from strongest to weakest:
1. Strong - Guarantees linearizability (reads return the most recent committed version) - Highest latency, lowest throughput - Best for applications requiring absolute data accuracy
2. Bounded Staleness - Reads may lag behind writes by a configured number of operations (K) or time interval (T) - Guarantees consistent prefix - Good for applications tolerating limited staleness with ordering guarantees
3. Session - Default consistency level - Guarantees read-your-writes within a single session - Most popular choice balancing consistency and performance
4. Consistent Prefix - Guarantees reads never see out-of-order writes - Updates appear in order but may be stale - Good for applications where order matters more than immediacy
5. Eventual - Weakest consistency, highest performance - No ordering guarantee - Best for scenarios where consistency can be sacrificed for speed
How Consistency Levels Work
When you configure a consistency level: - It is set at the account level as a default - Can be relaxed (weakened) per request, but never strengthened - Affects read operations; writes are always committed to a quorum
Code example to set consistency per request:
ItemRequestOptions options = new ItemRequestOptions { ConsistencyLevel = ConsistencyLevel.Eventual };
How to Answer Exam Questions
When facing consistency level questions, follow this decision framework:
1. Financial/Banking/Inventory scenarios → Strong or Bounded Staleness 2. User session data, shopping carts → Session consistency 3. Social media feeds, activity logs → Consistent Prefix or Eventual 4. Maximum performance needed → Eventual 5. Default or general purpose → Session
Exam Tips: Answering Questions on Setting the Appropriate Consistency Level for Cosmos DB Operations
• Remember the order: Strong → Bounded Staleness → Session → Consistent Prefix → Eventual (strongest to weakest)
• Session is the default: If a question mentions 'default' or doesn't specify requirements, Session is likely the answer
• You can only weaken consistency per request: If your account is set to Session, you can request Eventual but not Strong
• Bounded Staleness has two parameters: K (number of versions) and T (time interval) - know both exist
• Strong consistency is NOT available in multi-region write scenarios: This is a common exam trap
• Look for keywords: 'read your own writes' = Session, 'order guaranteed' = Consistent Prefix, 'latest data' = Strong
• Cost and latency increase with stronger consistency: If the question emphasizes cost optimization, lean toward weaker levels
• Consistent Prefix guarantees order but allows staleness: Perfect for scenarios like displaying a social feed in correct sequence
• Multi-region deployments: Bounded Staleness behaves like Strong within a region but allows staleness across regions