Skip to content
Lesson 32

Event-driven architecture

Decide first, then announce. Learn how events let checkout stop waiting for everyone, and the two rules that keep announcements from getting lost or counted twice.

The lesson in text

Click a paragraph to hear it.

01Checkout waits for everyone

Wednesday afternoon. Maya brings the checkout numbers. For one guest in twenty, paying for a booking takes 2.8 seconds or more. And about 50 bookings a day fail after the guest's card was already charged.

Here's why. When a guest pays, Booking does six things in a row, and waits for each one. Hold the room. Charge the card. Create the invoice. Send the email. Add loyalty points. And tell the hotel's own booking system, called a channel manager.

That last one is slow and often fails. When it times out, the whole checkout fails, even though the card was charged and the room was held. Fifty guests a day get an error and a refund.

In this lesson, you'll learn how to make checkout wait only for what matters: the difference between telling and announcing, and the two rules that keep announcements from getting lost.

02Telling or announcing

Think of a hotel front desk. The manager can tell housekeeping: clean room 214, now. That's one message, to one person, who can say no: we're short of staff.

That's a command: a request to one receiver to do something. The sender waits to hear if it worked.

Now the desk picks up the staff radio and says: the guest in 214 has checked out. Housekeeping goes to clean. The minibar team checks the fridge. Billing closes the bill. The desk doesn't wait for any of them.

That's an event: an announcement of something that already happened. It's written in the past tense, booking confirmed, and it doesn't say who should do what. Each listener decides for itself.

Command or event?
CommandEvent
At the front desk"Clean room 214, now."On the radio: "The guest in 214 has checked out."
Written asAn order: do thisA fact, in the past tense: this happened
Goes toOne receiverAnyone who listens
Can be refused?Yes: the receiver can say noNo: it already happened
The senderWaits to hear if it workedWaits for no one
StayHub exampleHold the room, charge the cardBooking confirmed, guest registered
A name like “send welcome email” is a command dressed up as an event: it still tells someone what to do. Announce the fact instead, and let each listener decide.

Building around these announcements is called event-driven architecture. One part publishes a fact; any part that cares subscribes to it.

03Decide, then announce

Back to checkout. Of the six steps, only two decide whether a booking exists: holding the room and charging the card. The guest needs that answer now. Everything else is a reaction to a booking that already exists.

So Booking keeps those two steps in the checkout, and waits for them. Then it announces: booking confirmed. Invoicing, email, loyalty and the hotel sync each hear it, and do their work on their own time.

Checkout before and after
Before: six in a row
  1. 1. Hold the room30 ms
  2. 2. Charge the card400 ms
  3. 3. Create the invoice250 ms
  4. 4. Send the email300 ms
  5. 5. Add loyalty points80 ms
  6. 6. Tell the hotel's channel manager500 ms, often seconds, often fails
1 guest in 20 waits 2.8 s or more
After: decide, then announce
  1. 1. Hold the room
  2. 2. Charge the card
  3. 3. Announce: booking confirmed
Invoice, email, loyalty and hotel sync listen and do their work on their own time. The hotel sync retries for up to 6 hours, then alerts Hotel Partners.
The guest waits about 0.5 s
Before: the guest waited for all six, and a failure in the last one failed the whole checkout after the card was charged (about 50 bookings a day). After: the guest waits for the two steps that decide; the rest react to “booking confirmed”.

The guest now waits about half a second instead of 2.8. And if the hotel's system is down, nothing fails: the hotel sync simply tries again later.

There's a second gift. Before, Booking had to know every part that cared about a booking. Now it only announces. When a new team wants to react, say to send a text message, it subscribes, and Booking doesn't change at all.

So checkout uses both. Holding the room and charging the card are commands: Booking asks, waits, and the answer can be no. Booking confirmed is the event: it's only said once the answer was yes, and nobody can refuse it.

04Catching up later

This has a price. For a few seconds, the parts disagree. The booking exists, but the email hasn't left yet, and the loyalty points aren't there. The system catches up a little later. That's called eventual consistency.

So the page must say the truth: booking confirmed, your email will arrive in a minute. It reads that from Booking itself, not from the parts that are still catching up.

And some things can never be eventual. Never sell the last room twice. Never confirm before the payment is known. Those are decisions, and they stay in the checkout, where the guest waits for the answer.

The risky reactions get an owner. The hotel sync keeps trying for up to six hours, then alerts the Hotel Partners team. Something that can fail quietly needs someone who hears when it does.

05The lost announcement

The first version had a subtle bug. It saved the booking in the database, and then, in a second step, published the announcement.

One Thursday at 18:20, a deploy restarted the servers. For 9 bookings, the restart came exactly between the two steps. The bookings were saved. The announcements were never sent.

So those 9 guests got no email and no points, and their hotels never heard about them. 2 hotels sold the same room to someone else. Two guests with valid confirmations arrived at a desk with no room for them.

Save, then announce: the gap
Booking
Database
Announcements
Hotel sync
1. save the booking ✓
2. publish — never happens: servers restarted
nothing to hear: the hotel never knows
Two separate writes. A restart between them (a deploy at 18:20) saved 9 bookings that were never announced: no email, no points, and 2 hotels resold the room.

Saving in one place, then announcing in another, is two separate writes. Any crash between them leaves the two disagreeing, and a retry can't help a program that has already died.

06The outbox

Back at the front desk. The clerk writes the check-out and the announcement in the same logbook, in one go. A colleague reads the new lines from the book out on the radio. If the clerk faints, the line is still in the book.

That's the outbox. Booking saves the booking and the announcement in the same database, in one single save. Either both are there, or neither is. A small helper then publishes every announcement that hasn't been sent yet.

The outbox: save the announcement with the change
One single save (a transaction)
bookings: LIS48213, suite-12, 12–15 Novthe booking
outbox: BookingConfirmed, id evt-9d2f, not sentthe announcement
Outbox table
Helper
Listeners
any unsent lines?
BookingConfirmed (id evt-9d2f)
mark as sent
One save holds both rows, so they can't disagree. The helper may send a line twice after a crash, so every listener notes the ids it has handled and skips repeats. Alert if the oldest unsent line is more than a minute old.

But the helper can crash just after sending and before ticking the line off, and send it again. So listeners may hear the same announcement twice. Each announcement gets an id, and every listener notes the ids it has handled and skips repeats. No second email, no double points.

Last, measure the delay. If the oldest unsent announcement is more than a minute old, someone gets an alert. Work that happens later can fail silently, so watch it, or your hotels will tell you first.

07Where announcements travel

You'll hear about tools like Kafka, created at LinkedIn, that carry billions of events between many separate systems. They're powerful, and they need a team to run them.

StayHub is one app on one database, with about 2,500 bookings a day. For that, a table in the database and the workers it already runs are enough. When it splits into services one day, the helper can publish to a tool like Kafka, and the listeners barely change.

One trap to avoid: commands dressed up as events. An announcement called send welcome email still tells someone what to do. Announce the fact, guest registered, and let each listener decide.

08Your job as lead

You ask Claude Code to make checkout faster. It replies: done, events are now asynchronous, and checkout dropped to half a second. You open the change. It saves the booking, and then, as a second step, publishes the announcement.

Fast, and exactly the bug that lost 9 bookings. Don't ask for a retry. Ask for an outbox: the announcement saved in the same transaction as the booking, with an id, and a helper that publishes it.

For any event-driven change, ask four questions. Which steps decide, and stay in the request? What happens if this listener hears the same event twice? What does the guest see while the rest catches up? And how will we know if announcements stop flowing?

Let's recap. A command tells one receiver what to do. An event announces what already happened, and anyone can listen. Decide first, then announce. Save the announcement with the change, in an outbox. And make every listener safe to hear it twice. Next: how to keep your core rules safe from the tools around them.

Check yourself

0/10 answered
  1. 1Which of these is an event?

  2. 2In the front desk picture, what is an event?

  3. 3Checkout holds the room, charges the card, then calls invoicing, email, loyalty and the hotel's system, waiting for each. What is the best change?

  4. 4Which step must stay synchronous, inside the checkout?

  5. 5Right after booking, a guest sees no loyalty points yet. They appear a few seconds later. What is this?

  6. 6The code saves the booking, then publishes the event in a second step. A deploy restarts the servers in between. What happens?

  7. 7What does the outbox do?

  8. 8Users see two confirmation emails for one booking after a crash. What was missing?

  9. 9Claude Code says: "Events are now async, checkout dropped to half a second." Its code saves the booking, then publishes. What do you ask for?

  10. 10StayHub is one app on one database, with about 2,500 bookings a day. Where should its events travel today?

Free account

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 account
Founding members

Every 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

Questions from the community

All questions →