SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
  • Message Queues
  • Pub/Sub
  • Message Brokers
  • Delivery Semantics
  • Dead Letter Queues
  • Kafka Deep Dive
  • Retries with Backoff
  • RabbitMQ and SQS
  • Event Streaming vs Message Queues
  • Message Ordering
  • Task Queues, Workers and Scheduled Jobs
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/APIs and Communication/Asynchronous Communication

Message Brokers

The post office that stores, routes and delivers messages (Kafka, RabbitMQ, SQS).

โฑ 14 min readIntermediateInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

A message broker is the software in the middle. It receives messages from producers, stores them safely, routes them and delivers them to consumers.

  • It hides the details: producers and consumers only talk to the broker.
  • It runs queues, topics, retries, acks and storage for you.
  • Two main styles: routing brokers (like RabbitMQ) and log brokers (like Kafka).

02Concept: What is it?

Without a broker, every service must call every other service and handle failures itself. With a broker, each service connects once, to the broker.

Without and with a broker
Without a broker
๐Ÿ…ฐ๏ธ
๐Ÿ…ฑ๏ธ
every service calls every service
๐Ÿ…ฒ
๐Ÿ…ณ
With a broker
๐Ÿ…ฐ๏ธ
๐Ÿ…ฑ๏ธ
๐Ÿ“ฎBroker
๐Ÿ…ฒ
๐Ÿ…ณ
Fewer connections, and no service needs to know about the others.

What a broker does

๐Ÿ’พStores
Keeps messages safe on disk until they are handled.
๐Ÿ”€Routes
Decides which queue or topic a message goes to.
๐Ÿ“ฌDelivers
Sends to consumers, tracks acks and retries.
๐Ÿ›ก๏ธProtects
Handles failures, limits and security.

03Real-Life Example

๐ŸตThe Post Office
You do not walk to a friendโ€™s house in another city. You drop a letter at the post office. The post office sorts it, stores it, sends it on the right route and delivers it. If your friend is out, it tries again or keeps it.
Post office
Message broker
You posting a letter
Producer
Address on the envelope
Routing key / topic
Sorting room
Exchange / router
Mailbox for each street
Queue or partition
Mail carrier
Delivery to consumer
Signature on delivery
Ack
Undeliverable mail office
Dead letter queue

04Why Do We Need It?

๐Ÿ”ŒDecouple services
Services do not need to know about each other.
๐ŸงฐDo not rebuild the hard parts
Storage, retries, acks and ordering come built in.
๐ŸŒŠAbsorb spikes
The broker buffers bursts of messages.
๐Ÿ“ˆScale each side
Add producers or consumers without changing the other side.
๐ŸงฏSurvive restarts
Messages stay on disk if a service is down.
๐Ÿ”ญOne place to watch
Queue depth, lag and errors are all visible.
A broker is a shared, reliable middle layer so services can talk without being tied together.

05Where Do We Use It?

๐Ÿ›’Order pipelines
Order, payment, stock and shipping steps.
๐Ÿ“ŠEvent streams and analytics
Clicks and logs flow to data systems.
๐Ÿ”„Microservices
Services share events instead of calling each other.
๐Ÿ””Notification systems
Millions of emails, SMS and push messages.
๐ŸฆBanking and payments
Reliable steps with audit trails.
๐ŸญIoT and telemetry
Many devices send small messages.

06Types / Parts / How It Works

There are many products. They fall into two main styles. This is the general idea; each product has more features.

Routing broker (RabbitMQ-style)
Smart broker, simple consumer
  • Producer sends to an exchange
  • Exchange routes by rules (bindings) into queues
  • A message is removed once a consumer acks it
  • Good for task queues and flexible routing
Log broker (Kafka-style)
Simple broker, smart consumer
  • Messages are appended to a partitioned log
  • Messages stay until the retention time ends
  • Each consumer group tracks its own offset
  • Good for event streams, replay and high volume
Kafka-style: partitions, keys and groups
๐Ÿ“คProducerskey = user id
โ†’โ†“hash(key)
๐Ÿ“œPartition 0a1 a2 a3
๐Ÿ“œPartition 1b1 b2
๐Ÿ“œPartition 2c1 c2 c3
โ†’โ†“offsets
๐Ÿ‘ฅGroup: billing
๐Ÿ‘ฅGroup: analytics
Producer Ordered log Consumer group
The same key always goes to the same partition, so its messages stay in order. Each group reads on its own.

Compare the common ones (general idea)

ToolStyleGood at
RabbitMQRouting broker with exchanges and queuesTask queues, flexible routing
Apache KafkaDistributed log with partitionsEvent streaming, replay, huge throughput
Amazon SQSManaged queueSimple background jobs with little to run
Amazon SNS / Google Pub/SubManaged topics for fan-outPub/Sub without running servers
Redis (lists, streams)In-memory data store used as a light brokerFast, simple jobs

Ideas to know

IdeaSimple meaning
Exchange and bindingThe router and its rules for sending messages to queues
PartitionOne ordered log. More partitions means more parallel readers
OffsetThe position of a consumer in a partition (โ€œI have read up to 42โ€)
Consumer groupA set of consumers that share a topic. Each partition goes to one member of the group
RetentionHow long the broker keeps messages (time or size)
ReplicationCopies of data on other brokers so one failure does not lose it
LagHow far a consumer is behind the newest message
VisualizerRouting broker vs log brokerPublish some messages in each style. Try consuming and replaying in the Kafka-style view.
Broker style (general idea)
๐Ÿ“ฃProducer
โ†“
๐Ÿ”€Exchangeroutes by key and bindings
โ†“
orders-queue
binding: order
empty
billing-queue
binding: payment
empty
audit-queue
binding: all (#)
empty
Publish a message and watch the broker decide where it goes. Switch the style to compare.

07How to Use It / Steps / Implementation

Choosing and using a broker

  1. 1
    Write down what you need
    Task queue or event stream? Do you need replay? How many messages a second? Ordering?
  2. 2
    Pick a style
    Task jobs and routing: queue broker. Event history and many readers: log broker. Little ops work: a managed cloud service.
  3. 3
    Design topics and keys
    Pick a key (like user id) so related messages stay in order.
  4. 4
    Choose partitions or queues
    Enough partitions for your future parallel consumers. Hard to change later.
  5. 5
    Set durability
    Replicate across machines and set how long to keep messages.
  6. 6
    Handle failures
    Retries with backoff, a dead letter queue, idempotent consumers.
  7. 7
    Monitor
    Queue depth, consumer lag, error rate and broker health.
Pick a queue broker whenโ€ฆ
  • You hand out jobs to workers
  • Messages are deleted once done
  • You need smart routing rules
Pick a log broker whenโ€ฆ
  • Many systems read the same events
  • You need to replay old events
  • Volume is very high

08Key Terms to Know

TermMeaning in easy words
BrokerSoftware that stores, routes and delivers messages
ExchangeA router that sends a message to queues by rules
BindingA rule that links an exchange to a queue
TopicA named stream of messages
PartitionOne ordered log inside a topic
OffsetA consumerโ€™s position in a partition
Consumer groupConsumers that share the work of a topic
RetentionHow long messages are kept
ReplicationKeeping copies on several brokers
LagHow far behind a consumer is
AckA signal that a message was handled
ThroughputMessages handled per second

09Pros, Cons and Trade-offs

โœ… Pros
  • Decouples services
  • Built-in storage, retries and delivery tracking
  • Buffers spikes
  • Scales producers and consumers separately
  • One place to monitor
โš ๏ธ Cons
  • Another system to run and learn
  • Can become a single point of failure unless replicated
  • Ordering and exactly-once are hard to get right
  • Wrong partition count is hard to fix later
  • Extra delay compared with a direct call
ChoiceWe gainWe lose
Routing brokerFlexible routing, simple task queuesNo built-in replay once acked
Log brokerReplay, many readers, huge volumeMore to learn, order only per partition
Managed cloud brokerNo servers to runLess control, vendor lock-in, cost
Self-hosted brokerFull controlYou run, patch and scale it

10Common Problems and Mistakes

Common mistakes

  1. One broker node. It becomes a single point of failure. Replicate it.
  2. Bad partition key. One โ€œhotโ€ key sends most traffic to one partition.
  3. Too few partitions. You cannot add more parallel consumers later without effort.
  4. Not watching consumer lag. Slow consumers go unnoticed until data is hours old.
  5. Retention too short. A consumer that is down for a day misses messages.
  6. Treating the broker like a database. It is for moving messages, not for queries.
  7. Assuming global order. Order is usually only within a queue or a partition.
๐Ÿ”ฅHot partition
One partition gets most of the traffic.
๐ŸŒConsumer lag
Readers fall behind the newest message.
โš–๏ธRebalancing
When a consumer joins or leaves, partitions are moved and processing pauses briefly.

11Real-World Examples

๐ŸงตKafka
Widely used for event streams and data pipelines.
๐Ÿ‡RabbitMQ
Widely used for task queues and routing between services.
โ˜๏ธAmazon SQS / SNS
Managed queue and topic services in the cloud.
๐ŸŒGoogle Pub/Sub
A managed topic and subscription service.
๐Ÿ“Note
These are general descriptions. Exact features change over time, so check the official docs before you design with them.
Mini case: order pipeline
๐Ÿ›’Order service
โ†’โ†“
๐Ÿ“ฎBroker
โ†’โ†“
๐Ÿ’ณPayment
๐Ÿ“ฆStock
๐Ÿ“งEmail
One broker connects the order, payment, stock and email services.

12Related Topics to Read Next

Message Queuesโ†’

The simplest thing a broker runs.

Pub/Subโ†’

Topic-based fan-out that brokers support.

Delivery Semanticsโ†’

What promises a broker can make.

Dead Letter Queuesโ†’

How brokers handle failing messages.

Single Point of Failure (SPOF)โ†’

Why you replicate the broker.

Consistent Hashingโ†’

A related idea for spreading keys across nodes.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • โœ”Broker: stores, routes and delivers messages between services.
  • โœ”Routing style (RabbitMQ): exchange โ†’ queues, message deleted after ack.
  • โœ”Log style (Kafka): partitioned log, offsets, consumer groups, replay.
  • โœ”Order: only inside one queue or one partition. Use a key.
  • โœ”Managed: SQS, SNS, Google Pub/Sub when you do not want to run servers.
  • โœ”Be safe: replicate, set retention, add a DLQ, make consumers idempotent.
  • โœ”Watch: queue depth and consumer lag.
  • โœ”One line: โ€œA broker is the post office of your system.โ€
โ† Previous
Pub/Sub
Next โ†’
Delivery Semantics