Soru

Zorluk: OrtaPerform Container and Item Operations in Azure Cosmos DB using SDK

You are developing a C# backend service that manages user profiles in a multi-tenant SaaS application. User profile items are stored in an Azure Cosmos DB container with the partition key path `/tenantId`. The Azure Cosmos DB account is configured to use Session consistency.

A user updates their profile through one instance of the service, which returns a session token. A separate service instance must immediately read this updated profile. To guarantee read-your-writes consistency across the separate service instances with the lowest latency and RU cost, you must perform a point read using the Cosmos DB .NET SDK v3.

Which C# code snippet should you use?

  1. csharp
    ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
    ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(
    id: "user-99",
    partitionKey: new PartitionKey("tenant-abc"),
    requestOptions: options
    );
    Cevap
  2. B
    csharp
    ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(
    id: "user-99",
    partitionKey: new PartitionKey("tenant-abc")
    );
  3. C
    csharp
    ItemRequestOptions options = new ItemRequestOptions { SessionToken = sessionToken };
    ItemResponse<UserProfile> response = await container.ReadItemAsync<UserProfile>(
    id: "user-99",
    partitionKey: new PartitionKey("active"),
    requestOptions: options
    );
  4. D
    csharp
    FeedIterator<UserProfile> iterator = container.GetItemQueryIterator<UserProfile>(
    "SELECT * FROM c WHERE c.id = 'user-99'"
    );

Cevap

The correct code snippet performs a point read using ReadItemAsync, passes the tenant ID as a PartitionKey object, and includes the session token within ItemRequestOptions.
The correct snippet uses ReadItemAsync to perform a point read (the lowest latency and cost operation for single item retrieval). By passing the partition key as new PartitionKey("tenant-abc") and setting the SessionToken property in ItemRequestOptions, it successfully guarantees read-your-writes consistency across separate service instances.

Adım Adım Çözüm

1
Select the correct SDK method for a low-cost, low-latency single item retrieval.
ReadItemAsync is selected over GetItemQueryIterator because point reads are faster and cost fewer RUs (1 RU for items <= 1KB).
Point reads are the most efficient read operations in Azure Cosmos DB.
2
Ensure the partition key parameter is supplied correctly using the Cosmos DB .NET SDK v3 types.
Use 'new PartitionKey("tenant-abc")' as the partitionKey parameter.
Item operations in SDK v3 require a PartitionKey struct parameter containing the partition key value of the targeted document.
3
Configure the session token to maintain read-your-writes consistency across different client sessions.
Instantiate ItemRequestOptions, set the SessionToken property, and pass it as the requestOptions parameter.
Session consistency guarantees read-your-writes only within the same client session unless the session token is explicitly shared and passed to separate clients.

Anahtar Kavram

Performing point reads with session tokens using the Azure Cosmos DB .NET SDK v3 to guarantee read-your-writes consistency across clients.
Tahmini Süre:1m 30s
Bu soruyu puanla