Soru

Zorluk: Çok zorPerform Container and Item Operations in Azure Cosmos DB using SDK

You are developing a C# application using the Azure Cosmos DB .NET SDK v3 to manage document collaboration sessions. The container is configured with a partition key path of `/documentId`. The Azure Cosmos DB account is configured with the default consistency level of Session.

An update to a document is processed by Web App Instance A using the following code:
csharp
ItemResponse<Document> writeResponse = await container.ReplaceItemAsync<Document>(
updatedDocument,
updatedDocument.Id,
new PartitionKey(updatedDocument.DocumentId)
);
string token = writeResponse.Headers.Session;

Immediately after the update, Web App Instance B (which runs on a separate virtual machine and utilizes a different `CosmosClient` instance) needs to read the same document to display the latest updates to the same user.

Which C# code segment should you execute on Web App Instance B to guarantee a read-your-writes consistency guarantee for this operation?

  1. A
    ItemRequestOptions options = new ItemRequestOptions
    {
    ConsistencyLevel = ConsistencyLevel.Session
    };
    ItemResponse<Document> readResponse = await container.ReadItemAsync<Document>(
    documentId,
    new PartitionKey(documentId),
    options
    );
  2. B
    ItemResponse<Document> readResponse = await container.ReadItemAsync<Document>(
    documentId,
    new PartitionKey(documentId)
    );
  3. ItemRequestOptions options = new ItemRequestOptions
    {
    SessionToken = token
    };
    ItemResponse<Document> readResponse = await container.ReadItemAsync<Document>(
    documentId,
    new PartitionKey(documentId),
    options
    );
    Cevap
  4. D
    ItemRequestOptions options = new ItemRequestOptions
    {
    SessionToken = token
    };
    ItemResponse<Document> readResponse = await container.ReadItemAsync<Document>(
    documentId,
    new PartitionKey(userStatus),
    options
    );

Cevap

Configure ItemRequestOptions with the SessionToken property set to the session token retrieved from the write response, and pass this along with the document ID and partition key to the ReadItemAsync call.
The correct option explicitly passes the session token from the write operation of Web App Instance A to the read operation on Web App Instance B, establishing session consistency across different client instances.

Adım Adım Çözüm

1
Retrieve the session token from the header of the write response on Web App Instance A.
The token is stored in a variable, enabling it to be sent to another client session.
Since session consistency is client-scoped, the session token is required to extend consistency guarantees across distinct CosmosClient instances.
2
Construct ItemRequestOptions on Web App Instance B and assign the session token to the SessionToken property.
An options object containing the correct session token is initialized.
This tells the SDK to execute the read request with the context of the write session token.
3
Call ReadItemAsync passing the document ID, the correct partition key (documentId), and the request options.
The read operation executes successfully, returning the updated document.
Passing the correct partition key is mandatory for item operations in Cosmos DB, and the request options apply the session token context.

Anahtar Kavram

Azure Cosmos DB Session Consistency across distinct client instances
Bu soruyu puanla