Skip to content

Update Rate Limiter for BatchWrite to Reduce Resource/Write Capacity Exhaustion - #20

Merged
ohadkatz merged 1 commit into
mainfrom
okatz/fix-batchwrite-rate-limiter
Jul 24, 2026
Merged

Update Rate Limiter for BatchWrite to Reduce Resource/Write Capacity Exhaustion #20
ohadkatz merged 1 commit into
mainfrom
okatz/fix-batchwrite-rate-limiter

Conversation

@ohadkatz

@ohadkatz ohadkatz commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

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

Comment thread table.go
if reserve <= 0 {
reserve = int(writeBatchSize)
}
if reserve > rl.Burst() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread table.go
if reserve > rl.Burst() {
reserve = rl.Burst()
}
time.Sleep(rl.ReserveN(time.Now(), reserve).Delay())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Token bucket mechanism: Sleep until token bucket is filled up

Comment thread table.go
&dynamodb.BatchWriteItemInput{
RequestItems: batch,
RequestItems: batch,
ReturnConsumedCapacity: aws.String(dynamodb.ReturnConsumedCapacityTotal),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReturnedConsumedCapacity was never added, so we always assumed maximum capacity

@ohadkatz ohadkatz changed the title Fix inert rate limiter in writeBatch Update Rate Limiter for BatchWrite to Reduce Resource/Write Capacity Exhaustion Jul 23, 2026
Comment thread table.go
reqCapacity = 0
for _, each := range consumedCapacity {
reqCapacity += *each.CapacityUnits
if each != nil && each.CapacityUnits != nil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
ohadkatz force-pushed the okatz/fix-batchwrite-rate-limiter branch from 78e98a1 to 1419caa Compare July 23, 2026 15:57
@ohadkatz
ohadkatz requested a review from chris-regnier July 24, 2026 20:08

@chris-regnier chris-regnier left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I appreciate the inline annotations and links to documentation.

@ohadkatz
ohadkatz merged commit a6487b1 into main Jul 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants