Cosmos DB — Consistency Levels

Let us now explore different consistency levels provided by CosmosDB. Most of databases available today provide only strong and eventual consistency whereas Cosmos DB provides five different level of consistency.

The consistency levels are region-agnostic and are guaranteed for all operations regardless of the region from which the reads and writes are served, the number of regions associated with your Azure Cosmos account, or whether your account is configured with a single or multiple write regions.

Let’s understand the guarantees associated with each consistency levels. To start with we will look at read consistency levels.

Strong: Strong consistency offers a linearizability guarantee. Linearizability refers to serving requests concurrently. The reads are guaranteed to return the most recent committed version of an item. A client never sees an uncommitted or partial write. Users are always guaranteed to read the latest committed write.

Bounded staleness: The reads are guaranteed to honor the consistent-prefix guarantee. The reads might lag behind writes by at most “K” versions (i.e “updates”) of an item or by “T” time interval, whichever is reached first. Inside the staleness window, Bounded Staleness provides the following consistency guarantees:

  • Consistency for clients in the same region for an account with single write region = Strong
  • Consistency for clients in different regions for an account with single write region = Consistent Prefix
  • Consistency for clients writing to a single region for an account with multiple write regions = Consistent Prefix
  • Consistency for clients writing to different regions for an account with multiple write regions = Eventual

Bounded staleness is frequently chosen by globally distributed applications that expect low write latencies but require total global order guarantee. Bounded staleness is great for applications featuring group collaboration and sharing, stock ticker, publish-subscribe/queueing etc.

Session: Within a single client session reads are guaranteed to honor the consistent-prefix, monotonic reads, monotonic writes, read-your-writes, and write-follows-reads guarantees. This assumes a single “writer” session or sharing the session token for multiple writers.

Clients outside of the session performing writes will see the following guarantees:

  • Consistency for clients in same region for an account with single write region = Consistent Prefix
  • Consistency for clients in different regions for an account with single write region = Consistent Prefix
  • Consistency for clients writing to a single region for an account with multiple write regions = Consistent Prefix
  • Consistency for clients writing to multiple regions for a account with multiple write regions = Eventual

Session consistency provides write latencies, availability, and read throughput comparable to that of eventual consistency but also provides the consistency guarantees that suit the needs of applications written to operate in the context of a user.

Consistent prefix: Updates that are returned contain some prefix of all the updates, with no gaps. Consistent prefix consistency level guarantees that reads never see out-of-order writes.

If writes were performed in the order A, B, C, then a client sees either A, A,B, or A,B,C, but never out-of-order permutations like A,C or B,A,C. Consistent Prefix provides write latencies, availability, and read throughput comparable to that of eventual consistency, but also provides the order guarantees that suit the needs of scenarios where order is important.

Below are the consistency guarantees for Consistent Prefix:

  • Consistency for clients in same region for an account with single write region = Consistent Prefix
  • Consistency for clients in different regions for an account with single write region = Consistent Prefix
  • Consistency for clients writing to a single region for an account with multiple write region = Consistent Prefix
  • Consistency for clients writing to multiple regions for an account with multiple write region = Eventual

Eventual: There’s no ordering guarantee for reads. In the absence of any further writes, the replicas eventually converge. Eventual consistency is the weakest form of consistency because a client may read the values that are older than the ones it had read before. Eventual consistency is ideal where the application does not require any ordering guarantees. Examples include count of Retweets, Likes, or non-threaded comments.

In the next part of the article, we will explore about different partitioning strategy. Stay tuned!

References: