Skip to content

Chapter 17: Message Queue


Metadata Card

AttributeContent
Difficulty(Intermediate)
PrerequisitesBasic understanding of producer/consumer patterns
KeywordsMessage queue, Kafka, RabbitMQ, Pulsar, pub-sub, broker, partition, offset
CodePython (client examples)

Your Progress

"The conveyor belt system between warehouses — decoupling production and consumption, smoothing peaks and filling valleys."

Why Message Queues?

ProblemSolution
Tight coupling between servicesDecouple producers and consumers
Traffic spikes overwhelm consumersBuffer + back-pressure
Service temporarily unavailablePersistent storage + replay
Broadcast to multiple servicesPub-sub pattern
Async processingFire-and-forget

Core Concepts

ConceptDescription
ProducerSends messages to the queue
ConsumerReads messages from the queue
BrokerServer that manages topics/partitions
TopicCategory/feed name for messages
PartitionOrdered, immutable sequence of messages
OffsetPosition of a message within a partition
Consumer GroupMultiple consumers coordinate to process a topic

Delivery Semantics

GuaranteeMeaning
At most onceMessage may be lost, never duplicated
At least onceMessage never lost, may be duplicated
Exactly onceMessage delivered once and only once

Kafka

Design: Distributed commit log. Topics split into partitions, each partition is an ordered log.

Topic "orders"
┌──────────┬──────────┬──────────┬──────────┐
│Partition0│Partition1│Partition2│Partition3│
├──────────┼──────────┼──────────┼──────────┤
│ msg 0     │ msg 0     │ msg 0     │ msg 0     │
│ msg 1     │ msg 1     │ msg 1     │ msg 1     │
│ msg 2     │ msg 2     │ msg 2     │ msg 2     │
│ ...       │ ...       │ ...       │ ...       │
└──────────┴──────────┴──────────┴──────────┘

Key features: High throughput (sequential I/O), durable, replayable, scalable.

RabbitMQ

Design: Smart broker / dumb consumer model. Exchange routes messages to queues based on routing rules.

Exchange types: Direct, Topic, Fanout, Headers.

Features: AMQP protocol, flexible routing, dead letter queues.

Pulsar

Design: Separate serving and storage layers (Apache BookKeeper). Supports both queue and streaming semantics.

Key innovation: Unified messaging model — same system can be used as a message queue (exclusive consumers) or a streaming system (competing consumers).

Use Cases

  • Event-driven microservices: Service A publishes events, Service B/C subscribe
  • Stream processing: Kafka Streams, Flink, Spark Streaming
  • Task queues: Celery, Bull, Sidekiq
  • Log aggregation: Centralized log collection
  • Metrics collection: Distributed metrics pipeline

Traveler's Notes

Message queues are the backbone of modern, loosely-coupled distributed systems. They allow services to communicate asynchronously, smooth out traffic spikes, and provide a durable buffer between producers and consumers. Understanding the trade-offs between Kafka (throughput), RabbitMQ (flexible routing), and Pulsar (unified model) helps you choose the right foundation for your architecture.

Built with VitePress | Software Systems Atlas