cosmos-db
  1. cosmos-db-strong

Strong Consistency in CosmosDB

Explanation

CosmosDB provides multiple consistency levels to choose from based on your application needs. Strong consistency guarantees that every read operation will return the most recently written version of the data. In other words, it ensures that all replicas of the data have been updated before a read operation returns.

Syntax

To set strong consistency level in CosmosDB, you can use the following syntax:

consistency = Strong

Example

The following code example shows how to set strong consistency level in CosmosDB SDK:

DocumentClient client = new DocumentClient(new Uri(endpointUrl), authKey);
FeedOptions queryOptions = new FeedOptions { ConsistencyLevel = ConsistencyLevel.Strong };
IQueryable<Document> queryable = client.CreateDocumentQuery<Document>(collectionLink, queryOptions);

Use

Strong consistency is best suited for applications that need immediate consistency and cannot tolerate any stale data. It's ideal for applications that require transactional consistency, such as banking or financial applications.

Important Points

  • Strong consistency can impact the performance and availability of your database, as it requires all replicas to be updated before a response can be sent.
  • Strong consistency may not be feasible in a globally distributed environment, as it can significantly increase the latency of cross-region queries.
  • CosmosDB allows you to set consistency level at both database and container levels.

Summary

Strong consistency in CosmosDB ensures that every read operation will return the most recently written version of the data. While it's ideal for applications that require transactional consistency, it can impact performance and availability of your database, and may not be suitable for globally distributed environments.

Published on: