04. Communication / Lesson 3 of 4
Pub/Sub & Events
Announce that something happened, and let anyone who cares listen.
- 10 min
- Intermediate
- 10 steps
- Calls per order3 (warning)
- Subscribers3
- Deliveries1,500/s
- Request
- Health check
Step 1 of 10: One thing happens, many services care
Read every step as text
Step 1: One thing happens, many services care
When an order is placed, the email, stock and analytics services all need to know. The simple way: the Order service calls each one. That is 3 calls for every order, 1,500/s at 500 orders a second.
Now add a fraud check. The Order service must be changed and redeployed, and if one callee is slow or down, the order slows down too. The publisher knows too much.
Every new listener means a change in the one service that should not care who listens.
Step 2: Publish to a topic, not to people
In publish/subscribe the Order service sends one message, an event such as
order.placed, to a topic on a broker. It does not know or care who reads it.The broker holds the topic and passes the event on. The publisher now makes 1 call per order, whether there are 3 listeners or 30.
An event says what happened ("order placed"). A command says what to do ("send an email"). Pub/sub carries events.
Step 3: Each subscriber gets its own copy
A service subscribes to the topic. Each subscription has its own place in the stream, so a slow subscriber does not hold back the others.
Adding the fraud check is now one new subscription. The Order service is not touched and not redeployed: the publisher still makes 1 call.
Loose coupling: teams add listeners without asking the publisher's team.
Step 4: Fan-out: one in, many out
One published event becomes one delivery per subscriber. With 500 orders a second and 4 subscribers the broker sends
500 × 4 = 2,000/s.The work moved from the publisher to the broker, which can be scaled and tuned for exactly this. The cost grows with the number of subscribers, so it is worth counting them.
Fan-out multiplies traffic. A topic with 1,000 subscribers turns 500 events a second into 500,000 deliveries a second.
Step 5: A log that keeps the events
A plain topic may drop a message once it is delivered. A log-based system (Kafka is the well-known one) works differently: events are appended to a log and stay there for a set time, such as 7 days.
Reading does not delete anything. Each subscriber keeps its own position, called an offset: the number of the next event it will read. A fast reader and a slow reader share the same log.
The log is the source of the story. Subscribers are just different positions in it.
Step 6: Partitions split the log
One log on one machine has a limit. A topic is split into partitions, here 3, holding 5, 4, 3 events (12 in all). Each partition is its own log with its own offsets.
The producer picks the partition from a key: all events with the same key (the same order id, or user id) land in the same partition, in order. Order is kept inside a partition, not across partitions.
More partitions means more parallel readers and writers. Pick the key so related events stay together.
Step 7: A group shares the work
A consumer group is a set of workers that act as one subscriber. The group's workers divide the partitions: each partition is read by exactly one worker of the group.
The email group has 3 workers for 3 partitions, one each, so it works 3 times as fast. A 4th worker would sit idle, because there is no free partition. A different group (analytics) reads all events again on its own.
The number of partitions is the ceiling for parallelism inside one group.
Step 8: Replay: go back and read again
Because the log keeps events, a bug is not the end. The analytics code had a bug, so you fix it, set its offset back to 0 and let it replay all 12 events. A new service can also start from the beginning and build its own view.
A normal queue deletes a message once it is acked, so this is not possible there. The price of a log is storage: you pay to keep the events for the whole retention time.
Replay turns "we lost a day of statistics" into "we ran the job again".
Step 9: Event-driven design and its price
In an event-driven system services react to events instead of being told what to do. Order placed starts email, stock and fraud, each on its own, and a new feature can listen without any change upstream.
The price: the flow of a request is no longer in one place, so it is harder to follow and debug. Subscribers see events a little late (eventual consistency), may see one twice (make them idempotent), and a bad event shape breaks many readers.
Write down event names and shapes like a public API, and version them.
Step 10: Pub/sub in one picture
The publisher sends one event and makes 1 call, not 3 or 4. The broker fans it out: 500 orders a second to 4 subscribers is 2,000/s.
A log keeps events so readers track their own offsets and can replay. Partitions and consumer groups share the work and keep order per key.