Sharing Session Tokens in Azure Cosmos DB Multi-Node Apps
You manage an Azure Cosmos DB for a NoSQL API account named account1. The account contains a database named db1, which contains a container named container1. You configure account1 with a session consistency level. You plan to develop an application named App1 that will access container1. Individual instances of App1 must perform reads and writes. App1 must allow multiple nodes to participate in the same session. You need to configure an object to share the session token between the nodes. Which object should you use?
Community Insight
The question tests the understanding of session consistency mechanics in distributed Azure Cosmos DB clients, specifically how to maintain a consistent read-write view across multiple nodes using the session token.
This guide explains how to configure Azure Cosmos DB for NoSQL to support session consistency across multiple application nodes by sharing the session token. It establishes that Request options are the correct mechanism to pass the token between client instances.
Candidates often choose 'Connection policy' because it configures general client behavior, but it does not handle the per-request session state required for multi-node consistency.
Community Discussion (1 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
In Azure Cosmos DB with session consistency, each client instance maintains its own session token to ensure monotonic reads and bounded staleness. When an application like App1 runs on multiple nodes behind a load balancer, different nodes may have different session tokens. To allow these nodes to participate in the same session, the session token must be shared. This is achieved by capturing the token from a write operation (viaResponse.SessionToken) and then explicitly passing it in subsequent requests using RequestOptions.SessionToken. This ensures all nodes see the most recent committed data.Why the Other Options Are Wrong
A Document response contains the session token header, but it is the source of the token, not the configuration object used to share or apply it to future requests. A Connection policy defines global settings like retry policies and connection modes, but it cannot store dynamic session tokens. Feed options are used for querying containers (e.g., pagination), not for managing the session consistency context of individual item operations.Community Comment Notes
As Ja13 noted, "With session consistency, each client instance keeps its own session token... To share the session: Node 1 writes and gets the token... The token goes to the user." This confirms that the token must be manually propagated via request options to maintain consistency across distributed nodes.Exam Strategy
Always distinguish between configuring the client (Connection Policy) and configuring the individual operation (Request Options). For session consistency in distributed apps, remember that the session token is a per-request property that must be explicitly passed if you want to share state across different client instances.
Frequently Asked Questions
Why can't I use Connection Policy to share session tokens?
Connection Policy configures static client behaviors like retry limits. Session tokens are dynamic values that change with every write and must be applied per-request via RequestOptions.
What happens if I don't share the session token?
Different nodes will have different session views, potentially leading to stale reads or violations of monotonic read guarantees in a session-consistent account.