CodingNeed.

Production engineering workshop · 55 MIN

System design · Deliver events with a transactional outbox

Write business state and the intent to publish in one transaction.

A database commit and a message broker publish are separate failure domains. An outbox stores the event beside the business update. A relay claims pending events, publishes them and marks delivery. It can crash after publish but before marking, so consumers must tolerate duplicates. This gives at-least-once attempts, not exactly-once effects. For low-level design, separate repository, relay and consumer interfaces and make retries observable.

First reproduce the normal path. Then force a failure at each boundary and inspect what remains true.

Read the example

-- PostgreSQL teaching schema: use in a disposable database.
CREATE TABLE orders (id uuid PRIMARY KEY, amount_cents bigint NOT NULL CHECK(amount_cents >= 0));
CREATE TABLE outbox (
 id uuid PRIMARY KEY, order_id uuid NOT NULL REFERENCES orders(id),
 payload jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(),
 sent_at timestamptz
);
CREATE INDEX outbox_pending ON outbox(created_at,id) WHERE sent_at IS NULL;
BEGIN;
INSERT INTO orders VALUES ('00000000-0000-4000-8000-000000000001',500);
INSERT INTO outbox(id,order_id,payload) VALUES (
 '00000000-0000-4000-8000-000000000002',
 '00000000-0000-4000-8000-000000000001',
 '{"type":"OrderCreated","amountCents":500}'
);
COMMIT;
-- Relay claim (inside a short transaction; design a lease for publication outside it):
SELECT id,payload FROM outbox WHERE sent_at IS NULL
ORDER BY created_at,id FOR UPDATE SKIP LOCKED LIMIT 50;
-- Do not claim exactly-once delivery: relay crash can publish an event twice.
Check the expected output
Review the behavior in the stated project environment.

Your challenge

In a disposable PostgreSQL database, create orders and outbox tables. Commit an order and its event together. Implement a relay with bounded batches and retry backoff, plus an inbox UNIQUE(event_id) in the consumer’s business transaction. Draw the HLD and list relay/consumer interfaces for the LLD.

Solution cost: Indexed batch selection plus broker latency; throughput depends on batching and contention. time · O(backlog); retention and archival must be planned. space

Common trap

Marking sent before publish can lose events. Holding row locks across slow broker I/O damages throughput.

Study the project implementation
-- PostgreSQL teaching schema: use in a disposable database.
CREATE TABLE orders (id uuid PRIMARY KEY, amount_cents bigint NOT NULL CHECK(amount_cents >= 0));
CREATE TABLE outbox (
 id uuid PRIMARY KEY, order_id uuid NOT NULL REFERENCES orders(id),
 payload jsonb NOT NULL, created_at timestamptz NOT NULL DEFAULT now(),
 sent_at timestamptz
);
CREATE INDEX outbox_pending ON outbox(created_at,id) WHERE sent_at IS NULL;
BEGIN;
INSERT INTO orders VALUES ('00000000-0000-4000-8000-000000000001',500);
INSERT INTO outbox(id,order_id,payload) VALUES (
 '00000000-0000-4000-8000-000000000002',
 '00000000-0000-4000-8000-000000000001',
 '{"type":"OrderCreated","amountCents":500}'
);
COMMIT;
-- Relay claim (inside a short transaction; design a lease for publication outside it):
SELECT id,payload FROM outbox WHERE sent_at IS NULL
ORDER BY created_at,id FOR UPDATE SKIP LOCKED LIMIT 50;
-- Do not claim exactly-once delivery: relay crash can publish an event twice.

Further reading: Official documentation

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.