Question

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

You are developing a C# backend service for a smart home energy monitoring application that stores device configurations in Azure Cosmos DB using the NoSQL API. You need to implement Optimistic Concurrency Control (OCC) using the Azure Cosmos DB .NET SDK v3 to ensure that updates to a device configuration are not overwritten by concurrent processes.

Which sequence of actions should you perform to complete the update?

  1. 1Initialize a CosmosClient instance, and call GetContainer on the target Database object to retrieve a Container reference.
  2. 2Call ReadItemAsync<DeviceConfig> with the configuration ID and a PartitionKey instance to retrieve the current item.
  3. 3Extract the ETag value from the Headers property of the returned ItemResponse<DeviceConfig> object.
  4. 4Instantiate a new ItemRequestOptions object and set its IfMatchEtag property to the extracted ETag value.
  5. 5Call ReplaceItemAsync<DeviceConfig> with the modified object, its ID, its PartitionKey, and the ItemRequestOptions object.

Answer

Initialize the CosmosClient and obtain a Container reference, retrieve the item using ReadItemAsync along with its partition key, retrieve the ETag from the response headers, create an ItemRequestOptions instance with the IfMatchEtag property set to the retrieved ETag, and call ReplaceItemAsync with the updated document, its ID, PartitionKey, and the request options.
To implement Optimistic Concurrency Control (OCC) in Azure Cosmos DB using the C# .NET SDK v3, you must follow a read-before-write pattern. First, retrieve a reference to the container via `CosmosClient` and `Database`. Next, fetch the target document using `ReadItemAsync<T>` specifying the item ID and its `PartitionKey`. You then extract the `ETag` metadata property from the response headers. Next, create a new `ItemRequestOptions` instance and assign the extracted `ETag` string to its `IfMatchEtag` property. Finally, invoke `ReplaceItemAsync<T>` passing the updated object, its ID, its `PartitionKey`, and the custom `ItemRequestOptions`. If another process has modified the document in the meantime, the ETag on the server will not match, and the SDK will throw a `CosmosException` with a `412 Precondition Failed` status code, preventing the overwrite.

Step-by-Step Solution

1
Acquire container reference
A Container instance is obtained using CosmosClient.
An active SDK client and container reference are prerequisites for executing any database operations.
2
Read the item
The existing document is read into memory along with its ETag.
You must obtain the current state of the document and its unique ETag value to perform conditional validation.
3
Read ETag from response headers
The ETag string is extracted.
The Cosmos DB SQL API returns metadata, including ETag, in response headers (ItemResponse.Headers.ETag).
4
Configure Request Options
An ItemRequestOptions object with IfMatchEtag configured.
Setting IfMatchEtag ensures the server validates that the item has not been updated since it was read.
5
Call ReplaceItemAsync
The item is updated in Cosmos DB, or a 412 Precondition Failed status code is thrown if the ETag has changed.
The replacement writes the modifications back to the container under the OCC constraint.

Key Concept

Implementing Optimistic Concurrency Control (OCC) using the Cosmos DB .NET SDK v3 with ETag validation.
Estimated Time:2m 30s
Rate this question