Message Queue vs Pub/Sub vs Kafka: Differences

SQS, SNS, RabbitMQ and Kafka compared by where a message lives after it is read, who tracks progress, and how many readers get it.

Queues, pub/sub and logs: common questions

What is the difference between Kafka and RabbitMQ?

RabbitMQ is a queue: a message is removed once a consumer acknowledges it. Kafka is a log: messages stay for a retention period and each consumer group tracks its own offset, so many readers can replay them.

Related chapters

Keep learning

Overview

One queue SQS, a RabbitMQ queue Plain pub/sub Redis Pub/Sub Pub/sub + queue each SNS → SQS, RabbitMQ fanout Kafka, Kinesis, Redis Streams Email worker 1 Email worker 2 Analytics worker The email workers took every message. Analytics came back to an empty queue. In a queue, each message goes to one consumer and is deleted after its ack. Each order reached one worker only, so 2 orders got no email and analytics saw 2 of 6. A queue shares out work. It is not a way to tell several apps the same thing. Analytics missed all six, for good. Nothing was stored while it was away. Plain pub/sub pushes to whoever is connected right now. Every email was also sent twice. Every subscriber got every message, so both email workers sent each email: 12 emails for 6 orders. Good for live updates where a miss is fine, like typing indicators or cache invalidation. Analytics’ own queue kept all six while it was down, and delivered them when it came back. Each order emailed once, and analytics saw all six. Replay still fails: each queue deletes a message after its ack. To re-run last week, you need a log. Analytics resumed from its saved offset and read all six. Two partitions: odd orders in one, even in the other. Each email worker owns one partition. Analytics moved its offset back to 0 and read all six again. Email was not affected. Messages stay in the log until retention (7 days by default in Kafka), whoever has read them. not received received after coming back received again on replay Broker type Analytics is offline while orders 1–6 are sent Next day, analytics wants to re-read everything The order service publishes orders 1 to 6. Who received what? ✓ received ✓ after coming back ↺ on replay · never After a consumer acks Message is deleted Nothing was stored to delete Who remembers progress The broker, per message (in flight or waiting) Each consumer group, as one offset per partition One message reaches One consumer Every subscriber connected now One consumer in each group Push or pull SQS: pull (long poll). RabbitMQ: push with a prefetch limit Pull, in batches Consumer offline Messages wait (SQS keeps them 4 days by default, up to 14) It misses them Resumes from its offset if still within retention Replay old messages Yes: move the offset back Per connection, while connected Strict within a partition Scale consumers by Adding workers, any number Adding subscribers (each gets everything) Slow consumer Backlog grows in the queue Messages dropped, or the broker disconnects it system design · core concepts · messaging 2 of 2 Queues, pub/sub and logs: how they differ inside SQS, SNS, RabbitMQ and Kafka all “move messages”. Inside, they answer three questions differently: where does a message live after it is read, who remembers what each reader has seen, and how many readers get each message.

1. Three shapes

A to-do list. Each message is done by one worker, then crossed off. share work tell everyone keep history Chapter 1 needed two of these: a queue so a crashed email worker loses nothing, and pub/sub so analytics could join without touching the order service.

2. Inside a queue

The broker keeps a list of messages and tracks each one: waiting, in flight with a worker, or deleted. appended to the queue hidden from other workers within the visibility timeout delete (ack) gone for good SQS hides a received message for 30 seconds by default. No delete in time → it becomes visible and another worker gets it. Competing consumers Ten workers on one queue share the work. Add workers to go faster. Dead-letter queue After N failed receives, the message moves to a side queue instead of blocking or looping forever. Order is weak Two workers finish in any order. SQS FIFO keeps order per message group, at a lower throughput. Smart broker The broker tracks every message’s state. Consumers stay simple. RabbitMQ works the same way but pushes: it sends messages to a connected consumer up to a prefetch limit, and requeues unacked ones if that consumer’s connection drops.

3. Inside publish/subscribe

The broker keeps a list of subscribers per topic. A published message is copied to each one. to topic “orders” look up subscribers email, analytics, fraud copy and push one copy each keep nothing no backlog per subscriber Plain pub/sub (Redis Pub/Sub) Pushes only to connected clients. Offline means missed. At most once. Push with retries (SNS) Pushes to queues, functions or URLs, and retries failed deliveries for a while. It does not hold a backlog for a slow reader. Email queue Analytics queue publish OrderPlaced #7 two subscriptions copy stored email workers compete for it analytics is down: it waits analytics back reads #7 from its own queue

4. Inside a log

Partition 0 with offsets 0 to 7 and two consumer bookmarks Consumer groups Each group gets every message. Inside a group, each partition goes to exactly one consumer. Order per partition Messages with the same key (say, the order id) land in the same partition and stay in order. Retention, not ack Messages are removed by age or size (7 days by default), not because someone read them. Parallelism has a ceiling 6 partitions → at most 6 busy consumers in one group. Extra consumers sit idle. Dumb broker, smart consumer The broker just appends and serves bytes. Consumers track their own offsets. Fix a bug, move the offset back, reprocess last week. Queues cannot do this.

5. Lab: send six orders through each type

Two apps listen: email (two workers) and analytics (one worker). Switch the broker type, then take analytics offline or ask for a replay.

7. Routing inside the broker: RabbitMQ exchanges

RabbitMQ adds a step before the queue. Producers send to an exchange, and bindings decide which queues get a copy. Routing key must match exactly: “payment.failed” → the payments queue. Wildcards: “order.*.eu” or “order.#”. Every bound queue gets a copy. Pub/sub, with storage. Match on message headers instead of the key. So one RabbitMQ can act as a queue, as pub/sub, or as a router. Kafka leaves routing to the consumer: each reader filters what it needs.

8. Delivery guarantees, and where they come from

At most once Ack before doing the work, or no storage at all. A crash loses the message. Redis Pub/Sub. At least once Ack after the work. A crash before the ack means redelivery, so duplicates. The default for SQS, RabbitMQ and Kafka consumers. most common “Exactly once” Any side effect outside the broker, like sending an email or charging a card, still needs an idempotent consumer: store the message id and skip ids you have seen.

9. Picking one

You need Typical choice Background jobs: resize images, send emails SQS, RabbitMQ Tell several services that something happened Pub/sub + a queue per subscriber SNS → SQS, RabbitMQ fanout Event history, replay, stream processing, very high volume Kafka, Kinesis Live signals where a miss is fine: typing, presence, cache invalidation Plain pub/sub Redis Pub/Sub Strict order for one customer or one account FIFO queue or log keyed by that id SQS FIFO, Kafka with a key Interview tip: say which question you are answering. “Each order must be emailed once, and analytics must be able to replay” already rules out a plain queue and plain pub/sub. ★ Remember Queue: one consumer per message, deleted on ack. Shares work. Pub/sub: one copy per subscriber. Add a queue per subscriber for durability. Log: kept by time, read by offset. Every group sees everything and can replay. Almost everything is at least once: make consumers idempotent.

Go deeper

← Before message queues: why systems needed a mailbox · Kafka course · the outbox pattern · celebrity fan-out · a queue in front of GPUs

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.