An enterprise microservice architecture uses an Azure Cosmos DB SQL API container to store user profiles. The container's partition key path is set to `/userId`, and the database account uses Session consistency. Each user profile is managed by independent client applications running on different host nodes.
A write operation executed by one client node updates a user profile. Immediately following this write, a second client node needs to retrieve the updated profile. To achieve read-your-writes consistency across these separate client nodes, you must retrieve the write operation's session state and apply it to the read request.
Which C# code segment using the Cosmos DB .NET SDK v3 correctly implements this consistency requirement?
- A// App Service A
await container.UpsertItemAsync<UserProfile>(profile, new PartitionKey(profile.UserId));
// App Service B
ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(userId, new PartitionKey(userId)); - // App Service A
ItemResponse<UserProfile> response = await container.UpsertItemAsync<UserProfile>(profile, new PartitionKey(profile.UserId));
string sessionToken = response.Headers.Session;
// App Service B
ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(userId, new PartitionKey(userId), options);Answer - C// App Service A
ItemResponse<UserProfile> response = await container.UpsertItemAsync<UserProfile>(profile, new PartitionKey(profile.Status));
string sessionToken = response.Headers.Session;
// App Service B
ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(userId, new PartitionKey(profile.Status), options); - D// App Service A
ItemResponse<UserProfile> response = await container.UpsertItemAsync<UserProfile>(profile, new PartitionKey(profile.UserId));
string sessionToken = response.Headers.Session;
// App Service B
CosmosClientOptions clientOptions = new CosmosClientOptions { ApplicationName = sessionToken };
CosmosClient client = new CosmosClient(connectionString, clientOptions);
Container container = client.GetContainer(dbId, containerId);
ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(userId, new PartitionKey(userId));
Answer
The correct option captures the session token from the headers of the write response and sets it on the request options of the read operation.
The correct option captures the session token string from the write response headers (`response.Headers.Session`) on the first client instance, transmits it to the second client instance, and assigns it to the `SessionToken` property of the `ItemRequestOptions` object passed to the read operation. This ensures that the second instance can perform a point read that acknowledges the session state of the write operation, fulfilling the read-your-writes consistency model.
Step-by-Step Solution
Key Concept
Session token propagation across different SDK client instances using ItemRequestOptions
Estimated Time:2m 0s