Edit

Manage consistency levels in Azure Cosmos DB

This article explains how to manage consistency levels in Azure Cosmos DB. You learn how to configure the default consistency level, override the default consistency, manually manage session tokens, and understand the Probabilistically Bounded Staleness (PBS) metric.

As you change your account level consistency, ensure you redeploy your applications and make any necessary code modifications to apply these changes.

Note

We recommend that you use the Azure Az PowerShell module to interact with Azure. To get started, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

Configure the default consistency level

To learn more about the default consistency level, see Consistency levels in Azure Cosmos DB.

To view or modify the default consistency level:

  1. Sign in to the Azure portal.

  2. Find your Azure Cosmos DB account, and open the Default consistency pane.

  3. Select the level of consistency you want as the new default, and then select Save.

The Azure portal also provides a visualization of different consistency levels with music notes.

Screenshot of the consistency menu in the Azure portal.

Override the default consistency level

The service sets the default consistency level, but clients can override it. The consistency level can be set on a per-request basis, which overrides the default consistency level set at the account level.

Tip

When using the traditional ConsistencyLevel override, consistency can only be relaxed at the SDK instance or request level. To move from weaker to stronger consistency using this approach, update the default consistency for the Azure Cosmos DB account. However, the newer ReadConsistencyStrategy feature (Java SDK v4.69+, .NET SDK v3.46+) allows you to set any supported read-consistency strategy per-read, including stronger than the account default, without changing the account configuration.

Tip

Overriding the default consistency level only applies to reads within the SDK client. An account configured for strong consistency by default still writes and replicates data synchronously to every region in the account. When the SDK client instance or request overrides this level with Session or weaker consistency, reads are performed using a single replica. For more information, see Consistency levels and throughput.

.NET SDK

// Override consistency at the client level
documentClient = new DocumentClient(new Uri(endpoint), authKey, connectionPolicy, ConsistencyLevel.Eventual);

// Override consistency at the request level via request options
RequestOptions requestOptions = new RequestOptions { ConsistencyLevel = ConsistencyLevel.Eventual };

var response = await client.ReadDocumentAsync(collectionUri, document, requestOptions);

Java V4 SDK

Java SDK V4 (Maven com.azure::azure-cosmos) Async API


CosmosAsyncClient client =
        new CosmosClientBuilder()
                .endpoint(HOST)
                .key(MASTER_KEY)
                .consistencyLevel(ConsistencyLevel.EVENTUAL)
                .buildAsyncClient();

Java V2 SDKs

Async Java V2 SDK (Maven com.microsoft.azure::azure-cosmosdb)

// Override consistency at the client level
ConnectionPolicy policy = new ConnectionPolicy();

AsyncDocumentClient client =
        new AsyncDocumentClient.Builder()
                .withMasterKey(this.accountKey)
                .withServiceEndpoint(this.accountEndpoint)
                .withConsistencyLevel(ConsistencyLevel.Eventual)
                .withConnectionPolicy(policy).build();

Node.js/JavaScript/TypeScript SDK

// Override consistency at the client level
const client = new CosmosClient({
  /* other config... */
  consistencyLevel: ConsistencyLevel.Eventual
});

// Override consistency at the request level via request options
const { body } = await item.read({ consistencyLevel: ConsistencyLevel.Eventual });

Python SDK

# Override consistency at the client level
connection_policy = documents.ConnectionPolicy()
client = cosmos_client.CosmosClient(self.account_endpoint, {
                                    'masterKey': self.account_key}, connection_policy, documents.ConsistencyLevel.Eventual)

Go SDK

Define consistency level at the request:

container, _ := c.NewContainer("moviesdb", "movies")

container.NewQueryItemsPager("select * from c", azcosmos.NewPartitionKey(), &azcosmos.QueryOptions{
		ConsistencyLevel: azcosmos.ConsistencyLevelEventual.ToPtr(),
})

container.ReadItem(context.Background(), azcosmos.NewPartitionKeyString("Quentin Tarantino"), "Pulp Fiction", &azcosmos.ItemOptions{
		ConsistencyLevel: azcosmos.ConsistencyLevelStrong.ToPtr(),
})

Use Read Consistency Strategy

The ReadConsistencyStrategy feature (available in Java SDK v4.69+ and .NET SDK v3.46+) provides a more flexible way to control read consistency. Unlike the traditional ConsistencyLevel override which can only relax consistency, ReadConsistencyStrategy allows you to set any supported read-consistency strategy per-read, including stronger than the account default, without changing your account configuration.

Important

ReadConsistencyStrategy is currently in preview. It's supported in direct mode only and isn't available when using gateway mode.

Available strategies

Strategy Behavior Use case
DEFAULT Uses the account or client-level consistency setting No override needed
SESSION Read-your-writes and monotonic reads within a session Per-user consistency in web apps
EVENTUAL Maximum availability, minimum latency During outages when availability > consistency
LATEST_COMMITTED Performs quorum reads with barrier requests against the local region's replicas. The SDK reads from a read quorum of secondary replicas and uses barrier requests to ensure they have converged to the latest quorum-acknowledged (committed) LSN within that region. This gives you the freshest data that has been committed locally. On the write path, replication across regions remains asynchronous (no RPO boundary), which provides better write availability Strong consistency within a region without an RPO boundary
GLOBAL_STRONG Linearizable reads across all regions (synchronous) Financial transactions, inventory systems

Tip

LATEST_COMMITTED is often a better choice than bounded staleness when consistent reads are required but an RPO guarantee isn't. It performs quorum reads against secondary replicas within the local region, ensuring they have converged to the latest committed version. The trade-off: unlike bounded staleness, which guarantees reads lag behind writes by at most K versions or T time interval, LATEST_COMMITTED provides no such staleness bound across regions.

Java SDK

Client-level default:

CosmosAsyncClient client = new CosmosClientBuilder()
    .endpoint(endpoint)
    .credential(new DefaultAzureCredentialBuilder().build())
    .readConsistencyStrategy(ReadConsistencyStrategy.LATEST_COMMITTED)
    .buildAsyncClient();

Per-request override (for example, during a detected outage):

CosmosItemRequestOptions options = new CosmosItemRequestOptions();
options.setReadConsistencyStrategy(ReadConsistencyStrategy.EVENTUAL);
container.readItem(id, partitionKey, options, MyItem.class);

Per-request override for queries:

CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
queryOptions.setReadConsistencyStrategy(ReadConsistencyStrategy.LATEST_COMMITTED);

SqlQuerySpec querySpec = new SqlQuerySpec(
    "SELECT * FROM c WHERE c.category = @cat",
    new SqlParameter("@cat", "electronics"));
container.queryItems(querySpec, queryOptions, MyItem.class);

.NET SDK

Client-level default:

CosmosClient client = new CosmosClientBuilder(endpoint, new DefaultAzureCredential())
    .WithReadConsistencyStrategy(ReadConsistencyStrategy.LatestCommitted)
    .Build();

Per-request override:

ItemRequestOptions options = new ItemRequestOptions
{
    ReadConsistencyStrategy = ReadConsistencyStrategy.Eventual
};
var response = await container.ReadItemAsync<MyItem>(id, new PartitionKey(partitionKey), options);

Per-request override for queries:

QueryRequestOptions queryOptions = new QueryRequestOptions
{
    ReadConsistencyStrategy = ReadConsistencyStrategy.LatestCommitted
};
var iterator = container.GetItemQueryIterator<MyItem>("SELECT * FROM c", requestOptions: queryOptions);

ReadConsistencyStrategy vs ConsistencyLevel override

Capability ConsistencyLevel override ReadConsistencyStrategy
Relax consistency below account default
Strengthen consistency above account default
Set at client level
Set per-request
Dynamic runtime switching Limited ✅ Designed for this
Available strategies Strong, Bounded Staleness, Session, Consistent Prefix, Eventual (can only relax from account default) Default, Session, Eventual, Latest Committed, Global Strong

Note

When both ConsistencyLevel and ReadConsistencyStrategy are set on the same request, ReadConsistencyStrategy takes precedence.

Utilize session tokens

One of the consistency levels in Azure Cosmos DB is session consistency. This level is the default level applied to Azure Cosmos DB accounts. When working with session consistency, every new write request to Azure Cosmos DB is assigned a new SessionToken. The CosmosClient uses this token internally with each read/query request to ensure that the set consistency level is maintained.

In some scenarios, you need to manage this session yourself. Consider a web application with multiple nodes, each node has its own instance of CosmosClient. If you want these nodes to participate in the same session (to be able to read your own writes consistently across web tiers) you would have to send the SessionToken from FeedResponse<T> of the write action to the end-user using a cookie or some other mechanism, and have that token flow back to the web tier and ultimately the CosmosClient for subsequent reads. If you're using a round-robin load balancer that doesn't maintain session affinity between requests, such as the Azure Load Balancer, the read could potentially land on a different node to the write request, where the session was created.

If you don't flow the Azure Cosmos DB SessionToken across, you could end up with inconsistent read results for a while.

Session tokens in Azure Cosmos DB are partition-bound, meaning they're exclusively associated with one partition. In order to ensure you can read your writes, use the session token that was last generated for the relevant items. To manage session tokens manually, get the session token from the response and set them per request. If you don't need to manage session tokens manually, you don't need to use these samples. The SDK keeps track of session tokens automatically. If you don't set the session token manually, by default, the SDK uses the most recent session token.

.NET SDK

var response = await client.ReadDocumentAsync(
                UriFactory.CreateDocumentUri(databaseName, collectionName, "SalesOrder1"));
string sessionToken = response.SessionToken;

RequestOptions options = new RequestOptions();
options.SessionToken = sessionToken;
var response = await client.ReadDocumentAsync(
                UriFactory.CreateDocumentUri(databaseName, collectionName, "SalesOrder1"), options);

Java V4 SDK

Java SDK V4 (Maven com.azure::azure-cosmos) Async API


// Get session token from response
CosmosItemResponse<JsonNode> response = container.readItem(itemId, new PartitionKey(partitionKey), JsonNode.class).block();
String sessionToken = response.getSessionToken();

// Resume the session by setting the session token on the RequestOptions
CosmosItemRequestOptions options = new CosmosItemRequestOptions();
options.setSessionToken(sessionToken);
CosmosItemResponse<JsonNode> response2 = container.readItem(itemId, new PartitionKey(partitionKey), JsonNode.class).block();

Java V2 SDKs

Async Java V2 SDK (Maven com.microsoft.azure::azure-cosmosdb)

// Get session token from response
RequestOptions options = new RequestOptions();
options.setPartitionKey(new PartitionKey(document.get("mypk")));
Observable<ResourceResponse<Document>> readObservable = client.readDocument(document.getSelfLink(), options);
readObservable.single()           // we know there will be one response
  .subscribe(
      documentResourceResponse -> {
          System.out.println(documentResourceResponse.getSessionToken());
      },
      error -> {
          System.err.println("an error happened: " + error.getMessage());
      });

// Resume the session by setting the session token on RequestOptions
RequestOptions options = new RequestOptions();
requestOptions.setSessionToken(sessionToken);
Observable<ResourceResponse<Document>> readObservable = client.readDocument(document.getSelfLink(), options);

Node.js/JavaScript/TypeScript SDK

// Get session token from response
const { headers, item } = await container.items.create({ id: "meaningful-id" });
const sessionToken = headers["x-ms-session-token"];

// Immediately or later, you can use that sessionToken from the header to resume that session.
const { body } = await item.read({ sessionToken });

Python SDK

// Get the session token from the last response headers
item = client.ReadItem(item_link)
session_token = client.last_response_headers["x-ms-session-token"]

// Resume the session by setting the session token on the options for the request
options = {
    "sessionToken": session_token
}
item = client.ReadItem(doc_link, options)

Go SDK

// Get the session token from the create item response
resp, _ := container.CreateItem(context.Background(), azcosmos.NewPartitionKeyString("Quentin Tarantino"), movie, &azcosmos.ItemOptions{
	ConsistencyLevel: azcosmos.ConsistencyLevelSession.ToPtr(),
})

// Use the session token to read the item
container.ReadItem(context.Background(), azcosmos.NewPartitionKeyString("Quentin Tarantino"), movieId, &azcosmos.ItemOptions{
	SessionToken: resp.SessionToken,
})

Monitor Probabilistically Bounded Staleness metric

How eventual is eventual consistency? For the average case, we can offer staleness bounds with respect to version history and time. The Probabilistically Bounded Staleness (PBS) metric tries to quantify the probability of staleness and shows it as a metric.

To view the PBS metric:

  1. Go to your Azure Cosmos DB account in the Azure portal.

  2. Open the Metrics (Classic) pane, and select the Consistency tab.

  3. Look at the graph named Probability of strongly consistent reads based on your workload (see PBS).

Screenshot of the Probabilistically Bounded Staleness graph in the Azure portal.

Next steps

Learn more about how to manage data conflicts, or move on to the next key concept in Azure Cosmos DB.