Update Rate Limiter for BatchWrite to Reduce Resource/Write Capacity Exhaustion - #20
Merged
Merged
Conversation
ohadkatz
commented
Jul 23, 2026
| if reserve <= 0 { | ||
| reserve = int(writeBatchSize) | ||
| } | ||
| if reserve > rl.Burst() { |
Contributor
Author
There was a problem hiding this comment.
ohadkatz
commented
Jul 23, 2026
| if reserve > rl.Burst() { | ||
| reserve = rl.Burst() | ||
| } | ||
| time.Sleep(rl.ReserveN(time.Now(), reserve).Delay()) |
Contributor
Author
There was a problem hiding this comment.
Token bucket mechanism: Sleep until token bucket is filled up
ohadkatz
commented
Jul 23, 2026
| &dynamodb.BatchWriteItemInput{ | ||
| RequestItems: batch, | ||
| RequestItems: batch, | ||
| ReturnConsumedCapacity: aws.String(dynamodb.ReturnConsumedCapacityTotal), |
Contributor
Author
There was a problem hiding this comment.
ReturnedConsumedCapacity was never added, so we always assumed maximum capacity
ohadkatz
commented
Jul 23, 2026
| reqCapacity = 0 | ||
| for _, each := range consumedCapacity { | ||
| reqCapacity += *each.CapacityUnits | ||
| if each != nil && each.CapacityUnits != nil { |
Contributor
Author
There was a problem hiding this comment.
Check for null values before adding to capacity bucket
write_qps was never enforced: BatchWriteItem was called without ReturnConsumedCapacity, so ConsumedCapacity came back empty, the reservation was always zero, and the sync consumed all provisioned write capacity. Now we request consumed capacity, reserve it before each write (capped at the limiter's burst), and drain the batch on success so the loop exits. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
ohadkatz
force-pushed
the
okatz/fix-batchwrite-rate-limiter
branch
from
July 23, 2026 15:57
78e98a1 to
1419caa
Compare
chris-regnier
approved these changes
Jul 24, 2026
chris-regnier
left a comment
There was a problem hiding this comment.
This looks good to me. I appreciate the inline annotations and links to documentation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Update our Batch Write system in the interim to improve and reduce the amount of write QPS that causes the WCU pool to throttle other developer workflows. I have identified the write QPS method used in this tool as a first step to helping reduce throttling by improving the way we handle rate limiting, especially on retries, and to help improve logging.
It seems the write_qps was never enforced because consumed capacity was never requested. Below is a deep dive to explain the changes made