Azure Cosmos DB Consistency Levels Explained: Strong, Bounded Staleness, Session, Consistent Prefix, and Eventual
The Banking App That Lost Money Three Times a Week
Cosmos DB offers five consistency levels β Strong, Bounded Staleness, Session, Consistent Prefix, Eventual β each with precise, non-obvious internal mechanics.
π― System Scenario & Requirements
System Goal
Cosmos DB offers five consistency levels β Strong, Bounded Staleness, Session, Consistent Prefix, Eventual β each with precise, non-obvious internal mechanics.
Actors & Components
- Application or API tier
- Core service under design
- Operator or SRE observer
Functional Requirements
- TLDR: Cosmos DB offers five consistency levels β Strong, Bounded Staleness, Session, Consistent Prefix, Eventual β each with precise, non-obvious internal mechanics.
- Session does not mean HTTP session; it means a client-side token that tracks what you have seen.
- Strong is unavailable with multi-region writes.
Constraints & Assumptions
- A single consistency level cannot serve every application in a globally distributed database.
- To understand why, start with a physical constraint: replication across regions takes time.
- Full Guarantee Comparison Failure Modes That Catch Teams Off-Guard Strong + multi-region write = hard platform rejection.
- Enabling multi-region writes and setting Strong consistency produces a configuration error.
- This section walks through the exact failure the banking app experienced step by step and shows the fix.
- Why this scenario matters: It represents the most common Cosmos DB production consistency bug.
β‘ Execution Stages & Decision Trace
Stage 1 β The Five Consistency Levels β What the Words Actually Mean in Cosmos DB
Cosmos DB exposes five consistency levels, ordered from strongest to weakest.
Before examining each mechanically, it is worth seeing their contracts side by side β the casual meanings of these words will mislead you if you rely on them.
Deep Dive
Scenario context
Cosmos DB offers five consistency levels β Strong, Bounded Staleness, Session, Consistent Prefix, Eventual β each with precise, non-obvious internal mechanics. Session does not mean HTTP session; it means a client-side token that tracks what you have seen. Strong is unavailable with multi-region writes. Eventual allows genuinely out-of-order reads. Picking the wrong level costs you either correctness or throughput ββ¦
This walkthrough focuses on The Banking App That Lost Money Three Times a Week.
Before making a design choice, clarify:
- A fintech team running a banking application on Azure Cosmos DB filed a production incident: users in Southeast Asia were occasionally seeing their pre-deposit balance immediately after depositing money.
- The account balance showed the old amount as if the deposit had never happened.
- Transactions were completing successfully.
Concepts in play: consistency-replication, database-indexing, graph-traversal-algorithms
Stage 1 β The Five Consistency Levels β What the Words Actually Mean in Cosmos DB
- Cosmos DB exposes five consistency levels, ordered from strongest to weakest.
- Before examining each mechanically, it is worth seeing their contracts side by side β the casual meanings of these words will mislead you if you rely on them.
- The ordering matters: every level to the right trades away consistency guarantees in exchange for lower write latency, higher availability, or both.
- The following spectrum diagram places each level against its key implications: This spectrum shows that Strong stands uniquely apart β it is the only level with a 2x RU cost on reads and the only level incompatible withβ¦
graph LR
S[ Strong Linearizable 2x RU cost No multi-write support]
BS[ Bounded Staleness Lag up to K versions or T secs Ordering in single-write region]
SE[ Session Read-your-own-writes 1x RU token-scoped]
CP[ Consistent Prefix No gaps or reversals 1x RU ordering only]
EV[ Eventual Max throughput 1x RU zero ordering]
S -->|"Weaker"| BS
BS --> SE
SE --> CP
CP -->|"Weakest"| EV
style S fill:#c0392b,color:#fff
style BS fill:#e67e22,color:#fff
style SE fill:#27ae60,color:#fff
style CP fill:#2980b9,color:#fff
style EV fill:#8e44ad,color:#fff
Observable outcome: The following spectrum diagram places each level against its key implications: This spectrum shows that Strong stands uniquely apart β it is the only level with a 2x RU cost on reads and the only level incompatible withβ¦
Stage 2 β How Cosmos DB Enforces Each Level β Quorums, Version Watermarks, and Token Routing
- Each consistency level is enforced through a different combination of three mechanisms: write quorum policy, read routing policy, and version tracking.
- Strong β Write quorum spans ALL configured regions.
- A write returns success only after every region has durably committed it.
- Reads must route to the primary write region or a validated quorum β no lagging replica may serve a Strong read.
Observable outcome: Reads must route to the primary write region or a validated quorum β no lagging replica may serve a Strong read.
Stage 3 β Deep Dive: Session Token Internals and Staleness Watermarks Under the Hood
- Internals: How Session Tokens and Version Watermarks Track Distributed State Session token mechanics operate entirely at the SDK layer.
- When you create a Cosmos DB client, it holds an empty partition-to-LSN map.
- After every write, the server response includes a session token β a string encoding the logical sequence number of the write on the relevant partition.
- The SDK stores this LSN per partition in memory.
sequenceDiagram
participant C as Client
participant WR as Write Region (SEA)
participant EU as Read Region (West EU)
participant US as Read Region (East US)
Note over C,US: Strong Consistency synchronous global quorum
C->>WR: Write W1 (deposit $500)
WR->>EU: Synchronous replication
WR->>US: Synchronous replication
EU-->>WR: ACK committed
US-->>WR: ACK committed
WR-->>C: Success + LSN:1042 (all regions confirmed)
Note over C,US: Session Consistency async replication + token routing
C->>WR: Write W2 (deposit $200)
WR-->>C: Success + session-token LSN:1043 (local quorum only)
C->>EU: Read balance [x-ms-session-token: LSN:1043]
EU-->>C: $1700 (routed to replica at LSN >= 1043)
Observable outcome: The SDK stores this LSN per partition in memory.
Stage 4 β Visualizing the Five Levels β How Read Visibility Differs Across the Spectrum
- Four diagrams follow β each shows the same write operation (depositing money) and read operation, but with different replication and routing behavior.
- Taken together, they illustrate exactly where each level's guarantee ends.
- Strong: Every Region Confirms Before the Client Receives Success Strong consistency blocks the client until all regions synchronously commit the write.
- The diagram below shows a three-region account β the client's success message arrives only after West Europe and East US have both confirmed.
sequenceDiagram
participant C as Client (SEA)
participant WR as Write Region (SEA)
participant EU as West Europe
participant US as East US
C->>WR: Write W1 (deposit $500)
WR->>EU: Synchronous replication of W1
WR->>US: Synchronous replication of W1
EU-->>WR: ACK W1 committed
US-->>WR: ACK W1 committed
WR-->>C: Success W1 globally visible
C->>EU: Read balance
EU-->>C: $1500 (W1 visible in all regions)
sequenceDiagram
participant A as Client A (has session token)
participant B as Client B (new SDK no token)
participant DB as Cosmos DB
A->>DB: Write W1 (deposit $500)
DB-->>A: Success + session token LSN:1042
Note over A: SDK stores LSN:1042
A->>DB: Read balance [token: LSN:1042]
DB-->>A: $1500 (replica at LSN >= 1042 guaranteed fresh)
B->>DB: Read balance [no token]
DB-->>B: $1000 (nearest replica may predate W1)
Observable outcome: The diagram below shows a three-region account β the client's success message arrives only after West Europe and East US have both confirmed.
Stage 5 β Real-World Deployments β Which Level Serves Banking, Social Media, and Audit Logs
- Financial Services: Account Balances and Inventory Counters Banking applications require Strong consistency for balance reads.
- A balance that shows the pre-deposit amount for even a single read can trigger incorrect overdraft fees, failed payment authorizations, or reconciliation discrepancies.
- The 2x RU cost and higher write latency are justified β incorrect balance data causes real monetary loss.
- E-commerce inventory counters face the same trade-off at higher write frequency.
Observable outcome: E-commerce inventory counters face the same trade-off at higher write frequency.
Stage 6 β Choosing the Right Level β Decision Flowchart and Use-Case Reference
- The right consistency level depends on three questions: Does stale data cause financial or correctness harm?
- Do users need to see their own writes immediately?
- Does the order of writes matter?
- Walk this flowchart per-feature, not per-account.
flowchart TD
START[What consistency level do I need?]
START --> Q1{Is stale data business-critical? money / inventory / compliance}
Q1 -->|"Yes"| Q2{Single write region or multi-region writes?}
Q2 -->|"Single write region"| STRONG[Strong Linearizable no staleness 2x RU on reads]
Q2 -->|"Multi-region writes"| BS[Bounded Staleness Strongest with multi-write Set T or K to your SLA]
Q1 -->|"No"| Q3{Must users see their own writes immediately?}
Q3 -->|"Yes"| SESSION[Session Default Read-your-own-writes via token Maintain token in serverless!]
Q3 -->|"No"| Q4{Does write ORDER matter? audit log or event history}
Q4 -->|"Yes sequence matters"| CP[Consistent Prefix No gaps, no reversals Recency not guaranteed]
Q4 -->|"No any order is fine"| EVENTUAL[Eventual Max throughput Only for staleness-tolerant data]
style STRONG fill:#c0392b,color:#fff
style BS fill:#e67e22,color:#fff
style SESSION fill:#27ae60,color:#fff
style CP fill:#2980b9,color:#fff
style EVENTUAL fill:#8e44ad,color:#fff
Observable outcome: Walk this flowchart per-feature, not per-account.
End-to-end outcome
After working through the stages, you should be able to explain:
- "Session" means client-session-token continuity β not HTTP session, not database session.
- The token is an SDK-managed string encoding the LSN your client has seen.
Alternatives considered
The source focuses on one primary design. A second architecture is not compared, so no alternative is invented here.
Failure and degraded modes
The CAP and PACELC Trade-offs That Forced Five Levels Into Existence
- Detection: A single consistency level cannot serve every application in a globally distributed database.
- User or operator impact: A single consistency level cannot serve every application in a globally distributed database.
Full Guarantee Comparison
- Detection: Full Guarantee Comparison
- User or operator impact: Full Guarantee Comparison
Failure Modes That Catch Teams Off-Guard
- Detection: Strong + multi-region write = hard platform rejection.
- User or operator impact: Strong + multi-region write = hard platform rejection.
This section walks through the exact failure the banking app experienced step by step and shows the fix.
- Detection: Why this scenario matters: It represents the most common Cosmos DB production consistency bug.
- User or operator impact: Why this scenario matters: It represents the most common Cosmos DB production consistency bug.
The Failing Flow β New SDK Client Per Invocation
- Detection: No error is thrown because Cosmos DB behaves correctly per the Session consistency contract.
- User or operator impact: No error is thrown because Cosmos DB behaves correctly per the Session consistency contract.
The Fix β Externalizing the Session Token
- Detection: Three approaches, in order of simplicity: 1.
- User or operator impact: Three approaches, in order of simplicity: 1.
