Home / Cosmos DB / Engineer Course
V4
🎚️
Lesson · 5 min

Consistency Levels

Five knobs you can dial per request — what they cost and what they guarantee.

TL;DR

Most databases give you two choices — strong (slow, correct) or eventual (fast, possibly wrong). Cosmos gives you five, each with a precise guarantee and a published RU cost. The default for 90% of apps is **session** — a client reads its own writes, never sees its own data go backwards, and pays a fraction of the cost of strong consistency.

Key takeaways
  • Five levels, ordered strong → bounded staleness → session → consistent prefix → eventual. Pick once at account level, override per request.
  • Session is the default and the right answer almost always. Read-your-writes within a session, no monotonicity violations.
  • Strong consistency disables multi-region writes — you can only have one or the other.
  • Reads under stronger consistency cost more RUs (up to 2× for strong vs eventual).
  • The right level isn't a developer preference, it's a product question — what's the worst thing that happens if a user sees a stale value?

There’s a famous result in distributed systems called the CAP theorem — when the network partitions (and it always eventually does), you must choose between consistency and availability. You can’t have both at full strength. Cosmos exposes this trade-off as five named, well-defined points on a continuum, and lets you pick which one you want — globally and per-request.

The five levels at a glance

From strongest (slowest, most expensive) to weakest (fastest, cheapest):

Strong

Every read sees the most recent committed write, globally. Linearizable. Disables multi-region writes — you can only have one write region. Use for — bank balances, inventory at last-item, anything where stale = money lost.

Bounded staleness

Reads can lag by at most K operations or T time (e.g. 100,000 versions or 5 seconds, whichever first). Within those bounds, you get a clear “how stale” guarantee. Use for — leaderboards, fraud signals, “fresh enough” dashboards.

Session (default)

Within a single client session, you have read-your-writes consistency, monotonic reads, and consistent prefix. Across sessions, eventual. Use for — virtually every CRUD app, every social feed, every CMS, every cart. The pragmatic default.

Consistent prefix

You may see stale data, but you’ll never see writes out of order. If A happened before B, you’ll never see B without A. Use for — comment threads, event logs, anything where order matters more than freshness.

Eventual

All replicas converge eventually. No ordering, no monotonicity, no read-your-writes. Cheapest. Use for — view counts, like counters, recently-viewed lists.

The session token, demystified

When you use session consistency (the default), the SDK silently maintains a session token per CosmosClient instance. It looks like this — 0:-1#42 — and represents “I’ve seen up to logical sequence 42 on this partition.”

Every write returns a token. Every read sends the latest token along — the read is served from a replica that’s caught up to at least that point. The replica responds with its own current token, and the SDK keeps the highest one for next time.

What this gives you — a single user clicking “save” and then “view” always sees their save. Different users (different sessions) may see things in different orders briefly. That’s the session boundary.

If you need to share session state across processes (e.g. the API server writes, the worker reads), you can serialize and pass the token explicitly via CosmosClient.RequestOptions.

RU cost by level

Reads scale with consistency strength. Approximate cost of a single point read:

LevelRU cost
Strong2.0
Bounded staleness2.0
Session1.0
Consistent prefix0.5
Eventual0.5

Writes are the same cost across levels — Cosmos still has to commit them everywhere either way.

The multi-region constraint

This is the most surprising rule — strong consistency forbids multi-region writes. The reason is mechanical — to guarantee linearizability across regions, every write would need a round-trip to every region, killing throughput. Cosmos refuses to let you opt into that combination.

If you want both global writes and a strict guarantee, the right choice is bounded staleness — strict by some bound, but the bound is per-region.

Picking yours

Don’t pick by gut. Ask the product question — what’s the cost of a stale read?

  • “We refund the user, that’s it” → session or weaker.
  • “We oversell the last item and disappoint a customer” → bounded staleness.
  • “We double-charge or under-charge a financial transaction” → strong.

Most teams pick session at the account level and override to strong on the 2-3 endpoints that genuinely need it.

🎯 Common questions
Q1. If session is good enough for 90% of apps, why does Cosmos offer five levels?

Because the other 10% really matter. Financial ledgers genuinely need strong; analytics dashboards genuinely benefit from eventual. The five-level menu lets you match the guarantee to the workload instead of paying for the worst case everywhere.

Q2. What does "session" actually track?

A session token — a small opaque string the SDK attaches to your CosmosClient. Every write returns a new token; subsequent reads from the same client are guaranteed to see at least that write. Different clients (different processes, browsers, instances) have independent sessions and may temporarily disagree.

Q3. Can I mix consistency levels in one app?

Yes — you set the *default* on the account, then override on a per-request basis (downward only — you can weaken from the default but not strengthen above it). Use strong for the "place order" call; use eventual for the "show recent activity" feed.

📺 Video

The lesson video is on YouTube — coming once the upload goes public.

🧪 Simulator

A live simulator for this lesson's mechanic (e.g. RU calculator, partition-key picker). Coming in Phase 2.

🎨 Visualization

An interactive diagram of this lesson's core idea — coming as we build out the visualization library.

💻 Code

A copy-paste reference snippet plus a short build challenge.

Comments 0

Discuss this page. Markdown supported. Be kind.

Loading…
Loading comments…