Prompt
duplicate messages in pub sub
Latest observation
Oct 24, 2026
OpenAI APIWeb search: off
Duplicate messages in pub/sub systems are common, and usually mean the system is providing at-least-once delivery, not exactly-once delivery.
Why duplicates happen
A message may be delivered more than once when:
- the subscriber processes a message but fails before acknowledging it
- the broker retries delivery after a timeout
- the publisher re-sends after not knowing whether the publish succeeded
- network partitions or consumer restarts happen
What to do about it
The usual fix is to make consumers idempotent:
- give each message a unique ID
- store processed IDs in a database or cache
- ignore any message already seen
- make processing safe to run more than once
Example approach
If a message has:
{
"message_id": "12345",
"payload": {...}
}
Then the consumer can:
- check whether
12345was already handled - if yes, skip it
- if no, process it and record it as done
If you need stricter guarantees
- use exactly-once semantics if your platform supports it, though it’s often limited and costly
- use transactions with message offsets/checkpoints
- use deduplication keys at the application layer
Bottom line
In pub/sub, duplicates are usually expected behavior unless you explicitly build deduplication. Design consumers to be idempotent.
If you want, I can also show:
- how to handle duplicates in Kafka
- how to handle them in Google Pub/Sub
- a code example in Python/Java/Node.js
Brands