Question

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

You are developing a C# backend service that manages delivery truck configurations using the Azure Cosmos DB .NET SDK v3. The container is configured with Session consistency and uses `/fleetId` as the partition key path.

The application runs on multiple independent Azure App Service instances behind a load balancer. You need to implement a workflow where one instance updates a truck configuration (with `id` of `truck-99` and `fleetId` of `fleet-west`) and another instance immediately reads the updated configuration, guaranteeing a read-your-writes level of consistency.

Which code snippet should you use?

  1. A
    // Instance 1: Update the configuration
    await container.ReplaceItemAsync<TruckConfig>(
    updatedConfig,
    "truck-99",
    new PartitionKey("fleet-west")
    );

    // Instance 2: Read the configuration without passing a session token
    ItemResponse<TruckConfig> readResponse = await container.ReadItemAsync<TruckConfig>(
    "truck-99",
    new PartitionKey("fleet-west")
    );
  2. // Instance 1: Update the configuration and extract the session token
    ItemResponse<TruckConfig> writeResponse = await container.ReplaceItemAsync<TruckConfig>(
    updatedConfig,
    "truck-99",
    new PartitionKey("fleet-west")
    );
    string token = writeResponse.Headers.Session;

    // Instance 2: Read the configuration using the session token
    ItemResponse<TruckConfig> readResponse = await container.ReadItemAsync<TruckConfig>(
    "truck-99",
    new PartitionKey("fleet-west"),
    new ItemRequestOptions { SessionToken = token }
    );
    Answer
  3. C
    // Instance 1: Update the configuration using the status as the partition key
    ItemResponse<TruckConfig> writeResponse = await container.ReplaceItemAsync<TruckConfig>(
    updatedConfig,
    "truck-99",
    new PartitionKey("Active")
    );
    string token = writeResponse.Headers.Session;

    // Instance 2: Read the configuration using the status as the partition key
    ItemResponse<TruckConfig> readResponse = await container.ReadItemAsync<TruckConfig>(
    "truck-99",
    new PartitionKey("Active"),
    new ItemRequestOptions { SessionToken = token }
    );
  4. D
    // Instance 1: Update the configuration and extract the session token
    ItemResponse<TruckConfig> writeResponse = await container.ReplaceItemAsync<TruckConfig>(
    updatedConfig,
    "truck-99",
    new PartitionKey("fleet-west")
    );
    string token = writeResponse.Headers.Session;

    // Instance 2: Attempt to pass the partition key inside the request options
    ItemResponse<TruckConfig> readResponse = await container.ReadItemAsync<TruckConfig>(
    "truck-99",
    null,
    new ItemRequestOptions { SessionToken = token, PartitionKey = new PartitionKey("fleet-west") }
    );

Answer

The code snippet that extracts the session token from the write response headers and passes it in the ItemRequestOptions when performing the read operation.
The correct code snippet retrieves the session token from the write response headers on the first instance and passes it to the read operation on the second instance using the SessionToken property of ItemRequestOptions. This ensures that the second instance reads from a replica that has been updated with the write from the first instance.

Step-by-Step Solution

1
Analyze the consistency requirements
The application uses Session consistency, which guarantees read-your-writes within the same client session. However, because the application runs on multiple independent Azure App Service instances, each instance will have its own CosmosClient and thus a different client session.
Understanding the scope of Session consistency across multiple client sessions is critical to identifying why the session token must be shared.
2
Determine the mechanism to share the session context
To achieve read-your-writes consistency across different client sessions, the session token must be explicitly passed from the writer instance to the reader instance.
Explicitly passing the session token allows the reader client to request data from a replica that has caught up to at least the transaction indicated by the token.
3
Identify the correct SDK v3 methods and parameters
Use ReplaceItemAsync to write the update, extract the session token via writeResponse.Headers.Session, and pass it to ReadItemAsync using ItemRequestOptions. The partition key must be 'fleet-west' as the container is partitioned by '/fleetId'.
This matches the C# SDK v3 syntax and complies with the partition key path requirement.

Key Concept

Managing Session consistency scope across multiple SDK client instances using session tokens.
Rate this question