Question

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

You are developing a C# backend service for a multi-tenant logistics application using the Azure Cosmos DB .NET SDK v3. The database contains a Shipments container configured with a partition key path of `/tenantId`.

You need to implement a method to update a shipment's delivery status with the lowest latency and cost. The method must prevent dirty writes if another thread or client instance updates the shipment concurrently.

Which of the following C# code segments should you implement to perform this update operation?

  1. ItemResponse<Shipment> response = await container.ReplaceItemAsync<Shipment>(
    shipment,
    shipment.Id,
    new PartitionKey(shipment.TenantId),
    new ItemRequestOptions { IfMatchEtag = shipment.ETag }
    );
    Answer
  2. B
    ItemResponse<Shipment> response = await container.ReplaceItemAsync<Shipment>(
    shipment,
    shipment.Id,
    new PartitionKey(shipment.Status),
    new ItemRequestOptions { IfMatchEtag = shipment.ETag }
    );
  3. C
    ItemResponse<Shipment> response = await container.ReplaceItemAsync<Shipment>(
    shipment,
    shipment.Id,
    new PartitionKey(shipment.TenantId),
    new ItemRequestOptions { SessionToken = shipment.SessionToken }
    );
  4. D
    ItemResponse<Shipment> response = await container.ReplaceItemAsync<Shipment>(
    shipment,
    shipment.Id,
    requestOptions: new ItemRequestOptions { IfMatchEtag = shipment.ETag }
    );

Answer

The correct code segment calls ReplaceItemAsync using the shipment's TenantId as the partition key and sets the IfMatchEtag property in ItemRequestOptions to the shipment's ETag.
The correct code segment uses the Azure Cosmos DB .NET SDK v3 `ReplaceItemAsync` method to safely update the shipment. It targets the correct logical partition by passing the tenant ID as the partition key (`new PartitionKey(shipment.TenantId)`), which matches the container partition key path of `/tenantId`. It prevents dirty writes by passing the current ETag of the shipment object through the `IfMatchEtag` property in `ItemRequestOptions`. If another thread has modified the document, its ETag will have changed, causing the update to fail with a `PreconditionFailed` status code, achieving Optimistic Concurrency Control.

Step-by-Step Solution

1
Determine the correct partition key based on the container configuration.
The container uses `/tenantId` as the partition key path, meaning every point operation must supply the specific tenant ID value via `new PartitionKey(shipment.TenantId)` to resolve the correct logical partition.
Cosmos DB requires the partition key to perform single-partition point operations.
2
Select the proper concurrency control mechanism.
To prevent dirty writes concurrently, configure Optimistic Concurrency Control (OCC) using the item's ETag value.
ETag matching ensures that the write operation fails if the item has been modified by another client session or thread since it was last read.
3
Construct the SDK method call with required parameters.
Use `container.ReplaceItemAsync<T>` passing the item, its ID, the partition key, and the `ItemRequestOptions` containing `IfMatchEtag`.
This aligns with the .NET SDK v3 signature requirements for updating an existing document safely.

Key Concept

Performing safe point updates using Optimistic Concurrency Control and the correct partition key in Azure Cosmos DB .NET SDK v3.
Estimated Time:2m 30s
Rate this question