Message queues and Kafka
We'll cover the following
Visual summary

Download imageFree to share, please link back to this lesson.
A message queue is a waiting line between two services. Example: when a user places an order, the order service must also send an email, update stock and notify the warehouse. Calling all of them directly means the order is slow, and if the email service is down the order fails. Instead the order service drops one message ("order 42 placed") on a queue and returns immediately. Other services pick it up when they are ready.
This gives three things: the producer (sender) does not wait for the consumer (receiver); a slow or dead consumer does not break the producer; and traffic spikes pile up in the queue instead of crashing anything. The broker is the queue server in the middle (RabbitMQ or Kafka). It stores messages durably, hands them out, and tracks who has read what.
Order service (producer)
| "order 42 placed" (published ONCE)
v
[ BROKER: topic order.placed ]
|-> Email service (gets its own copy of every message)
|-> Stock service (gets its own copy of every message)
|-> Warehouse service (gets its own copy of every message)Claim-check pattern: keep messages small
Do not put big files on a queue (e.g. a 1B-record output file). Write the file to S3 and put only a small pointer message on the queue: {"file": "s3://bucket/out.parquet", "records": 1000000000}. The next service fetches the file itself.
Two designs: RabbitMQ vs Kafka
Think of RabbitMQ as a to-do list: each message is a task, and once a worker finishes it, the task is crossed off and gone. Think of Kafka as a notebook that keeps every entry: readers keep their own bookmark and can flip back and re-read old pages.
| RabbitMQ | Kafka | |
|---|---|---|
| Model | Task queue. The broker pushes messages to workers. | Durable log. Consumers pull messages at their own pace. |
| After delivery | Worker acks, message is deleted. | Message stays (days, or forever). Reading does not remove it. |
| Who tracks progress | The broker: waits for an ack, resends if none arrives in time. | The consumer: keeps an offset (a bookmark) and saves (commits) it. |
| Strengths | Simple jobs, flexible routing. | Replay, many independent consumers, huge throughput (sequential disk writes). |
Kafka vocabulary
| Term | What it is | Example |
|---|---|---|
| Topic | A named stream of messages, like a table or folder. | orders |
| Partition | A topic is split into partitions so many consumers can read in parallel. Each partition is an ordered, append-only log. | orders has 6 partitions |
| Offset | A message's position number inside its partition. The message's offset is fixed and shared. The bookmark (committed offset) is separate: it is stored per consumer group, per partition, in Kafka's internal __consumer_offsets topic. So each group has its own position in the same log. | email group is at offset 1052 in partition 3, while analytics group is at 980 |
| Consumer group | A team of consumers sharing one topic. Each partition is read by exactly one member of the group, so work is split. A different group gets its own full copy of the stream. | email-service group and analytics group both read all of orders |
| Partition key | The producer picks the partition by hashing a key. Same key means same partition. | key = user_id: all events of one user stay in order |
- Ordering: Kafka guarantees order only inside one partition, not across the whole topic. Use a key (e.g.
user_id) when events for the same entity must stay in order. - Broker vs consumer: the Kafka broker never waits for consumer acks and never resends. After a crash the consumer asks for its last committed offset and re-reads from there.
- Producer side
acks: this is a different thing.acks=allmeans the producer's write is confirmed only once all in-sync replicas have stored it, so a broker crash cannot lose it.
Fan-out vs scaling: groups vs partitions
Two different questions, and mixing them up is the most common confusion:
- Who gets a copy of the message? A consumer group. Email, SMS and invoice are different services that each need every order, so each is its own group, and all of them see all orders. That is fan-out.
- How do several workers share one service's work? Partitions. Inside one group, each order goes to only one worker, so the group's work is split. That is scaling.
Worked example: the order service publishes order 42 placed once to topic order.placed, which has 6 partitions, and three services read it.
group "email" (3 workers): every order reaches email, split across its 3 workers
group "sms" (2 workers): every order reaches SMS, split across its 2 workers
group "invoice" (6 workers): every order reaches invoice, one worker per partition
Email workers: worker-1 reads P0,P1 worker-2 reads P2,P3 worker-3 reads P4,P5So order 42 is read three times in total, once by each group. Within each group, only one worker handles it.
- Add a new service (e.g. loyalty points): create a new consumer group. The order service does not change, and the group can even start from the oldest retained message.
- A service is slow (invoice): add workers to that group only. The limit is the partition count, since a partition belongs to one worker in a group. A 7th worker on a 6-partition topic sits idle.
- A service is down (email): the other groups keep going. Email resumes from its own committed offset when it comes back.
- Do not make one topic per consumer. The producer would publish N times, which can half-fail (email written, SMS not), and it would need to know every consumer.
RabbitMQ does fan-out differently. A plain queue gives each message to only one consumer, so you create one queue per service and let the broker copy the message into each: publish to a fanout or topic exchange, which is bound to email-queue, sms-queue and invoice-queue. Each queue has its own pool of workers that compete for its messages. On AWS the same pattern is SNS topic to one SQS queue per service. The producer still publishes once; the broker does the copying.
Check yourselfThe invoice service is falling behind on order.placed. Does that slow down the SMS service? How do you speed invoice up, and what is the ceiling?
No. Each group has its own committed offset in the same log, so invoice lagging does not affect SMS. To speed invoice up, add workers to the invoice group only. The ceiling is the partition count: with 6 partitions, a 7th worker has nothing to read. Beyond that you need more partitions (which changes key-to-partition mapping, so plan it) or faster per-message processing.
Delivery guarantees come from when you commit
The question is what happens if the consumer crashes between reading a message and saving its bookmark. Say it reads message 7 (a payment). Two orders are possible:
A) Commit AFTER processing = at-least-once
1. read msg 7
2. charge the card
3. CRASH before commit
4. restart: offset still says 6
5. msg 7 read again -> charged twice
B) Commit BEFORE processing = at-most-once
1. read msg 7
2. commit offset 7
3. CRASH before charging
4. restart: offset says 7 is done
5. msg 7 never processed -> lost| Guarantee | Meaning | In Kafka |
|---|---|---|
| At-most-once | May lose messages, never duplicates | Commit offset before processing |
| At-least-once | Never lost, may be delivered twice. This is the usual default. | Commit offset after processing |
| Exactly-once | Hard and expensive, mostly Kafka-internal flows | Rare in practice |
Idempotency: the fix for duplicates
Idempotent means doing the same operation twice has the same effect as doing it once. Problem: a consumer charges $50, crashes before acking, the message is redelivered, and the user pays $100. Fix:
- Every message carries a unique ID (e.g.
payment_id). - Before processing, check whether that ID was already handled (a DB table with a unique constraint, or Redis
SETNX). - If yes, skip and ack. If no, do the work and record the ID in the same transaction as the charge.
- Also pass an idempotency key to the payment provider (Stripe supports this).
Check yourselfYour consumer sends a welcome email per user_signed_up message and some users got two emails. Why, and what do you change?
The consumer commits after processing (at-least-once), so after a crash or rebalance the message was redelivered. Make it idempotent: store the message ID (or user_id + email type) with a unique constraint and skip if it already exists. Switching to commit-before-processing would just trade duplicates for lost emails.
Which one to pick
- Kafka: event streams, high volume, several teams reading the same data, replay after a bug.
- RabbitMQ: simple background jobs (send email, resize image) with routing, where messages are not needed after they are done.
Summary: say it in the interview
- Add a group when a new service needs the data. Add workers (up to the partition count) when an existing service is too slow.
- "At-least-once delivery plus idempotent consumers gives effectively-once processing."