← Back to System Design, page 1

system-design-0018 · Medium mid

What is the difference between a message queue and a publish-subscribe log such as Kafka? When would you use RabbitMQ-style queues versus Kafka?

A message queue delivers each message to one consumer and removes it once acknowledged, which suits distributing jobs. A log like Kafka appends messages to partitions and keeps them for a retention period; many consumer groups read independently by tracking offsets and can replay. Use queues for task distribution with per-message acks and routing; use a log for event streams, multiple subscribers, ordering per key and replay.

Full answer

Why asynchronous messaging

A queue sits between a producer and a consumer so they do not have to be up, or fast, at the same time. Uploading an assignment can return immediately while virus scanning and plagiarism checks run later. Messaging absorbs spikes (the queue grows instead of the service failing), decouples teams, and lets you add consumers.

Classic message queue (RabbitMQ, Amazon SQS)

  • A message goes to a queue; one of the competing consumers receives it.
  • The consumer acks after processing; unacked messages are redelivered after a timeout or when the consumer disconnects.
  • Once acked, the message is gone.
  • Brokers like RabbitMQ add routing (exchanges, topic patterns), priorities, per-message TTL and dead-letter queues.
  • Ordering is not strongly guaranteed once you have several consumers and redeliveries (SQS FIFO queues add ordering per message group).

Fits: background jobs, email sending, image resizing, any "do this work once" pattern.

Partitioned log (Apache Kafka, Pulsar, Kinesis)

  • A topic is split into partitions; each partition is an append-only, ordered log.
  • Producers choose a partition, usually by key (student_id), so all events for one key stay in order.
  • Messages are kept for a retention period (for example 7 days) whether or not anyone read them.
  • Each consumer group tracks its own offset per partition. The billing service, the analytics pipeline and the search indexer each read the same events independently.
  • Within a group, each partition is read by one consumer at a time, so the partition count caps parallelism for that group.
  • Consumers can rewind offsets to reprocess history after a bug fix.

Fits: event-driven architectures, change data capture, activity tracking, log aggregation, stream processing.

Choosing

NeedPrefer
Distribute independent jobs, per-message retry and delayQueue
Several independent subscribers to the same eventsLog
Strict order per entityLog with key partitioning
Replay historyLog
Complex routing rules, prioritiesRabbitMQ-style broker
Very high sustained throughputLog (sequential disk writes, batching)

Common to both

Delivery is usually at least once, so consumers must be idempotent. Plan for poison messages (a message that always fails) with a retry limit and a dead-letter queue, and monitor consumer lag (how far behind the consumers are).

By level

Fresher: Explain why a queue decouples a producer from a consumer, using a background job example, and what an acknowledgement is. One background job example, such as resizing images, is enough.

Mid: Compare queues and logs on retention, ordering, consumer groups and replay, and choose one for a described workload. Expect correct statements about retention, offsets and partitions.

Senior: Discuss partition sizing, rebalancing, exactly-once claims and their limits, back-pressure, schema evolution of events, multi-region replication of topics and the operational cost of running a log cluster. Expect a view on partition sizing and consumer lag alerting.

Key points

  • Queues give each message to one consumer and delete it after ack
  • Logs retain messages; consumer groups track offsets and can replay
  • Kafka orders messages within a partition, so key by entity
  • Partition count caps consumer parallelism in a group
  • Both are usually at-least-once, so consumers must be idempotent

Common mistakes

  • Saying Kafka deletes messages when they are consumed
  • Expecting global ordering across Kafka partitions
  • Adding consumers beyond the number of partitions and expecting more throughput
  • Ignoring poison messages and dead-letter queues

Follow-ups

  • How would you keep events for one order in sequence while scaling consumers?
  • What is consumer lag and how do you alert on it?
  • How would you reprocess a day of events after a bug?

Tips

  • Start with one-consumer-per-message versus many-groups-read-the-log
  • Mention partitions and keys when ordering comes up
  • Give one concrete use case for each type

Evaluation rubric

  • Correct model of queues versus logs
  • Explains ordering, retention and consumer groups
  • Sensible selection criteria
  • Mentions idempotency and failure handling