Publish/subscribe pattern
How one service can announce an event to many others without knowing who they are. Seven steps follow order 42 through a broker, a topic and four subscribers.
In 30 seconds
In publish/subscribe, a sender does not call its receivers. It publishes a message to a named topic held by a broker, and that is all it does.
Any service that cares subscribes to the topic. The broker gives every subscriber its own copy of each message, which is called fan-out.
The publisher never learns who is listening, so listeners can be added or removed without touching it. A work queue is different: there, each message goes to only one worker.
Publish/subscribe pattern, step by step
Brokers differ in what happens when a subscriber is offline: some drop its messages, others keep them until it returns. Many may also deliver a message twice, so subscribers should tolerate repeats.
- Step 1: Direct calls tie services up. A shop takes order 42. The orders service calls three other services itself, one after another. It has to know each of them by name, wait for each, and be edited whenever a new one wants to hear about orders.
- Step 2: Publish to a topic instead. A broker is put in the middle. It holds topics, which are named channels. The orders service now does one thing: it publishes a message about order 42 to the topic order.placed, and moves on.
- Step 3: Subscribers sign up. Each service that cares tells the broker which topics it wants. Stock and Analytics subscribe to order.placed. Email subscribes to both topics. The broker keeps the list of who is listening to what.
- Step 4: Fan-out: one in, three out. When order 42 is published, the broker looks up the subscribers of order.placed and hands a copy to each one. This is fan-out: a single message spreads to every listener, and each reacts in its own way.
- Step 5: The publisher does not know. A fraud check is added later. It subscribes to order.placed and starts receiving orders. Nothing in the orders service changed, because the publisher only knows the topic name, never who is listening.
- Step 6: Not the same as a work queue. A work queue also sits between sender and receivers, but it shares jobs out: each message goes to exactly one worker, so adding workers spreads the load. A topic copies: each message goes to every subscriber, so adding subscribers adds reactions.
- Step 7: Where it is used. The same shape appears wherever one event interests many parties: a chat room delivers each post to everyone in it, a price feed pushes each change to every open screen, and backend services react to business events without calling one another.
Try it yourself
Learn this first
Learn next
- Event-driven architectureServices announce facts as events and others react, without direct calls. Intermediate
- Background jobs and workersMove slow work out of the request: a queue, workers and retries. Beginner
- Observer patternSubscribe, notify, unsubscribe: how one change reaches many listeners. Beginner
Spotted a mistake on this page?