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?
- 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")
); - // 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 }
);Cevap - 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 }
); - 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") }
);
Cevap
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.
Adım Adım Çözüm
Anahtar Kavram
Managing Session consistency scope across multiple SDK client instances using session tokens.