StoicBuilder

HandbookXV the PantheonEvent-driven design

XVthe Pantheon, Design the system

Event-driven design

1 min readUpdated 28 November

In plain words

In event-driven design one part of the system publishes an event, such as subscription.cancelled, and any number of other parts react to it, so the publisher doesn't need to know who is listening.

Where you see it

When a Harbor customer cancelled, billing used to call deploys, email, analytics and the CRM one by one, and a slow CRM made cancelling slow. Now billing publishes subscription.cancelled, and each of the others consumes it at its own pace.

modules/billing/cancel.tsserver
await db.$transaction([ db.subscription.update({ where: { id }, data: { status: "cancelled" } }), db.outbox.create({ data: { type: "subscription.cancelled", payload: { id, accountId } } }),]);// a relay reads unsent outbox rows, publishes them, then marks them sent
The outbox pattern: the change and the event commit together, so you never cancel without announcing it, or announce a cancel that rolled back.

What goes wrong

  1. Publishing outside the transaction

    Saving to the database and then publishing to the broker can fail between the two steps, leaving a change nobody hears about. Write the event to an outbox table in the same transaction and publish from there.

  2. Consumers that can't cope with duplicates or disorder

    Brokers usually deliver at least once, and events can arrive out of order. Consumers should skip events they've already processed and check versions or timestamps before applying one.

  3. Nobody can see the whole flow

    With events, the path of one cancellation is spread across five consumers and no single file shows it. Keep a catalogue of events with owners and schemas, and trace them end to end.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Replace the direct calls billing makes after a cancellation with one subscription.cancelled event, written through an outbox table in the same transaction. Make every consumer idempotent by recording processed event IDs, version the event's schema, and carry trace context so one cancellation can be followed across consumers.

Quick check

Sign in to earn +10 XP for it

Harbor's billing saves a cancellation, then publishes an event to the broker. The process crashes between the two steps. What happens?

Got a prompt that touches Event-driven design?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.