Lesson 4 of 4 Developing with Azure Cosmos DB for NoSQL

Choosing Consistency Levels in Cosmos DB

This lesson explains the five consistency levels, their effect on cost, latency and availability, and how session tokens and per-request consistency work.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the guarantees of each consistency level.
  • Compare the read cost of the levels.
  • Explain how session tokens provide read-your-own-writes.
  • Describe how clients relax consistency.

Why consistency is a choice

Cosmos DB keeps at least four replicas of every partition in each region, and an account can span several regions. A write must reach replicas before a later read is guaranteed to see it, and waiting for more replicas, or for distant regions, takes time. Consistency levels let an application choose how much waiting it needs: stronger levels guarantee fresher, more predictable reads at the cost of latency, throughput and, across regions, availability; weaker levels return faster and cheaper but may return older data.

The account has a default consistency level, set in its configuration. Clients use the default unless they request a weaker level, and a request can never be stronger than the account default. The default for new accounts is Session.

The five levels

The levels form a spectrum from strongest to weakest. Each guarantees what the weaker ones do, plus more.

  • Strong: every read returns the most recent committed write. Reads behave as if there were a single copy of the data. Strong cannot be used with multiple write regions, and reads are served from a quorum of replicas.
  • Bounded staleness: reads may lag behind writes, but by no more than a configured number of versions (K) or a time interval (T), whichever is reached first. Outside the staleness window it behaves like strong; within a single region it gives near-strong guarantees with better availability.
  • Session: within one client session, reads see the session's own writes (read-your-own-writes), in order, and never go backwards (monotonic reads). Other clients may see older data. A session token returned with each write carries the guarantee.
  • Consistent prefix: reads never see writes out of order. Single-item writes otherwise behave with eventual consistency, while writes made in one transactional batch become visible together.
  • Eventual: no ordering guarantee; replicas converge eventually. It offers the lowest latency and the highest read throughput.
Put in order

From strongest to weakest

Exam skills 2.1.2

Arrange the five Cosmos DB consistency levels from strongest to weakest.

Log in to try this activity Interactive activities are free with an account.

Cost, latency and availability

The levels differ in measurable ways, which the exam often turns into the deciding factor. Reads at Strong and Bounded staleness are served from two replicas (a minority quorum) and cost about twice the RUs of reads at the weaker levels, which read from one replica. Writes cost the same at every level. Strong consistency across regions adds the round-trip time to other regions to every write, because writes must commit in all regions.

Strong
Read RU costAbout 2x
GuaranteeLatest committed write
Multi-region writesNot supported
Choose when Correctness over everything, single write region
Bounded staleness
Read RU costAbout 2x
GuaranteeLag within K versions or T time
Multi-region writesSupported
Choose when Global apps that need predictable lag
Session
Read RU cost1x
GuaranteeRead-your-own-writes in a session
Multi-region writesSupported
Choose when Most user-facing apps
Eventual
Read RU cost1x
GuaranteeNone beyond convergence
Multi-region writesSupported
Choose when Counters, likes, non-critical feeds

Session is the default because most applications only need users to see their own changes, such as a chat message they just sent, and Session provides that at the lowest read cost.

Scenario questions

Picking a consistency level

Exam skills 2.1.2

Choose the consistency level that fits each requirement.

Log in to try this activity Interactive activities are free with an account.

Session tokens and per-request consistency

With Session consistency, the guarantee belongs to whoever holds the session token. Within one CosmosClient, the Python SDK tracks session tokens automatically, so a process that writes and then reads sees its own write. When the write and the read happen in different processes, such as an API instance that writes and a different instance that serves the next request, the token must be passed along, or the second process may read an older version. Routing a user's requests to the same instance, or passing the token in the request, preserves the guarantee.

A client can relax consistency for its own reads, for example to Eventual for an analytics page that tolerates stale data, by setting consistency_level when creating the client. The relaxed level lowers read cost only when the account default is Strong or Bounded staleness; it cannot strengthen consistency beyond the default.

from azure.cosmos import CosmosClient
from azure.identity import DefaultAzureCredential

reporting_client = CosmosClient("https://ACCOUNT.documents.azure.com:443/",
                                credential=DefaultAzureCredential(),
                                consistency_level="Eventual")

The following diagram traces the write and the reload with Session consistency.

Read-your-own-writes with Session consistency. API instance A writes the message and receives a session token, which travels with the client to the instance that serves the reload. Instance B reads with that token, so the response includes the new message, at the same read cost as Eventual consistency.
Routing a user's requests to one instance is the alternative to passing the token.
True or false

Session tokens

Exam skills 2.1.2

Decide whether each statement about Session consistency is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

Users who cannot see their own message

A chat application writes each user message to Cosmos DB through one API instance and then reloads the conversation through another instance behind a load balancer. Occasionally the user's new message is missing from the reloaded conversation for a moment. The account uses Eventual consistency to minimize cost.

The requirement is read-your-own-writes for each user, which is exactly what Session consistency guarantees, at the same read RU cost as Eventual. Changing the account default to Session, and passing the session token from the write response to the instance that serves the reload (or routing a user's requests to one instance), removes the missing-message effect.

Strong consistency would also fix the problem but doubles read RUs and adds latency for no benefit. Consistent prefix guarantees order but not that a reader sees its own latest write. Adding a delay before reloading would only make the problem rarer.

Check yourself

The account default is Session. Can a client request Strong consistency for an important read?

Show answer

No. A client can only relax consistency below the account default, never strengthen it. Strong reads require Strong as the account default.

Exam Tips

Remember for the exam

  • Five levels: Strong, Bounded staleness, Session (default), Consistent prefix, Eventual.
  • Strong and Bounded staleness reads cost about twice the RUs of the weaker levels.
  • Strong cannot be combined with multiple write regions.
  • Clients may relax consistency per client or request, never strengthen it beyond the account default.