[java-pubsub] StreamingPull: new subscribers get zero messages - existing subscribers hoard entire backlog with default flow control
维护者通常 1 天内回复
@michaelpri10 已经在做这个了。
开始于 2026年4月9日。
评估
这个 Issue 还没有评估数据。
描述
Summary
When using StreamingPull with the default flow control settings (1000 outstanding messages), existing subscribers hoard messages in their client buffer and gRPC stream buffer. When new subscribers connect (e.g., via Kubernetes HPA autoscaling), they receive zero messages because all messages are locked inside existing subscribers' memory.
Scaling to zero and scaling back up fixes the problem immediately — all subscribers receive messages evenly. This confirms the issue is with how messages are distributed to existing vs new subscribers.
Environment
- Client library: google-cloud-pubsub (Java)
- Subscription type: StreamingPull
- Infrastructure: GKE (Google Kubernetes Engine) with HPA (Horizontal Pod Autoscaler)
- Processing: Single-threaded message processing (1 executor thread), each message triggers a large file download (~5 MB), so processing is slow (~30 seconds per message)
- Configuration:
parallelPullCount = 2,executorProviderThreadCount = 1, no explicitsetFlowControlSettings()
Problem Description
Setup
- 5 subscriber pods running continuously (HPA min replicas = 5, max = 50)
- Each pod uses StreamingPull with default flow control (1000 messages outstanding)
- Each pod processes 1 message at a time (single threaded)
What happens
- A burst of messages arrives (e.g., 75,000 messages)
- The 5 existing pods receive all messages into their buffers
- HPA detects high lag and scales to 50 pods
- The new 45 pods connect but receive ZERO messages
- Only the original 5 pods process messages
- Lag keeps increasing despite 50 pods running
What fixes it
- Scale to 0 (all pods die, all messages released)
- Scale back to 50
- All 50 pods now receive messages evenly
- Lag drops to 0 within 15-30 minutes
Root Cause Analysis
1. Two buffers inside each subscriber pod
Based on the explanation by a Google engineer in googleapis/python-pubsub#237, there are two separate buffers inside each subscriber:
| Buffer | Location | Size | Ack deadline extended? |
|---|---|---|---|
| Client buffer | Inside the application (RAM) | Controlled by maxOutstandingElementCount (default: 1000) |
Yes - client library extends automatically |
| gRPC stream buffer | Between gRPC layer and application (RAM) | Server sends messages in packets up to 10 MB | No - messages can expire here |
The Google engineer stated:
"Unless the flow control is being applied at the gRPC level, messages will be buffered in the gRPC stream, where more than the specified number of messages can be present due to the Pub/Sub server sending out messages in packets ≤ 10 MiB in size. We currently don't support server-side flow control."
2. Message hoarding calculation
With a message size of ~1 KB:
| What | Calculation | Result |
|---|---|---|
| Messages in one 10 MB gRPC packet | 10,000 KB / 1 KB | ~10,000 messages |
| gRPC buffer per pod (2 streams) | 2 × 10,000 | ~20,000 messages |
| Client buffer per pod (default) | 1000 | 1,000 messages |
| Total per pod | 20,000 + 1,000 | ~21,000 messages |
| Total for 5 pods | 5 × 21,000 | ~105,000 messages |
With a lag of 75,000 messages, all messages fit inside the 5 existing pods' buffers. Zero messages are available for new pods.
3. Server routing favors existing subscribers
From Google's troubleshooting documentation:
"The system dynamically directs more messages towards consumers that demonstrate higher capacity — those that acknowledge messages quickly and are not constrained by their flow control settings."
"Messages could be outstanding to clients already, and a backlog of unacknowledged messages doesn't necessarily mean you'll receive those messages on your next pull request."
Existing pods have ack history and appear to have high capacity (1000 outstanding messages, not constrained by flow control). New pods have zero history. The server keeps routing to existing pods.
4. Why the client buffer is the critical problem
-
Client buffer (1000 messages): Ack deadline is extended automatically by the client library. These messages are permanently locked to the pod until processed. With 1 thread and ~30 seconds per message, processing 1000 messages takes ~8 hours.
-
gRPC buffer (~20,000 messages): Ack deadline is NOT extended. These messages expire and return to the server. However, the server often re-delivers them to the same existing pods (because those pods appear to have higher capacity).
The combination of 1000 permanently-locked messages per pod + server routing preference = new subscribers are starved.
Workaround / Fix
Setting maxOutstandingElementCount to a low value (e.g., 10) significantly improves message distribution:
Subscriber subscriber = Subscriber.newBuilder(subscriptionName, receiver)
.setFlowControlSettings(
FlowControlSettings.newBuilder()
.setMaxOutstandingElementCount(10L)
.setLimitExceededBehavior(FlowController.LimitExceededBehavior.Block)
.build()
)
.build();
With maxOutstandingElementCount = 10:
- Each pod locks only 10 messages (instead of 1000) with extended ack deadlines
- Pods become "constrained by flow control" almost instantly
- Server routing shifts to new pods that are "not constrained"
- New subscribers receive messages
However, the gRPC stream buffer still receives ~10,000 messages in the initial 10 MB packet (since there is no server-side flow control). These messages expire and return to the server, but this creates unnecessary churn.
Feature Request
Server-side flow control for StreamingPull
The Google engineer in #237 stated: "We currently don't support server-side flow control."
If the server respected the client's maxOutstandingElementCount when sending messages (i.e., not sending more than the client's limit), it would:
- Prevent gRPC buffer flooding (no unnecessary 10 MB packets)
- Reduce message expiry and re-delivery churn
- Improve message distribution to new subscribers immediately
- Make Pub/Sub work better with Kubernetes HPA autoscaling
Documentation improvement
The current documentation does not clearly explain:
- The two-buffer model (client buffer + gRPC stream buffer)
- How default flow control (1000 messages) causes message hoarding with slow processors
- The impact on Kubernetes HPA autoscaling (new pods getting zero messages)
- The recommended flow control settings for slow-processing subscribers
Adding a section to the StreamingPull documentation or troubleshooting guide about this scenario would help many users running Pub/Sub on Kubernetes with HPA.
References
- googleapis/python-pubsub#237 - Google engineer confirms two-buffer behavior
- Troubleshooting pull subscriptions - Server routing based on flow control constraints
- Testing Cloud Pub/Sub clients - Default flow control is 1000 messages
- 主要语言
- Java
- 星标
- 2.1k
- 派生
- 1.2k
- 平均合并
- 2 天 14 小时
- 30 天内合并 PR
- 192
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
googleapis/google-cloud-java 的其他 Issue
-
[java-core] Timestamp.of(java.sql.Timestamp) is one second late for a time less than a second before the epoch可能已有人在做 @Vcode2407 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
googleapis/google-cloud-java#14636 ·
维护者通常 1 天内回复
-
[java-firestore] UserDataConverter encodes a pre-epoch Date with negative nanos, so the write is refused可能已有人在做 @wu-hui 于 23 天前认领。 未关闭priority: p2 type: bug
难度 2/5 1-3 小时 新手友好度 75/100
googleapis/google-cloud-java#14635 · 已指派 1 人 ·
维护者通常 1 天内回复
-
[java-firestore] UserDataConverter throws IndexOutOfBoundsException on an empty __binary__ value可能已有人在做 @wu-hui 于 23 天前认领。 未关闭priority: p2 type: bug
难度 2/5 1-3 小时 新手友好度 74/100
googleapis/google-cloud-java#14634 · 1 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
[java-firestore] CustomClassMapper.convertBigDecimal calls .abs() on Double, unintentionally flipping negative values to positive可能重新可做 @wu-hui 于 45 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
googleapis/google-cloud-java#14533 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Github CI failing when trying to install xmllint可能已有人在做 @blakeli0 于 81 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
googleapis/google-cloud-java#14132 · 1 条评论 ·
维护者通常 1 天内回复
查看 googleapis/google-cloud-java 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 88/100
-
难度 2/5 1 小时以内 新手友好度 84/100
cpp-best-practices/project_validator#251 ·
维护者通常 1 天内回复
-
backend enhancement
难度 2/5 半天 新手友好度 70/100
damodharg6/payledger#16 ·
-
[Bug]: An initiative list with no current entry logs an error on every change可能已有人在做 @dwizard0 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 85/100
维护者通常 1 天内回复