Kafka explained
Why five calls in a row let loyalty points break checkout, how an event log lets every service read at its own pace, and the words you need to judge a Kafka design: topics, bookmarks, partitions and keys.
The lesson in text
Click a paragraph to hear it.01Points broke checkout
Monday, 10:40. Omar, on call, posts in the team chat: the slowest checkouts take 6 seconds, and 12% of bookings are failing. Every error says the same thing: Loyalty timed out.
Karim, the CTO, can't believe it. Loyalty adds points to a guest's account. It's a nice extra. How can a points service stop people from booking a room?
StayHub has grown. About 150,000 guests a day, 12,000 bookings, 18 engineers in four teams, and twelve services. When the teams split the app into services, they took a shortcut that's about to cost them.
In this lesson, you'll see why that shortcut lets one sick service break checkout, how an event log like Kafka cuts the chain, and the handful of words you need to judge a Kafka design.
02Five calls in a row
Here's the shortcut. In the lesson on events, the outbox helper ran inside the app. When the listeners moved out into their own services, each one became a direct call from Booking. Booking saves the room, then calls five services, one after another, and waits for each.
Saving takes 80 milliseconds. Then Email, 120. Analytics, 40. Loyalty, 90. Notifications, 60. Fraud, 150. On a good day, the guest waits 540 milliseconds.
- 1Save the booking80 ms
- 2Email120 ms
- 3Analytics40 ms
- 4Loyalty90 ms · Monday: up to 5 s
- 5Notifications60 ms
- 6Fraud150 ms
This morning, Loyalty's database is overloaded. Loyalty answers in up to 5 seconds, or not at all. Booking waits, the guest waits, and when Loyalty gives up, the whole checkout fails. The room was fine. The points weren't.
You've met this before, in the lesson on microservices: every call in a row adds its delay and its failures to yours. Make Loyalty faster today, and next week it's Fraud, or Email. And each new team that wants to hear about bookings must ask Booking to add one more call.
03The staff-room logbook
Back to our hotel. In the lesson on events, the front desk announced things on the staff radio. A radio has one weakness: if you weren't listening, the message is gone.
So picture a big logbook in the staff room instead. The desk writes each fact once, on a new line at the end: room 214 booked. It never erases a line, and it never waits for anyone.
Each department keeps its own bookmark in the book. Housekeeping reads at its pace, billing at its own. If the minibar team is away for an hour, its bookmark simply stays where it was. When they're back, they read on from there, and miss nothing.
And when a new department opens, say the spa, it adds its own bookmark, and can even start from last week's pages. The desk changes nothing.
That's an event log. Kafka, created at LinkedIn, is the best-known one. Booking writes the fact once, answers the guest, and every service reads the log when it can, even after a bad morning.
04Kafka's words
Now the words, one at a time. A topic is one logbook. StayHub's is called bookings. Each line in it is an event, like booking created, with its own id.
A consumer group is one department: all the copies of the Loyalty service read together, as one group. Its bookmark is a number called the offset: the last line it has finished.
Reading doesn't remove anything. Kafka keeps every line for a set time, read or not. That's retention. StayHub keeps seven days, so a service can go back and read last week again. That's called a replay.
One book is too slow when many clerks want to read at once. So the topic is split into partitions, like binders. StayHub has 12. Inside one department, each binder is read by one clerk at a time. So up to 12 copies of a service can work side by side. A 13th just waits.
Which binder does a line go into? Kafka picks it from a key. StayHub uses the booking's id. So booking created, paid and cancelled, for the same booking, land in the same binder, in order. Kafka keeps order only inside a binder. With no key, a reader can see cancelled before created.
- 1. bk_8812 created
- 2. bk_4410 created
- 3. bk_8812 paid
- 4. bk_8812 cancelled
- 1. bk_5023 created
- 2. bk_5023 paid
- 1. bk_1197 created
- 2. bk_3086 created
- 3. bk_1197 paid
05The new checkout
Here's the new checkout. Booking saves the booking and the event in its outbox, in one single save, just like before. A small relay copies each new event into the bookings topic. And Booking answers the guest: confirmed.
The guest now waits about 90 milliseconds instead of 540. And Loyalty's bad morning doesn't reach a single guest. Its bookmark stops moving, that's all.
Five groups read the topic: Email, Analytics, Loyalty, Notifications and Fraud. Fraud was the one to think about: should a suspicious booking wait for it? The team decided no. Fraud reads like the others, and when a booking looks wrong, it writes its own event, booking flagged, and Booking acts on it.
BEGIN; insert booking bk_8812; insert outbox BookingCreated evt_01JB; COMMIT;one single saverelay → topic bookings, key bk_8812copied after the saveguest ← "Confirmed" (≈ 90 ms)nobody else waited forThe price is lateness. If Loyalty is down for 40 minutes, its group falls 40 minutes behind. That gap is called lag. When Loyalty is back, it catches up: points late, but not lost.
So lag is the new error rate, and someone must watch it. StayHub alerts when any group is more than 5 minutes behind. And an event that fails 5 times is moved to a separate dead-letter topic, so one bad line can't block its whole binder.
06Read twice
Thursday, 16:20. The loyalty team deploys a new version. While copies stop and start, Kafka shares the binders out again between the copies that are running. That's called a rebalance.
Some copies had already added the points, but hadn't moved their bookmark yet. The new readers started from the old bookmark, and added the same points again. About 300 guests got double points, and some of them spent them.
- 1. read evt_01JB
- 2. add 120 points
- 3. deploy, rebalance: bookmark not moved
- 4. new copy reads evt_01JB again: +120 again
- 1. read evt_01JB
- 2. one save: add 120 points + note evt_01JB as handled
- 3. move the bookmark
- 4. evt_01JB again? already handled: skip
This isn't a Kafka bug. It's how Kafka delivers by default: at least once. Once an event is written, it won't be lost. But a reader may get it twice, after a crash, a deploy, or a rebalance.
The fix is the one you know from the lesson on events. Every event has an id. Loyalty writes that id in a list of handled events, in the same save as the points, and skips any id it has already seen. And thanks to seven days of history, the team found the 300 guests and corrected them.
07When it's worth it
Kafka is a serious system to run: binders to plan, lag to watch, and someone on call when it misbehaves. A managed cluster for StayHub costs about €1,800 a month.
Karim pushes back: we already have the outbox. Why not one job per service per booking? Lina answers: then Booking must know every reader, and the sixth service asks us again. And a job is gone once it's done. Fraud wants to replay last week.
So here's the rule. One or two known jobs, like sending emails: an outbox and a worker are enough. Several teams that need the same facts, or a need to replay: that's what a log is for. Five readers, four teams and a replay put StayHub on the log side.
One trap to avoid. Don't use the log for questions that need an answer now. Publishing check availability, then waiting for a reply event, is a slow phone call in disguise. When you need the answer to continue, call directly.
08Your job as lead
You ask Claude Code to move Loyalty onto the bookings topic. It shows you the new reader and adds: offsets are committed automatically, so each event is processed once.
That sentence is the Thursday bug. Committing the bookmark automatically doesn't stop a second delivery. Ask for the event id to be saved in the same transaction as the points, and for a test that delivers the same event twice and finds the points added once.
For any Kafka change, ask four questions. Which steps leave the checkout, and which must still make the guest wait? What's the key, and what must stay in order? What happens when this reader gets the same event twice? And how far behind may it fall before someone is alerted?
Let's recap. A chain of calls ties checkout to its slowest neighbour. An event log is a logbook: write each fact once, and every group reads with its own bookmark. Partitions let readers share the work, the key keeps one booking in order, and every reader must be safe to read twice. Next: the payment provider stops answering mid-checkout, and nobody knows if the guest was charged.
Check yourself
0/10 answered1Booking saves the room, then calls Email, Analytics, Loyalty, Notifications and Fraud in a row, waiting for each. Loyalty is overloaded. Why do bookings fail?
2In the logbook picture, what is a consumer group's offset?
3Loyalty is down for 40 minutes, with the event log in place. What happens?
4The bookings topic has 12 partitions. To clear a backlog, you run 16 copies of the Analytics service in its group. How many do useful work?
5The hotel app sometimes shows a booking as cancelled before it was created. Producers send events with no key. What's the fix?
6After a loyalty deploy, about 300 guests got double points. What happened?
7Claude Code's loyalty reader adds points for each event and says: "Offsets are committed automatically, so each event is processed once." What do you ask for?
8Why does StayHub keep events for seven days instead of deleting them once read?
9A small team has one background job, sending confirmation emails, at about one event a second. Claude Code proposes a managed Kafka cluster. What do you answer?
10A service publishes "check availability", then waits for a reply event before showing a price. What's wrong?
Save your progress — create a free account
Free. No card. Your finished lessons follow you to any device, and you can ask questions in the community.
Create my free accountEvery lesson is free while we build. Lock in €9/month for later.
The first 100 members keep the founding price for as long as they stay. Reserving is free: no card, nothing is charged.
See the founding offer