Skip to content

feat(router): dynamic sizing of worker job buffers - #6463

Merged
atzoum merged 2 commits into
masterfrom
feat.dynamicRtWorkerBuffer
Oct 31, 2025
Merged

atzoum merged 2 commits into
masterfrom
feat.dynamicRtWorkerBuffer

Conversation

@atzoum

@atzoum atzoum commented Oct 24, 2025 •

Copy link
Copy Markdown
Contributor

🔒 Scanned for secrets using gitleaks 8.28.0

Description

This update introduces dynamic buffer sizing for job queues within router worker channels. Previously, the buffer capacity was fixed using the noOfJobsPerChannel configuration. With this change, the effective buffer capacity can now grow or shrink (up to maxNoOfJobsPerChannel, default: 10,000) based on the following calculation strategies:

1. Standard (default)

Buffer capacity is set to:

max(noOfJobsPerChannel, noOfJobsToBatchInAWorker)

noOfJobsToBatchInAWorker remains a reloadable value.

2. Experimental (enabled through enableExperimentalBufferSizeCalculator)

The buffer capacity is determined using the following metrics:

  1. Work loop throughput — average number of jobs processed per second by the worker's loop (workLoopThroughput).
  2. Query batch size per worker — calculated as jobQueryBatchSize / noOfWorkers — note that jobQueryBatchSize can change dynamically if pickup throttling is enabled.
  3. Configured batch size — noOfJobsToBatchInAWorker.

The maximum of the three values above is selected and then multiplied by a scaling factor of 2.0 to compute the dynamic buffer size.

Special Case:
If workLoopThroughput < 1, the buffer size is forced to 1 to intentionally start small and apply backpressure in scenarios of low processing throughput.


router 101-Router throughput drawio (2)

Additional Changes

  • During router shutdown, workers no longer wait to drain their job buffers. They exit early instead. Any in-progress jobs are marked as failed by the router on the next startup.
  • New worker statistics are now recorded:
    1. buffer capacity: router_worker_buffer_capacity
    2. buffer size: router_worker_buffer_size
    3. average work-loop throughput: router_worker_work_loop_throughput

Linear Ticket

resolves PIPE-2500

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

@atzoum
atzoum force-pushed the feat.dynamicRtWorkerBuffer branch 3 times, most recently from e1be1dd to 8204640 Compare October 24, 2025 10:19
@codecov

codecov Bot commented Oct 24, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59459% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.26%. Comparing base (8634c3f) to head (e332a55).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
router/worker_buffer.go 93.84% 2 Missing and 2 partials ⚠️
router/worker_buffer_calculator.go 93.10% 4 Missing ⚠️
router/worker_batch_loop.go 83.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6463      +/-   ##
==========================================
+ Coverage   79.25%   79.26%   +0.01%     
==========================================
  Files         529      530       +1     
  Lines       59365    59514     +149     
==========================================
+ Hits        47049    47175     +126     
- Misses       9495     9514      +19     
- Partials     2821     2825       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@atzoum
atzoum force-pushed the feat.dynamicRtWorkerBuffer branch 4 times, most recently from cbd3e0d to bfa6256 Compare October 24, 2025 15:07
doProcessRouterJobs() // process any remaining jobs in the batch
return // input channel is closed, exit the loop
}
if wl.ctx.Err() != 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.

Note: this is to allow router to stop faster and not hinder graceful shutdowns or workspace partition migrations

@atzoum
atzoum force-pushed the feat.dynamicRtWorkerBuffer branch from 2cd75e1 to 4baeda2 Compare October 25, 2025 10:03
@atzoum atzoum changed the title feat(router): dynamic worker job buffers feat(router): dynamic sizing of worker job buffers Oct 25, 2025
@atzoum
atzoum force-pushed the feat.dynamicRtWorkerBuffer branch from 8bc2224 to 20381e0 Compare October 27, 2025 09:41
@atzoum
atzoum marked this pull request as ready for review October 27, 2025 10:45
@atzoum
atzoum requested a review from krishna2020 October 27, 2025 10:45
@atzoum
atzoum force-pushed the feat.dynamicRtWorkerBuffer branch from 20381e0 to 94c1437 Compare October 28, 2025 12:02
🔒 Scanned for secrets using gitleaks 8.28.0
@atzoum
atzoum force-pushed the feat.dynamicRtWorkerBuffer branch from 6a080e4 to 003e52e Compare October 29, 2025 13:08
return func() int {
const minBufferSize = 1
m1 := workLoopThroughput.Load() // at least the average throughput of the work loop
if m1 < 1 { // if there is no throughput yet, the throughput is less than 1 per second, set buffer to minBufferSize

@mihir20 mihir20 Oct 30, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if we restart the server workLoopThroughput would be 0, would it cause slow processing issues initially?

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.

yes, this is intentional

start small and apply backpressure in scenarios of low processing throughput

Comment on lines +57 to +63
return int(
math.Ceil( // round up
// calculate the maximum of the three metrics to determine the buffer size
math.Max(
math.Max(math.Max(m1, m2), m3)*scalingFactor.Load(), // scale up to provide some buffer
minBufferSize, // ensure buffer size is at least minBufferSize
)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

in simple terms, if destination+dt is processing/s is faster we will give dest more jobs to process, but in case it it slow we we will still try to give atleast 2*max(noOfJobsToBatchInAWorker, jobQueryBatchSize/noOfWorkers)

@atzoum atzoum Oct 30, 2025 •

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.

exactly... until the processing throughput drops lower than 1 job/sec which will cause buffer to shrink to 1

@atzoum
atzoum requested a review from mihir20 October 31, 2025 06:29

@mihir20 mihir20 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Comment thread router/worker_batch_loop.go Resolved
🔒 Scanned for secrets using gitleaks 8.28.0
@atzoum
atzoum merged commit 5b2e519 into master Oct 31, 2025
@atzoum
atzoum deleted the feat.dynamicRtWorkerBuffer branch October 31, 2025 12:39
achettyiitr pushed a commit that referenced this pull request Nov 5, 2025
🔒 Scanned for secrets using gitleaks 8.28.0

# Description

This update introduces **dynamic buffer sizing** for job queues within
router worker channels. Previously, the buffer capacity was fixed using
the `noOfJobsPerChannel` configuration. With this change, the effective
buffer capacity can now grow or shrink (up to `maxNoOfJobsPerChannel`,
default: 10,000) based on the following calculation strategies:

### **1. Standard** (default)
Buffer capacity is set to:

`max(noOfJobsPerChannel, noOfJobsToBatchInAWorker)`

> `noOfJobsToBatchInAWorker` remains a reloadable value.

### **2. Experimental** (enabled through
`enableExperimentalBufferSizeCalculator`)
The buffer capacity is determined using the following metrics:

1. **Work loop throughput** — average number of jobs processed per
second by the worker's loop (`workLoopThroughput`).
2. **Query batch size per worker** — calculated as `jobQueryBatchSize /
noOfWorkers` — note that `jobQueryBatchSize` can change dynamically if
pickup throttling is enabled.
3. **Configured batch size** — `noOfJobsToBatchInAWorker`.

The **maximum** of the three values above is selected and then
**multiplied by a scaling factor of 2.0** to compute the dynamic buffer
size.

**Special Case:**  
If `workLoopThroughput < 1`, the buffer size is forced to `1` to
intentionally start small and apply backpressure in scenarios of low
processing throughput.

---

<img width="1742" height="753" alt="router 101-Router throughput drawio
(2)"
src="https://github.com/user-attachments/assets/499cb02a-96e0-416c-a09f-d84dfea087fc"
/>

## Additional Changes

- During router shutdown, workers no longer wait to drain their job
buffers. They exit early instead. Any in-progress jobs are marked as
failed by the router on the next startup.
- New worker statistics are now recorded:
  1. buffer capacity: `router_worker_buffer_capacity `
  2. buffer size: `router_worker_buffer_size `
  3. average work-loop throughput: `router_worker_work_loop_throughput `


## Linear Ticket

resolves PIPE-2500

## Security

- [x] The code changed/added as part of this pull request won't create
any security issues with how the software is being used.
This was referenced Nov 11, 2025
itsmihir pushed a commit that referenced this pull request Nov 11, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.63.0-rc.1](v1.62.0...v1.63.0-rc.1)
(2025-11-11)


### Features

* **router:** dynamic sizing of worker job buffers
([#6463](#6463))
([5b2e519](5b2e519))


### Bug Fixes

* max procs defaults
([#6473](#6473))
([48f9cfd](48f9cfd))
* max procs defaults
([#6473](#6473))
([3d2026c](3d2026c))
* pending batch router jobs for disabled destinations are not being
aborted
([#6477](#6477))
([8effa78](8effa78))
* remove source labels in repo query stats
([#6483](#6483))
([48f9cfd](48f9cfd))
* use type field from payload to check if event is blocked
([#6487](#6487))
([5ba053f](5ba053f))


### Miscellaneous

* apply security best practices from step security
([#6462](#6462))
([a860128](a860128))
* emit filtered metrics from all non-ut stages and clean up diff metrics
([#6485](#6485))
([399c4c6](399c4c6))
* refactor processor to move archival in a preprocess stage
([#6470](#6470))
([8634c3f](8634c3f))
* remove oauth status toggling
([#6492](#6492))
([45e5dba](45e5dba))
* sync release v1.62.0 to main branch
([#6472](#6472))
([65db4ee](65db4ee))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
itsmihir pushed a commit that referenced this pull request Nov 12, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.63.0](v1.62.0...v1.63.0)
(2025-11-11)


### Features

* **router:** dynamic sizing of worker job buffers
([#6463](#6463))
([5b2e519](5b2e519))


### Bug Fixes

* max procs defaults
([#6473](#6473))
([48f9cfd](48f9cfd))
* max procs defaults
([#6473](#6473))
([3d2026c](3d2026c))
* pending batch router jobs for disabled destinations are not being
aborted
([#6477](#6477))
([8effa78](8effa78))
* remove source labels in repo query stats
([#6483](#6483))
([48f9cfd](48f9cfd))
* use type field from payload to check if event is blocked
([#6487](#6487))
([5ba053f](5ba053f))


### Miscellaneous

* apply security best practices from step security
([#6462](#6462))
([a860128](a860128))
* emit filtered metrics from all non-ut stages and clean up diff metrics
([#6485](#6485))
([399c4c6](399c4c6))
* refactor processor to move archival in a preprocess stage
([#6470](#6470))
([8634c3f](8634c3f))
* remove oauth status toggling
([#6492](#6492))
([45e5dba](45e5dba))
* sync release v1.62.0 to main branch
([#6472](#6472))
([65db4ee](65db4ee))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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.

3 participants