Question

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

You are developing a C# backend service for a multi-tenant SaaS document editor. The Azure Cosmos DB container contains document metadata and is configured with Session consistency. The container's partition key is `/tenantId`.

Initially, the development team considered partitioning the container by a status field, `/isArchived`, but chose `/tenantId` to avoid hot partitions.

A user reports that when they modify a document on one device, the updated metadata is not immediately visible when they open the application on another device (which runs in a separate client session). You capture the session token from the write operation's response on the first device as `capturedSessionToken`.

You need to perform a point read on the second device to guarantee that the user reads the latest update.

Which C# code segment should you use?

  1. ItemRequestOptions options = new ItemRequestOptions { SessionToken = capturedSessionToken };
    ItemResponse<DocumentMetadata> response = await container.ReadItemAsync<DocumentMetadata>(
    documentId,
    new PartitionKey(tenantId),
    options
    );
    Answer
  2. B
    ItemResponse<DocumentMetadata> response = await container.ReadItemAsync<DocumentMetadata>(
    documentId,
    new PartitionKey(tenantId)
    );
  3. C
    ItemRequestOptions options = new ItemRequestOptions { SessionToken = capturedSessionToken };
    ItemResponse<DocumentMetadata> response = await container.ReadItemAsync<DocumentMetadata>(
    documentId,
    new PartitionKey(isArchived),
    options
    );
  4. D
    RequestOptions options = new RequestOptions { SessionToken = capturedSessionToken };
    var response = await client.ReadDocumentAsync<DocumentMetadata>(
    UriFactory.CreateDocumentUri("DatabaseId", "ContainerId", documentId),
    new RequestOptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey(tenantId), SessionToken = capturedSessionToken }
    );

Answer

ItemRequestOptions options = new ItemRequestOptions { SessionToken = capturedSessionToken };
ItemResponse<DocumentMetadata> response = await container.ReadItemAsync<DocumentMetadata>(
documentId,
new PartitionKey(tenantId),
options
);
The correct option uses the standard Cosmos DB .NET SDK v3 ReadItemAsync method, specifies the correct partition key, and utilizes ItemRequestOptions to pass the captured session token. This ensures read-your-writes consistency across distinct client sessions under Session consistency.

Step-by-Step Solution

1
Instantiate the ItemRequestOptions object and set its SessionToken property to the captured session token from the write operation.
An options object containing the write operation's session token is ready.
This is required to propagate the session context to the separate client session on the second device.
2
Call ReadItemAsync on the Container instance, passing the documentId, the partition key value wrapped in a PartitionKey object, and the request options.
A point read is performed targeting the correct logical partition with the session token context.
Passing the correct partition key is required for all item operations in Cosmos DB SDK v3, and the session token guarantees read-your-writes consistency.

Key Concept

Session consistency token propagation and point reads using Cosmos DB .NET SDK v3
Rate this question