Question

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

You are developing a C# backend service that manages customer order logs using the Azure Cosmos DB .NET SDK v3. The Cosmos DB container uses Session consistency and is partitioned by the customer's identifier (/customerId). A different client session has just created a new order log item with the ID "order-789" for the customer "customer-101". Your service must perform a point read to retrieve this new log item immediately, ensuring it reads the latest write. Which C# code segment should you use?

  1. string sessionToken = GetWriterSessionToken();
    ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
    ItemResponse<OrderLog> response = await container.ReadItemAsync<OrderLog>("order-789", new PartitionKey("customer-101"), options);
    Answer
  2. B
    ItemResponse<OrderLog> response = await container.ReadItemAsync<OrderLog>("order-789", new PartitionKey("customer-101"));
  3. C
    string sessionToken = GetWriterSessionToken();
    ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
    ItemResponse<OrderLog> response = await container.ReadItemAsync<OrderLog>("order-789", new PartitionKey("completed"), options);
  4. D
    string sessionToken = GetWriterSessionToken();
    ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
    ItemResponse<OrderLog> response = await container.ReadItemAsync<OrderLog>("order-789", PartitionKey.None, options);

Answer

The correct option is the one that retrieves the write session token, configures it in ItemRequestOptions, and passes both the item ID and the customer-101 partition key to ReadItemAsync.
The correct option obtains the session token from the write operation, configures it in ItemRequestOptions, and calls ReadItemAsync with the item ID and the partition key containing the customer identifier. Under Azure Cosmos DB's Session consistency, sharing the session token is required to guarantee read-your-writes consistency across different client sessions.

Step-by-Step Solution

1
Obtain the session token from the write operation that occurred in the other client session.
The token is stored in a string variable.
Under Session consistency, read-your-writes guarantees across different client sessions require passing the session token.
2
Create an instance of ItemRequestOptions and assign the SessionToken property.
An ItemRequestOptions object configured with the session token.
This request option instructs the Cosmos DB client to read data up to at least the point of the session token.
3
Call ReadItemAsync on the container object, passing the item ID, the PartitionKey containing customer-101, and the request options.
An ItemResponse containing the retrieved OrderLog item.
Cosmos DB .NET SDK v3 requires both the item ID and the partition key value to perform a point read operation.

Key Concept

Session consistency requires passing the session token across different client sessions to guarantee read-your-writes consistency during item operations.
Rate this question