Prompt
Pub/Sub lag and ordering issues
Latest observation
If you’re seeing Pub/Sub lag and ordering issues, the root cause is usually one of a few common patterns. Here’s a practical checklist to diagnose and fix it.
1) First clarify what “lag” means
In Pub/Sub, lag can mean:
- Publish lag: messages are being published slowly or in bursts
- Delivery lag: subscriber is receiving messages late
- Ack lag: subscriber gets messages but doesn’t ack fast enough, so they redeliver
- Processing lag: your app receives messages on time but downstream work is slow
Check:
- subscription oldest unacked message age
- num undelivered / unacked messages
- subscriber ack latency
- redelivery count
2) Common causes of lag
Subscriber too slow
- Not enough workers/threads
- Long processing in the callback
- Blocking I/O or DB calls
- CPU bottlenecks
Fix:
- increase subscriber parallelism
- process asynchronously
- batch downstream writes
- scale out instances
Ack deadline too short
If processing exceeds the ack deadline, messages get redelivered, which looks like lag and can worsen ordering.
Fix:
- extend ack deadline
- use automatic lease extension
- make processing faster or offload work
Flow control too tight
Subscriber client may be limiting outstanding messages too aggressively.
Fix:
- increase max outstanding messages/bytes
- ensure app can actually handle the concurrency
Hot partition / skewed keys
If one ordering key gets most traffic, that key becomes the bottleneck.
Fix:
- spread load across more ordering keys
- reduce reliance on a single hot key
- shard by user/account/tenant if possible
3) Ordering issues: what to know
Pub/Sub ordering is only guaranteed per ordering key, not globally.
So ordering breaks if:
- messages use different ordering keys
- a single ordering key is processed concurrently downstream and completion order differs
- redelivery happens after failure and your app isn’t idempotent
- you publish from multiple producers without a consistent keying strategy
4) Common causes of ordering problems
Parallel processing within the same ordering key
Even if Pub/Sub delivers in order, your app may process out of order if it handles messages concurrently and commits results in arbitrary order.
Fix:
- process one message at a time per ordering key
- preserve sequence in downstream writes
- serialize by key in your worker
Message redelivery after failure
If message A fails and is redelivered while B succeeds, your consumer may observe B before A unless you guard against it.
Fix:
- make handlers idempotent
- store last processed sequence number
- ignore stale/out-of-order messages
Multiple subscriptions / fan-out
Each subscription has its own delivery behavior. Ordering across subscriptions is not coordinated.
Fix:
- don’t assume ordering across subscriptions
- enforce ordering in the consumer if needed
5) Practical debugging steps
-
Check metrics
- oldest unacked age
- ack latency
- redeliveries
- subscriber throughput
-
Verify ordering key usage
- Are all related messages using the same key?
- Are too many messages using one key?
-
Inspect subscriber concurrency
- Are you processing multiple messages from the same key in parallel?
- Are you doing async work without preserving order?
-
Look for failures/timeouts
- retries
- dead-letter topic usage
- ack deadline expirations
-
Test with a single key
- publish a small ordered stream
- confirm consumer behavior under failure and retry
6) Recommended design patterns
If ordering matters strongly
- Use ordering keys
- Keep processing per key serial
- Make handlers idempotent
- Persist sequence numbers/versioning
- Use dead-letter topics for poison messages
If throughput matters more than ordering
- Relax ordering requirements
- Use multiple keys
- Scale horizontally
- Process messages independently
7) Quick fixes that often help
- Increase subscriber worker count
- Enable/verify automatic ack deadline extension
- Reduce per-message processing time
- Avoid blocking operations in callback threads
- Use idempotency keys / sequence numbers
- Revisit ordering key strategy
If you want, I can help you debug this more concretely if you share:
- language/client library
- pull vs push subscription
- whether ordering keys are enabled
- sample metrics or logs
- what “ordering issue” looks like in your app
Brands