當多個工作負載共用相同的 Azure Cosmos DB 容器時,資源爭用可能會導致節流、增加延遲,以及潛在的業務影響。 吞吐量桶幫助你管理這些工作負載的資源使用,而不會為任何桶保留吞吐量。 您可以以容器總吞吐量的百分比來限制工作負載的最大吞吐量消耗。
常見使用案例
- 由獨立軟體供應商(ISV)管理的多租戶工作負載。
- ETL 作業中的批量執行。
- 資料擷取與遷移流程。
- 變更摘要處理。
吞吐量桶的運作方式
吞吐量桶透過限制桶可消耗的最大吞吐量,協助管理共用 Azure Cosmos DB 容器的工作負載資源消耗。 然而,吞吐量並不預留給任何貯體。 它會在所有工作負載間共享。
- 每個貯體都有最大輸送量百分比,限制了它可取用的容器總輸送量比例。
- 指派給貯體的要求最多只能取用此限制的輸送量。
- 如果貯體超過其設定的限制,則會對後續要求進行節流。
- 此機制有助於防止資源爭用,確保沒有任何單一工作負載耗用過多的輸送量並影響其他人。
Note
沒有明確桶的請求會使用容器的預設吞吐量桶(如果有設定的話)。 否則,它們會從容器中消耗吞吐量,且沒有桶特定限制。
設定吞吐量桶
若要在 Azure 入口網站中設定吞吐量儲存槽:
- 開啟您的 Azure Cosmos DB 帳戶並選擇 Data Explorer。
- 選取您的容器並開啟 「縮放與設定」。
- 選取「吞吐量桶」索引標籤。
- 將所需的吞吐量桶從 「非活動 」變更為 「活動」。
- 為每個啟用的桶設定最大吞吐量百分比,然後儲存設定。 每個容器集最多可設定五個桶。
設定預設吞吐量桶
你可以選擇在 Azure 入口網站中設定 Cosmos DB 容器的預設吞吐量桶,方法是將其中一個啟用的吞吐量桶設定為預設。 當請求未指定桶時,若已設定為預設吞吐量桶,該請求會自動指派到該桶。 如果設定為預設的桶後來被停用,未指派的請求會改用容器的吞吐量,而不再使用其他桶。
最低SDK版本需求
本文所述情境請使用以下版本或更新版本的 SDK:
在 SDK 請求中使用吞吐量桶
使用您的 SDK 的吞吐量桶選項將請求指派到某個桶。 以下範例顯示用戶端層級與請求層級的設定。
若要將要求指派給特定貯體,請使用 SDK 中的 RequestOptions。
using Microsoft.Azure.Cosmos;
string itemId = "<id>";
PartitionKey partitionKey = new PartitionKey("<pkey>");
// Define request options with Throughput Bucket 1 for read operation
ItemRequestOptions readRequestOptions = new ItemRequestOptions{ThroughputBucket = 1};
// Send read request using Bucket ID 1
ItemResponse<Product> readResponse = await container.ReadItemAsync<Product>(
itemId,
partitionKey,
readRequestOptions);
Product product = readResponse.Resource;
// Define request options with Throughput Bucket 2 for update operation
ItemRequestOptions updateRequestOptions = new ItemRequestOptions{ThroughputBucket = 2};
// Send update request using Bucket ID 2
ItemResponse<Product> updateResponse = await container.ReplaceItemAsync(
product,
itemId,
partitionKey,
updateRequestOptions);
若要將輸送量貯體套用至用戶端應用程式的所有要求,請使用 SDK 中的 ClientOptions。
using Microsoft.Azure.Cosmos;
using Azure.Identity;
var credential = new DefaultAzureCredential();
// Create CosmosClient with Bulk Execution and Throughput Bucket 1
CosmosClient cosmosClient = new CosmosClientBuilder("<endpointUri>", credential)
.WithBulkExecution(true) // Enable bulk execution
.WithThroughputBucket(1) // Assign throughput bucket 1 to all requests
.Build();
// Get container reference
Container container = cosmosClient.GetContainer("<DatabaseId>", "<ContainerId>");
// Prepare bulk operations
List<Task> tasks = new List<Task>();
for (int i = 1; i <= 10; i++)
{
var item = new
{
id = Guid.NewGuid().ToString(),
partitionKey = $"pkey-{i}",
name = $"Item-{i}",
timestamp = DateTime.UtcNow
};
tasks.Add(container.CreateItemAsync(item, new PartitionKey(item.partitionKey)));
}
// Execute bulk insertions with throughput bucket 1
await Task.WhenAll(tasks);
//Read item with throughput bucket 1
ItemResponse<Product> response = await container.ReadItemAsync<Product>(partitionKey: new PartitionKey("pkey1"), id: "id1");
Note
對於批量操作,請將 throughput bucket 設定在用戶端或批次請求層級,而非個別點操作。
資料總管中的貯體行為
- 如果已設定 Bucket 1 ,則來自數據總管的所有要求都會使用此貯體,並遵守其輸送量限制。
- 如果未設定 Bucket 1 ,要求會利用容器的完整可用輸送量。
監視吞吐量 bucket
您可以在 Azure 平台的入口網站中追蹤儲存體使用量。
總請求數(預覽):透過依 ThroughputBucket 分割指標,查看每個桶的請求數。
總請求單位數(預覽版):透過將指標依 ThroughputBucket 分割,監控每個桶的請求單位消耗。
局限性
- 在共用吞吐量資料庫中,容器不受支援。
- 不適用於無伺服器 Cosmos DB 帳戶。