Microservices Message Brokers
Services often need to talk without waiting for each other. A message broker sits in the middle, accepts messages from senders, stores them safely, and delivers them to receivers when those receivers are ready. Senders and receivers never meet directly, and that distance makes the whole system calmer and more flexible.
Layman Explanation
A post office holds your letter until the recipient collects it. You do not stand at the recipient's door waiting for an answer. The post office keeps the letter safe if the recipient is away. A message broker plays the post office for software.
Why Use a Broker
- Sender and receiver work at different speeds without blocking each other.
- Messages survive when a receiver crashes or restarts.
- New receivers join without any change to the sender.
- Sudden traffic spikes queue up and drain at a steady pace.
Two Main Models
Queue: One Message, One Worker
Several workers read from a shared queue. Each message goes to exactly one worker, so work spreads across them.
Order service --> [ msg4 | msg3 | msg2 | msg1 ] --> Worker A
QUEUE --> Worker B
--> Worker C
(each message handled by one worker only)
Publish and Subscribe: One Message, Many Listeners
A publisher sends an event to a topic. Every subscriber to that topic receives its own copy.
+--> Email service
Order service --> TOPIC -+--> Inventory service
"OrderPlaced" +--> Analytics service
Popular Brokers
| Broker | Known For |
|---|---|
| RabbitMQ | Flexible routing and classic queues |
| Apache Kafka | High-volume event streams that keep history |
| Amazon SQS and SNS | Managed queues and topics in the cloud |
| Google Pub/Sub | Managed global messaging |
Delivery Guarantees
At Most Once
A message arrives zero or one time. Loss is possible, duplicates are not.
At Least Once
A message arrives one or more times. Loss is rare, and duplicates can occur. Receivers must handle repeats.
Exactly Once
A message takes effect one time. This guarantee is costly and works only in limited setups, so most teams combine at-least-once delivery with idempotent receivers.
Acknowledgments
A receiver confirms each message after processing it. The broker removes the message only after that confirmation. A crash before confirmation returns the message to the queue for another attempt.
Ordering and Partitions
Kafka splits a topic into partitions. Messages inside one partition keep their order. Choosing the order id as the partition key keeps all events of one order in sequence while different orders flow in parallel.
Choosing Between Queue and Stream
- Pick a queue when tasks are one-time jobs, such as sending an email.
- Pick a stream when many consumers need the full history, such as analytics and auditing.
Good Practices
- Keep messages small and self-describing with an id, type, and timestamp.
- Version message formats so old and new consumers coexist.
- Monitor queue length to catch slow consumers early.
- Protect the broker with authentication and encryption.
Example: Order Flow with a Broker
A customer places an order. The order service publishes one OrderPlaced event and returns immediately. The payment, inventory, and email services each consume the event at their own pace. When the email service goes down for an hour, messages wait in its queue and the email service sends everything after it returns. Customers never notice the outage.
Key Takeaways
A broker decouples services in time and in knowledge of each other. Use queues for work distribution, topics for event broadcast, and design receivers that tolerate repeated messages.
