Part 2 of Building a Notification Service
Why Kafka, and why not RabbitMQ?
Module 2 · 5 min read
Why Kafka, and why not RabbitMQ?
TL;DR
- RabbitMQ deletes a message once it’s acked. Kafka keeps it, and each consumer group tracks its own offset.
- Kafka is better at replay, many readers, per-user order and backlogs. RabbitMQ is better at priorities, TTLs, per-message redelivery and routing.
- I went with Kafka because the events were already there. The real bottleneck is the SMS gateway anyway.
The question everyone asks
Should the queue from module 1 be Kafka or RabbitMQ? Both are mature and fast enough for notifications, and I don’t think either is simply better. They’re built on different ideas.
A post office and a diary
RabbitMQ is a post office. It routes each message to one or more queues, a consumer takes it out, and on ack it’s deleted. The smart broker tracks every message and can hold some back or let urgent ones jump the line.
Kafka is a diary. Producers append events to a log, and nothing is routed or deleted on read. Each consumer group keeps its own offset, and events stay until retention removes them, days later. The broker stays simple. (More in my Learning Kafka series.)
Publish a few events, let the consumers read, then add a consumer or replay after a bug.
What Kafka gives you
- Many readers, one event. Push, SMS, inbox, analytics and fraud each read
payment.receivedas their own consumer group. A sixth reader needs no producer change and no new routing. - Replay. If a template bug sends SMS with an empty name for an hour, rewind the SMS group’s offset and resend that window.
- Order per user. Key by user ID and one user’s events share a partition, so “payment sent” never arrives after “refund issued”.
- It’s already there. In my setup, payment and login events already flowed through Kafka for other services, so notifications became one more consumer. That’s often the deciding reason, and I think a good one.
- Huge bursts. A million-event campaign is just a longer log, bytes on disk that don’t slow Kafka down.
Where RabbitMQ is better
People tend to leave this part out. RabbitMQ does these out of the box and Kafka doesn’t.
- Priorities. An OTP jumps ahead of campaign messages in the same queue. Kafka needs separate topics (
sms.otp,sms.bulk) with their own consumers and capacity (module 3). - Delays and expiry. A TTL per message or queue, plus dead-lettering, covers “don’t send an OTP older than two minutes” and “retry in 30 seconds”. In Kafka you build both, with a timestamp check in the consumer and retry topics.
- Per-message redelivery. One failed message is redelivered while the others move on. A Kafka offset is one position per partition, so a stuck message blocks everything behind it unless you move it aside.
- Flexible routing. Exchanges route by patterns like
sms.otp.*without new topics. - Simple work queues. For a few thousand jobs and many workers, each done once, RabbitMQ is simpler to run and reason about.
Well, the lines are blurring. RabbitMQ now has streams (replayable, log-style queues), and Kafka 4.x introduced share groups (KIP-932), letting many consumers share a partition like a work queue. The core difference remains.
| Need | Kafka | RabbitMQ |
|---|---|---|
| Many independent readers of the same event | Built in (consumer groups) | One queue per reader, bound up front |
| Replay after a bug | Rewind the offset | Gone once acked (except streams) |
| Order per user | Per partition, by key | Per queue, weakened by redelivery |
| Urgent messages jump the line | Separate topic | Priority queues |
| Delay / expire a message | Build it in the consumer | TTL and dead-lettering |
| Retry one failed message alone | Retry topic | Nack and redeliver |
| Very large backlogs | Just a longer log on disk | Possible, but needs care |
What I ended up with
Kafka as the event backbone. The events were already there, many services read them, and I didn’t want to give up replay. On top I built the RabbitMQ-style bits I needed: a high-priority OTP topic, an expiry check in the consumer, and retry and dead-letter topics (modules 3 and 4).
A small service built from scratch, with one reader per message and lots of delays and priorities, would do very well on RabbitMQ. “We use Kafka everywhere” is a good reason. “Kafka is faster” usually isn’t, because the bottleneck is the SMS gateway.
Quick check
A bug sent 2,000 SMS with the wrong amount an hour ago; the fix is deployed. Which setup lets you resend just those without the payment service republishing?
Kafka keeps events after they’re read, so the SMS group can rewind to the bug window. A classic queue deleted them on ack. (Keep idempotency on, so users who got a correct SMS don’t get another.)
Q&A
Isn’t Kafka overkill for notifications?
For a small app, maybe. It pays off with many producing services, many consumers and campaign spikes. With one producer and one consumer, a simple queue, or even a database table polled by workers, can be enough.
Can I use both, or a cloud queue?
Plenty of companies run Kafka for business events and RabbitMQ (or a cloud queue) for per-channel work queues, at the cost of running two systems. SQS and Pub/Sub are managed queues with visibility timeouts, delays and dead-letter queues. On their cloud they remove most of the ops work, though the log-versus-queue questions still apply.