Lesson 3 of 3 Semi-Structured Data Solutions

Choosing Cosmos DB Consistency and Write Models

This lesson explains the five consistency levels and multi-region writes.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the five consistency levels.
  • Choose a consistency level from stated requirements.
  • Compare single-region and multi-region writes.

The trade-off

When data is copied to several regions, a write cannot appear everywhere at the same moment. A database must therefore either wait for other regions, which adds latency, or return possibly stale data. Most databases offer only the two extremes. Cosmos DB offers five defined levels, so an architect can select a suitable point on this spectrum.

The five consistency levels

The levels below run from strongest to weakest. Consistency is set as a default for the account, and a client can relax it for a request.

Strong

Reads always return the latest committed write. Writes must be committed by a global majority of regions, so write latency rises with the distance between regions. Strong consistency gives an RPO of zero for a single write region, but it cannot be used with multiple write regions.

Bounded staleness

Reads may lag behind writes, but by no more than a configured number of versions or a time interval. It suits applications that need a known limit on staleness across regions, with better availability than Strong.

Session

Within one client session, a user always reads their own writes, and sees them in order. A client with no session token for a partition reads with Eventual behaviour, so other sessions may see older data for a short time. Session is the default and the most widely used level, as it provides what users expect at low cost, and it suits applications such as shopping carts or profiles.

Consistent prefix

Reads may be stale, and updates made as single document writes see eventual consistency. Updates made as a batch within a transaction are different: they become visible together, so a reader sees all of the transaction or none of it. It suits data written in related groups, such as an order and its line items, where a partly applied change must never appear.

Eventual

Replicas eventually converge, but reads may be out of order for a time. Eventual is the cheapest level for reads, and it suits data where order and immediate accuracy do not matter, such as a count of likes.

The following table recaps the levels with a typical use for each.

LevelGuaranteeExample use
StrongAlways the latest committed valueFinancial balances in one write region
Bounded stalenessLag limited by time or versionsInventory visible globally within a set delay
SessionA client reads its own writesShopping carts, user profiles
Consistent prefixTransactions appear wholeAn order with its line items
EventualNo ordering guaranteeLike counters, non-critical telemetry

The following diagram places Session among the Cosmos DB consistency levels and shows what each reader of a user's post sees.

Choosing consistency for user posts. Session consistency lets a user read their own writes immediately within the session; friends, without that session's token, read with eventual behaviour, seeing a post seconds later and possibly out of order, which the platform accepts. Multi-region writes in Asia, Europe and North America give low write latency.
The scenario is illustrative.
Sort into groups

Pick the consistency level

Exam skills 2.2.1

Choose the consistency level that fits each kind of data.

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

Global distribution

Adding a region to an account places a full replica of the data there, and clients read from their nearest region, reducing latency. Single-write-region accounts have one write region and any number of read regions. If that region fails, service-managed failover can promote another region, according to a priority list.

Multi-region writes allow all regions to accept writes, with the highest availability and low write latency worldwide. Because two regions could update the same item at once, a conflict can occur. The default resolution is last write wins, based on a timestamp property, and a custom resolution procedure is available on the NoSQL API. Designs in which conflicts cannot be tolerated should use a single write region. Multi-region writes are incompatible with Strong consistency, which is a frequent exam point.

An account with Strong consistency and regions separated by more than 5,000 miles is blocked by default, because of the latency of a global commit.

True or false

Distributing globally

Exam skills 2.2.1

Check the behaviour of global distribution in Cosmos DB.

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

Comparing write models

The following comparison recaps the two global configurations.

Single write region
Write latency worldwideLow only near the write region
ConflictsNone
Strong consistencyAvailable
Regional write failureFailover needed
Choose when Data needing strict correctness
Multi-region writes
Write latency worldwideLow everywhere
ConflictsPossible, resolved by policy
Strong consistencyNot available
Regional write failureOther regions continue
Choose when Global applications that accept conflict resolution
Worked example

Selecting consistency

A global social platform stores user posts. A user who publishes a post must see it immediately, but friends may see it a few seconds later. The order in which friends see posts is not critical. Users in Asia, Europe and North America must get low write latency. What should you recommend?

The first requirement matches Session consistency: a user reads their own writes within a session. Friends have no session token for those writes, so they read with Eventual behaviour, which the scenario accepts: they may see a post a few seconds later, and possibly out of order.

Low write latency on three continents requires multi-region writes, which rules out Strong consistency. Bounded staleness could work, but it adds cost and offers a guarantee that the scenario does not ask for. Conflicts on posts are rare, because a user edits their own content, so last write wins is acceptable.

Check yourself

Why can multi-region writes not use Strong consistency?

Show answer

Strong requires a global commit, which conflicts with regions accepting writes independently.

Exam Tips

Remember for the exam

  • From strongest to weakest: Strong, Bounded staleness, Session, Consistent prefix, Eventual.
  • Session is the default: a client reads its own writes.
  • Strong cannot be combined with multi-region writes.
  • Default conflict resolution is last write wins.