Question

Difficulty: HardPerform Container and Item Operations in Azure Cosmos DB using SDK

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?

  1. 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));
  2. // 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
  3. 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);
  4. 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

1
Capture the session token from the write operation's output headers in the writing client instance.
The session token string is retrieved from the `Session` property of the `Headers` object in the write response.
The write response headers contain the latest session state required to preserve session guarantees on subsequent operations.
2
Pass the captured session token to the reading client instance.
The reading client instance obtains the session token string.
Different client instances do not share session tokens automatically, so the token must be communicated out-of-band or via application logic.
3
Instantiate an `ItemRequestOptions` object on the reading client instance and set its `SessionToken` property to the captured token.
A request options object configured with the session token is created.
The .NET SDK v3 requires the session token to be supplied per-request via `ItemRequestOptions` to enforce session consistency.
4
Execute the point read operation using `ReadItemAsync`, passing the item ID, the partition key (constructed from `/userId`), and the request options.
The item is successfully retrieved with read-your-writes guarantees.
Providing the session token and the partition key ensures a consistent, low-latency point read of the updated item.

Key Concept

Session token propagation across different SDK client instances using ItemRequestOptions
Estimated Time:2m 0s
Rate this question