CSE Roadmap

Specialist tier · about 2 hours · 6 parts

Part 3 of 6 · 1 min read

Message brokers

Think of a message broker as a post office sitting between senders and recipients. A sender drops off a letter and walks away; the post office holds it safely until the recipient is ready to collect it, even if the recipient is out for the day.

In plain words: A message broker is a server that sits between producers and consumers, so producers do not need to know or wait on who is receiving their messages. Producers write messages to the broker, and consumers read them from the broker at their own pace. Centralizing messages in a broker makes it easier to handle consumers that disconnect, crash, or run slowly, because the broker, not the producer, holds the backlog. A broker that only keeps messages in memory loses them on a crash; one that writes them to disk survives a crash at some cost to speed.

A worked example. The bookstore's checkout service sends an "order placed" event to a broker and moves on immediately. A shipping service and an email service both read that same event from the broker whenever they are ready, even if the email service is temporarily down for maintenance and catches up later.

The key lines. No new SQL; the shape below is a message send, not a query.

broker.publish(topic="orders", message={"order_id": 9001, "customer_id": 501})

Message broker

A message broker is an intermediary server that receives messages from producers and holds them for delivery to consumers, decoupling the timing of sending from the timing of processing.

Example: the broker above holds the "order placed" event until both the shipping service and the email service have each read it.