Skip to content
Ashraful Islam
Menu

Backend software engineer · Kuala Lumpur

All notes

Note · 24 November 2026 · 6 min

The outbox is not optional

Every dual write is a silent failure waiting for a quiet Tuesday. One table and a worker with leases fixes it — at the cost of at-least-once delivery.

PostgreSQL · reliability · queues

The pattern is old enough to be boring, and it is still the thing I reach for first in any system that has to notify someone. The failure it prevents is not exotic: you write a row, then you call a third party. Between those two statements there is a window, and in that window the process can die, the network can drop, or the third party can return a 500 that you log and forget.

The row exists. The patient was never told. Nothing in the system is broken enough to alert, and the customer finds out at the clinic.

The shape

Instead of calling the provider, the request writes the intent to an outbox table in the same transaction as the business row. Either both commit or neither does — one atomic step, no window. A separate worker claims pending rows, does the side effect, and marks them done.

Claiming is where naive implementations break. If two workers select the same pending rows, the patient gets two SMS. The claim has to be atomic and time-boxed: pick rows with SKIP LOCKED, set a lease expiry, and let an expired lease return the row to the queue.

Claiming work without a queue service
WITH claimed AS (
  SELECT id FROM outbox
   WHERE status = 'pending'
      OR (status = 'leased' AND leased_until < now())
   ORDER BY created_at
   LIMIT 20
   FOR UPDATE SKIP LOCKED
)
UPDATE outbox o
   SET status = 'leased',
       leased_until = now() + interval '2 minutes',
       attempts = attempts + 1
  FROM claimed c
 WHERE o.id = c.id
RETURNING o.*;

What it actually costs

At-least-once. A worker can do the side effect and then die before marking the row done, so the effect happens twice. That is not a bug in the outbox; it is the contract. Every consumer has to be idempotent — an SMS provider keyed on a request id, an email that tolerates a duplicate, a push notification that collapses.

A second process. There is now a worker to deploy, monitor, restart and alert on. When it stops, nothing fails visibly — the outbox just grows and customers stop receiving things. The queue depth is the health check that matters, and it has to be an alert, not a dashboard nobody opens.

A dead-letter path. After N attempts, a row stops retrying and waits for a human. Without that, a permanently failing row either blocks the queue or retries forever and burns your provider quota.

Ordering. Per-entity order is not guaranteed once you have more than one worker. If "appointment confirmed" must arrive before "appointment cancelled", the outbox needs a partition key and a single consumer per key — or the events need to be state assertions rather than deltas.

Why I still use it

Because the alternative is a lie told by a try/catch. You cannot make a database transaction and an HTTP call atomic. You can only decide where the inconsistency lives: in the outbox, where a worker will fix it, or in the gap between systems, where nobody will.

The outbox also gives you something the direct call never does — a record. Every notification the system intended to send is a row, with attempts, errors and timestamps. When someone asks whether the patient was told, the answer is a query, not a guess.