Prompt
Pub/Sub ordering not working reliably, how do I debug it?
Latest observation
If Pub/Sub ordering isn’t behaving reliably, debug it in layers: publisher, topic/subscription config, delivery behavior, and your consumer code. Ordered delivery in Pub/Sub is only guaranteed within an ordering key and only while conditions are healthy—a single error can pause that key and make it look “out of order” if your app retries or processes messages concurrently.
1) Verify ordering is actually enabled end-to-end
Check all of these:
- Topic supports ordering: messages must be published with an ordering key
- Subscription has ordering enabled
- Publisher client enables ordering keys if your SDK requires it
- Subscriber code handles ordered delivery correctly:
- process messages for the same ordering key sequentially
- ack in order if your application logic depends on it
Things to confirm
- Messages are published with the same ordering key
- You are not using multiple independent consumers that can race on the same key
- Your subscriber is not doing parallel processing for a single ordering key
- You’re not mixing “ordered” and “unordered” expectations
2) Understand the main failure mode: one bad message can stall a key
With Pub/Sub ordering, if a message for an ordering key fails delivery/processing, Pub/Sub may pause subsequent messages for that key until the issue is resolved.
Common causes:
- subscriber code returns an error / doesn’t ack
- message exceeds ack deadline repeatedly
- transient network failures
- invalid payload causing app-level retries
- exactly-once or ack behavior interacting with retries
This can create symptoms like:
- messages for key
Astop - later messages for key
Aappear delayed or “missing” - when the key resumes, messages arrive in a burst and seem reordered relative to other keys
3) Check subscription and delivery metrics
Look at Cloud Monitoring metrics for Pub/Sub:
- num_undelivered_messages
- oldest_unacked_message_age
- ack_deadline_exceeded_count
- delivery_attempt_count / retry-related metrics
- subscriber-side latency and error rates
What to look for:
- one ordering key causing backlog growth
- repeated ack deadline expirations
- spikes in NACKs or exceptions in the consumer
If one key is unhealthy, that’s often the root cause.
4) Inspect logs with ordering key and message IDs
Add structured logs in both publisher and subscriber:
Publisher log fields
- ordering key
- message ID returned by Pub/Sub
- publish timestamp
- payload sequence number if you have one
Subscriber log fields
- ordering key
- message ID
- delivery attempt
- receive timestamp
- ack/nack result
- your application sequence number
If you can, include an application-level sequence number in the payload so you can compare:
- publish order
- receive order
- processing order
- ack order
This is the fastest way to tell whether the issue is:
- Pub/Sub delivery
- subscriber concurrency
- retry behavior
- application-side reordering
5) Make sure your consumer isn’t reordering messages itself
Even if Pub/Sub delivers in order, your app can break it.
Common mistakes:
- using a thread pool to process messages from the same ordering key in parallel
- acknowledging before processing completes
- writing to a database asynchronously without preserving sequence
- using multiple subscriber instances without key-aware coordination
- buffering and flushing out of order
Rule of thumb:
- same ordering key → single in-order processing lane
- different ordering keys can be parallelized safely
6) Check ack deadlines and retry behavior
If processing sometimes takes longer than the ack deadline:
- messages may be redelivered
- duplicates can appear
- ordering can appear inconsistent from the app’s perspective
Debug:
- compare processing time vs ack deadline
- see if your subscriber extends the lease properly
- look for slow handlers or downstream bottlenecks
If necessary:
- increase ack deadline
- use automatic lease extension
- reduce handler latency
- move slow work off the critical path
7) Reproduce with a small deterministic test
Create a minimal test:
- Publish messages with ordering key
test-key - Assign sequence numbers 1..N
- Subscriber logs every message and intentionally fails one message mid-stream
- Observe whether:
- later messages for that key pause
- redelivery occurs
- sequence resumes after acking the failed message
This helps distinguish:
- expected ordering pause behavior
- application bugs
- client library misuse
8) Use one subscription per consumer pattern carefully
If multiple consumers read the same subscription:
- they may each receive different messages for the same ordering key over time
- this is okay only if each key is processed sequentially and your logic is idempotent
If you need stronger control:
- use a single consumer per subscription, or
- route by ordering key to a single worker/partition, or
- enforce key-based serialization in your app
9) Confirm you’re not confusing ordering across keys
Pub/Sub ordering is not global. It is only per ordering key.
So this is valid:
- key
A: 1, 2, 3 - key
B: 1, 2, 3 - delivery interleaves A/B arbitrarily
If you expect all messages across all keys to be globally ordered, Pub/Sub ordering will not provide that.
10) If you use dead-letter topics, verify behavior
Dead-letter policies can change how failed messages are handled. If a message is repeatedly failed:
- it may be sent to the dead-letter topic
- later messages for the same ordering key may behave differently depending on the retry flow
Check:
- max delivery attempts
- whether failed messages are being dead-lettered
- whether your app is relying on retries to preserve continuity
Quick checklist
- Ordering key set on publish
- Ordering enabled on subscription
- Subscriber processes each ordering key sequentially
- No ack before processing completes
- Ack deadline long enough
- No hidden retries or parallel writes reordering your output
- Logs include ordering key + sequence number
- Monitoring shows whether one key is stalled
If you want, I can help you debug a specific setup
If you share:
- language/SDK
- publisher code
- subscriber code
- whether you use push or pull
- subscription config
- an example of the “out of order” logs
…I can help pinpoint the likely cause and suggest exact fixes.
Brands