Emit events without losing them
Continues from the last build: Writes are transactional, idempotent, and the order state machine cannot be skipped.
Last rung you closed the hole where two shoppers could buy the last unit of the same product.
What you'll build
The API commits order state and its outbound event in one atomic write. A relay worker turns undispatched outbox rows into signed, retried webhook deliveries without ever losing one, and a documented receiver pattern makes duplicate deliveries harmless.
See how we teach, before you sign up
You don't just get code dumped on you. Every starter file and every solution is explained line-by-line, in plain English. Here's one real file from this project:
export const outboxEvents = pgTable('outbox_events', {
id: uuid('id').primaryKey().defaultRandom(),
aggregateType: text('aggregate_type').notNull(),
aggregateId: uuid('aggregate_id').notNull(),
topic: text('topic').notNull(),
payload: jsonb('payload').notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
dispatchedAt: timestamp('dispatched_at'),
});
export const deadLetterEvents = pgTable('dead_letter_events', {
id: uuid('id').primaryKey().defaultRandom(),
eventId: uuid('event_id').notNull(),
payload: jsonb('payload').notNull(),
lastError: text('last_error').notNull(),
failedAt: timestamp('failed_at').notNull().defaultNow(),
});Reading this file
dispatchedAt: timestamp('dispatched_at'),Nullable, no default, this is the flag the relay's poll query filters on.payload: jsonb('payload').notNull(),Stores the exact event body that will be signed and sent, keep it a flat, versioned shape per topic.eventId: uuid('event_id').notNull(),Links a dead-lettered delivery back to its originating outbox row for replay or investigation.createdAt: timestamp('created_at').notNull().defaultNow(),The relay's poll query orders by this so old events are enqueued before newer ones.
The core new tables for this rung. dispatchedAt starts null and the relay flips it once, never resets it, so a row is never enqueued a third time under normal operation.
That's 1 of 7 explained code blocks in this single project.
The build, milestone by milestone
- 1
Prove fire-and-forget loses events
5 guided stepsIf you cannot reproduce the dual-write problem, you cannot prove you fixed it. Every later milestone's verify step compares against this baseline behavior.
- 2
Add the outbox table and write it in the same transaction
5 guided stepsThe outbox pattern turns a distributed problem, keep two systems in sync, into a local one, keep two rows in the same database in sync, which Postgres already guarantees you for free inside a transaction.
- 3
Build the relay worker
6 guided stepsThe relay is the bridge between a database row and a queue job. Polling on an interval, rather than trying to get a trigger to fire synchronously, keeps the relay simple and makes it safe to restart at any point without losing rows.
- 4
Deliver webhooks like a grown-up
5 guided stepsA merchant's endpoint will be down sometimes, that is normal operations, not a bug. Retrying with backoff gives it time to recover without hammering it, signing proves the payload actually came from you, and a dead letter path means a permanently broken endpoint produces a page, not silence.
- 5
Dedupe on the consumer side
6 guided stepsThis is the piece that turns honest at-least-once delivery into an exactly-once effect for the merchant. It is also the single most common interview question about this whole rung, how do you not lose events, and the honest answer is you do not prevent duplicates, you make them harmless.
What's inside when you start
You'll walk away with
This is portfolio-grade. Build it free.
Sign up to unlock every milestone step-by-step, the code skeletons, full reference solutions, and checkable tasks, with your progress saved as you build.
Start building