gpt4 book ai didi

java - DynamoDB 中的 RateLimited 写入

转载 作者:行者123 更新时间:2023-12-01 22:00:28 25 4
gpt4 key购买 nike

有多种方法可以对 DynamoDB 中的扫描操作进行速率限制,例如:https://java.awsblog.com/post/Tx3VAYQIZ3Q0ZVW/Rate-Limited-Scans-in-Amazon-DynamoDB

我想知道是否有一种方法可以在写作时做到这一点。假设我必须更新 Dynamo 中的一个表,其中包含约 200 个条目,并且我想这样做,这样就不会违反我的写入阈值,即每秒 1 个条目。

限速阅读的代码类似于:

    do {
// Let the rate limiter wait until our desired throughput "recharges"
rateLimiter.acquire(permitsToConsume);

// Do the scan
ScanRequest scan = new ScanRequest()
.withTableName("ProductCatalog")
.withLimit(100)
.withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
.withExclusiveStartKey(exclusiveStartKey);
ScanResult result = dynamodb.scan(scan);
exclusiveStartKey = result.getLastEvaluatedKey();

// Account for the rest of the throughput we consumed,
// now that we know how much that scan request cost
double consumedCapacity = result.getConsumedCapacity().getCapacityUnits();
permitsToConsume = (int)(consumedCapacity - 1.0);
if(permitsToConsume <= 0) {
permitsToConsume = 1;
}

// Process results here
processYourResults(result);

} while (exclusiveStartKey != null);

请注意,消耗的许可会根据扫描操作使用的容量进行更新。所以我的问题是如何更新它们以进行写入操作?

此外,如果我知道我的写入操作消耗了 1 个容量,那么在循环中将许可更新 1 是否安全? (这是一个假设)

最佳答案

想到的一个简单选择是在所有写入之前使用 SQS,或者在出现第一个阈值错误时立即使用 SQS 作为“溢出缓冲区”。

然后,您将拥有一个连续运行的后台进程,并以低于最大阈值的预定义速度将记录从 SQS 移动到 DynamoDB。当您的写入量出现峰值但又不想为 dynamodb 的 24x7 更高容量付费时,这种设计模式效果特别好,并且具有成本效益。

这样做的好处是客户端提交记录(写入)时,在等待重试发生时不会被阻塞; SQS 可以以几乎任何速度摄取写入,无需额外费用,即您按消息付费,而不是按消息速率付费。

关于java - DynamoDB 中的 RateLimited 写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33598966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com