Cosmos DBでRUを食いすぎて困った時の備忘録です。
Cosmos DBでデータ操作を行った時のレスポンスにはrequestChargeプロパティが含まれています。
これが処理を行うにあたって必要だったRUです。
どのデータを突っ込んだときにどれだけのRUを食っているのか実際に測定して戦略を立てるのが良いです。
import { CosmosClient } from '@azure/cosmos'
const client = new CosmosClient({
endpoint: host,
key: key,
})
const database = await client.database(databaseId)
const container = await database.container(containerId)
const { resource, requestCharge } = await containers[containerName].items.upsert(item)
logger.info(`[Request Charge]:${requestCharge}`)
リトライ設定を行うことで、RUがパンクしたときにリトライすることができます。
const client = new CosmosClient({
endpoint: host,
key: key,
connectionPolicy: {
retryOptions: {
maxRetryAttemptCount: 9, // 何回リトライするか。デフォルトは9回
fixedRetryIntervalInMilliseconds: 1000, // リトライのインターバル
maxWaitTimeInSeconds: 30, // リトライが発生してから何秒待つか
}
}
})