perf: use power-of-two random choices for channel selection to avoid thundering herd#235
Merged
perf: use power-of-two random choices for channel selection to avoid thundering herd#235
Conversation
sakthivelmanii
approved these changes
Apr 10, 2026
Collaborator
Author
|
cc: @kinsaurralde |
a8c31c2 to
852834c
Compare
852834c to
8fcd27d
Compare
sakthivelmanii
approved these changes
Apr 10, 2026
olavloite
reviewed
Apr 10, 2026
| private Duration scaleDownInterval = Duration.ZERO; | ||
| private boolean isDynamicScalingEnabled = false; | ||
| private int maxConcurrentStreamsLowWatermark = DEFAULT_MAX_STREAM; | ||
| private GcpManagedChannelOptions.ChannelPickStrategy channelPickStrategy = |
Collaborator
There was a problem hiding this comment.
For a follow-up PR: This field (and probably most of the other fields here) can be made final.
Collaborator
Author
There was a problem hiding this comment.
Agreed, will address in a follow-up. Most of these fields are set once in initOptions() during construction and never mutated after.
grpc-gcp/src/main/java/com/google/cloud/grpc/GcpManagedChannel.java
Outdated
Show resolved
Hide resolved
grpc-gcp/src/main/java/com/google/cloud/grpc/GcpManagedChannel.java
Outdated
Show resolved
Hide resolved
grpc-gcp/src/test/java/com/google/cloud/grpc/GcpManagedChannelTest.java
Outdated
Show resolved
Hide resolved
grpc-gcp/src/test/java/com/google/cloud/grpc/GcpManagedChannelTest.java
Outdated
Show resolved
Hide resolved
olavloite
reviewed
Apr 10, 2026
olavloite
approved these changes
Apr 10, 2026
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.
Summary
Shared background executor
ScheduledExecutorServicewith a single staticScheduledThreadPoolExecutor(SHARED_BACKGROUND_SERVICE) shared across allGcpManagedChannelinstances in the process.ScheduledFuture<?>handles (cleanupTask,scaleDownTask,logMetricsTask) and cancels them onshutdown()/shutdownNow()instead of terminating a dedicated thread pool.removeOnCancelPolicy(true)ensures cancelled tasks are immediately purged from the work queue.max(2, min(4, availableProcessors / 2)).Power-of-two channel selection (thundering herd fix)
pickLeastBusyChannelused a deterministic linear scan that always picked the first channel on tie. Under burst traffic — especially at startup or after idle periods when stream counts are equal — all concurrent callers see the same counts before any are incremented (TOCTOU race), causing all requests to pile onto channel 0. In steady-state with smooth traffic the existing scan distributes fine; the issue is acute during bursts when channels have equalized.lastResponseNanosto preserve connection warmth.ChannelPickStrategy.LINEAR_SCANviaGcpChannelPoolOptions.setChannelPickStrategy(). The new default isPOWER_OF_TWO.Design decisions
POWER_OF_TWOorLINEAR_SCAN).fallbackMapandDEFAULT_MAX_STREAM, which requires a full scan.POWER_OF_TWOusesgetMaxActiveStreams()(the global maximum across all channels) — if ANY channel hits the watermark, we scale up before other channels follow. This is more aggressive than using the global minimum or sampled minimum, butmaxSizeguards against over-provisioning. The global min would delay scale-up; the sampled min would be noisy.totalActiveStreams), which is distribution-agnostic.lastResponseNanos) naturally concentrates traffic on active channels under low load without any threshold or mode-switch logic.