A RabbitMQ architecture for notification systems
The short answer
Separate queues per channel so the slowest provider does not set your throughput. Use bounded exponential backoff with a dead-letter queue, and alert on its depth. Store the provider event ID before processing so at-least-once delivery becomes safe. Those three habits remove most notification failures.
Separate what fails differently
Email, SMS and push notifications fail in different ways and at different rates. Putting them on one queue means the slowest, flakiest provider decides your throughput. One queue per channel, and per priority class where it matters, keeps failures contained.
The same logic applies inside a channel. A password reset and a weekly digest are both email, but only one of them has a user staring at an inbox. Separate priority classes cost almost nothing and prevent a bulk send from delaying the message that matters.
Retries need a ceiling and a graveyard
Exponential backoff with a bounded retry count, then a dead-letter queue. A message that cannot be processed must end up somewhere a human can inspect it, not loop forever burning worker capacity.
The dead-letter queue is only useful if someone is alerted on its depth. Monitor it the way you monitor error rate. An unwatched dead-letter queue is just a slower way of dropping messages.
| Failure | Correct response |
|---|---|
| Provider returns 500 | Retry with backoff; it is probably transient |
| Provider returns 429 | Back off using their retry-after header, not your own schedule |
| Invalid recipient address | Do not retry; dead-letter immediately and flag the record |
| Malformed message | Dead-letter on first failure; retrying cannot fix a payload |
| Your own bug | Dead-letter, fix, then replay from the queue |
Idempotency is not optional
At-least-once delivery means your consumer will see duplicates. Store the provider event ID before processing and treat a repeat as a no-op. This single habit removes an entire category of production bug.
The ordering matters more than it looks. Record the ID first, in the same transaction as the work, and let a unique constraint reject the duplicate. Checking whether you have seen an ID and then processing is a race with a window wide enough to lose money in.
The topology, concretely
Abstract advice about queues is easy to agree with and hard to act on, so here is the arrangement we actually deploy for a product sending transactional and bulk notifications across three channels.
- 01One topic exchange for notifications. Routing keys read channel.priority, for example email.transactional or sms.bulk.
- 02One queue per channel and priority pair. Six queues for three channels and two priorities, which is more queues than people expect and still trivially manageable.
- 03A separate consumer pool per channel, sized to that provider's throughput rather than to a global average.
- 04One retry exchange with a per-queue delay, so a backed-off message leaves the main queue instead of blocking the consumer behind it.
- 05One dead-letter queue per channel, never one shared graveyard, because the triage question differs by channel.
The rule underneath all five: anything that can fail independently gets its own queue, and anything that gets its own queue gets its own alert. A queue nobody is alerted on is a place where messages go to be forgotten quietly.
Mistakes we are usually called in to undo
Notification systems fail in a small number of recognisable ways. These five account for most of the incidents we have been asked to diagnose.
- Retrying inside the consumer. Sleeping and retrying in place holds the connection, blocks the prefetch window, and turns one slow provider into a stalled worker pool. Backoff belongs in the broker, not in your loop.
- Acknowledging before the work is done. Acknowledging on receipt rather than on success means a crash loses the message silently, which is the failure mode queues exist to prevent.
- One queue for everything. The bulk digest and the password reset share a queue, and the digest decides how long the reset waits.
- A dead-letter queue with no alert and no owner. It fills up for six weeks, and by the time anyone looks the messages are too stale to replay.
- Unbounded prefetch. A consumer claims a thousand messages, then dies holding all of them, and redelivery arrives as a thundering herd against a provider that is already unhappy.
What to monitor
- Queue depth per channel, because a rising graph is the earliest warning you get.
- Dead-letter depth, alerted on any sustained increase rather than on a threshold.
- Consumer lag, because a queue that is draining slowly is a queue that is about to be deep.
- Per-provider error rate, split from your own error rate, so you know whose outage it is.
- Time from enqueue to delivery at the 95th percentile, which is what a user experiences.
