Skip to content

feat(pubsub): implement publish hedging to reduce tail latency - #13735

Open
tonyyyycui wants to merge 24 commits into
googleapis:mainfrom
tonyyyycui:publish-hedging-settings
Open

feat(pubsub): implement publish hedging to reduce tail latency#13735
tonyyyycui wants to merge 24 commits into
googleapis:mainfrom
tonyyyycui:publish-hedging-settings

Conversation

@tonyyyycui

@tonyyyycui tonyyyycui commented Jul 13, 2026

Copy link
Copy Markdown

This PR implements publish hedging in the Java Cloud Pub/Sub Publisher. Specifically, it adds support for scheduling "hedged" publish attempts when a publish call is slow to respond. A token-based method is utilized to rate-limit hedged requests and a coordinator is used to manage/cancel concurrent requests to prevent duplicate publishes.

@tonyyyycui
tonyyyycui requested review from a team as code owners July 13, 2026 17:31

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a hedging mechanism for the Pub/Sub Publisher, adding HedgeSettings for configuration and a thread-safe HedgeTokenBucket to limit hedged requests. The feedback suggests replacing the synchronized methods in HedgeTokenBucket with explicit ReentrantLock to reduce lock contention and improve performance on the critical path.

@tonyyyycui tonyyyycui changed the title Add HedgeSettings and integrate with publisher for token operations feat(pubsub): add HedgeSettings and integrate with Publisher Jul 13, 2026
@tonyyyycui tonyyyycui changed the title feat(pubsub): add HedgeSettings and integrate with Publisher feat(pubsub): implement HedgeSettings and integrate with Publisher Jul 13, 2026

@michaelpri10 michaelpri10 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.

At a higher level, I think the full implementation should be one PR (instead of the , given any submitted changes become public. It would be unexpected for customers to be able to set HedgingSettings without anything actually happening, so the final PR should have everything needed for the implementation included.

*
* @return the hedging delay.
*/
public Duration getHedgeDelay() {

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.

nit: Does this method need to be public? I think leaving it without an access modifier (i.e., making it package-private) should be sufficient.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've removed it for now, but I think it would also make sense if the user could see what they see the hedge delay is set to since it's a configurable field. Leaving this conversation unresolved.

@tonyyyycui tonyyyycui changed the title feat(pubsub): implement HedgeSettings and integrate with Publisher feat(pubsub): implement publish hedging to reduce tail latency Jul 20, 2026
final int batchSize = outstandingBatch.outstandingPublishes.size();
for (final OutstandingPublish outstanding : outstandingBatch.outstandingPublishes) {
outstanding.publishResult.addListener(
new Runnable() {

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.

I worried that this approach could have cause additional, unneeded overhead. The cancellation check be done in processQueue instead to avoid needing to create these Runnables, but then we run into an issue of the cancellation not propagating to in-flight hedged requests (i.e., publish at t=0, hedge at t=1000, cancellation at t=1050). This may be okay behavior given that cancellations are best effort. I'll raise this issue with the team.

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