Understanding Cosmos DB Consistency Levels
You'll learn: How Azure Cosmos DB offers five consistency models that let you trade off between data freshness, performance, and availability.
The Core Idea
When you distribute data across multiple regions (as Cosmos DB does), there's a fundamental tension: do you wait for all copies to update before confirming a write (slow but consistent), or do you confirm immediately and let copies sync later (fast but potentially stale)? Cosmos DB gives you five consistency levels to choose the right balance for your application.
Think of it like news distribution: A "strong" consistency is like waiting for every newspaper to print today's story before releasing it—everyone sees the same thing, but it's slower. "Eventual" consistency is like posting online immediately—some readers might see yesterday's version briefly, but everyone catches up eventually.
The Five Levels (Strongest to Weakest)
- Strong: Reads always return the most recent committed write. Highest latency, guaranteed freshness.
- Bounded Staleness: Reads might lag by a defined amount (time or number of versions). Predictable staleness.
- Session: Within a single user session, reads reflect that session's writes. Great for user-facing apps.
- Consistent Prefix: Reads never see out-of-order writes, but might be behind. You see A, then B, then C—never C, then A.
- Eventual: Lowest latency, highest availability. Replicas eventually converge, but reads might return older data temporarily.
The Trade-off
Stronger consistency = higher read latency + lower availability (if a region fails, writes may block). Weaker consistency = faster reads + better availability, but you might read stale data.
Key Takeaway:
Cosmos DB's five consistency levels let you explicitly choose how fresh your data must be versus how fast and available your application needs to be—a critical design decision for globally distributed databases.