Question

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

You are developing a C# service that processes patient health telemetry using the Azure Cosmos DB .NET SDK v3. The container's partition key is `/patientId`, and the database is configured with Session consistency.

You need to write code to create a patient profile and their first telemetry entry atomically in a single transaction. Then, a separate background processing service (instantiated as a different CosmosClient) must read the newly created profile with guaranteed read-your-writes consistency.

Which two of the following code segments must you implement to achieve this?

  1. TransactionalBatchResponse response = await container.CreateTransactionalBatch(new PartitionKey(patientId))
    .CreateItem<PatientProfile>(profile)
    .CreateItem<TelemetryLog>(log)
    .ExecuteAsync();
    Answer
  2. string sessionToken = response.Headers.Session;
    ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
    ItemResponse<PatientProfile> readResponse = await backgroundContainer.ReadItemAsync<PatientProfile>(
    profile.Id,
    new PartitionKey(patientId),
    options
    );
    Answer
  3. C
    TransactionalBatchResponse response = await container.CreateTransactionalBatch(new PartitionKey(profile.HospitalRegion))
    .CreateItem<PatientProfile>(profile)
    .CreateItem<TelemetryLog>(log)
    .ExecuteAsync();
  4. D
    ItemResponse<PatientProfile> readResponse = await backgroundContainer.ReadItemAsync<PatientProfile>(
    profile.Id,
    new PartitionKey(patientId)
    );

Answer

To perform the atomic transaction and read the written data with read-your-writes guarantees across different client sessions, you must create a transactional batch using the patient's partition key and then capture and pass the session token to the reader client.
The transactional batch must be created using the partition key (patientId) on the container instance. To guarantee read-your-writes consistency across separate CosmosClient sessions, the session token must be explicitly retrieved from the write response headers and passed in the ItemRequestOptions of the read request.

Step-by-Step Solution

1
Create and execute the transactional batch using the container instance.
Both the patient profile and telemetry log are written atomically using the patientId partition key.
Transactional batching in SDK v3 is container-scoped and requires all operations in the batch to share the same partition key.
2
Extract the Session token from the transactional batch response.
The session token string is retrieved from response.Headers.Session.
The write operation generates a session token that represents the state of the database after the transaction.
3
Pass the session token in the ItemRequestOptions to the background reader client.
The background client reads the item using the session token, ensuring it sees the writes.
Since the background reader uses a different client instance, Session consistency guarantees are only maintained if the session token is explicitly shared.

Key Concept

Cosmos DB Transactional Batching and Session Consistency Sharing
Rate this question