The outbox closes the gap between committing PostgreSQL state and publishing the corresponding Kafka event.
Write path
The domain transaction writes the resource change and an outbox row atomically. If the transaction fails, neither is visible. If it commits, publication intent remains durable even when Kafka is unavailable.
Claiming
Relay replicas claim pending rows with a processing lease and worker identity. Database claim tokens prevent stale owners from changing publication state, and an abandoned claim becomes eligible after its lease expires. An already accepted Kafka send can still duplicate when its database acknowledgement is lost.
Workflow claims share the total batch between initial delivery and replay, borrowing unused slots. A durable turn coordinates replicas, restarts and batch size one; explicit interleaving rotates first-send opportunities when batches are interrupted. The turn lock is released before broker I/O. Earlier unpublished same-key events, including dead or delayed failures, still block successors across both classes.
Publication outcomes
- Successful rows are marked published.
- Retryable failures increment attempt state and apply backoff.
- Rows that exhaust
OUTBOX_RELAY_MAX_ATTEMPTSare marked dead for inspection.
Cleanup
Ordinary published rows remain available for a configurable retention period,
then cleanup removes them in bounded batches. Pruning uses two candidate-limited
lanes: ordinary published events and COMPLETED container cleanup. A durable
turn, committed before lane work, alternates the preferred lane across replicas
and restarts; empty and failed calls still advance it. A busy reservation skips
the call (debug log), so aligned replica schedules can reduce throughput. Each call selects at most
OUTBOX_RELAY_CLEANUP_BATCH_SIZE candidate identities. The preferred lane is offered
half rounded up; the other lane receives the batch size
minus already charged candidates, and the
preferred lane gets at most one final borrowed operation. A failed lane is not
reentered in that call.
Each lane locks candidates before proof, deletes by candidate ID and commits independently. Cleanup requires an exact job, container, original-runtime and creation-intent receipt match; invalid job UUIDs and mismatched text intent fail closed. Unfinished container cleanup is not deletion evidence: publication or execution quarantine does not prove removal, so published cleanup remains retained until that matching original-instance receipt exists. The relay can republish unfinished work after lost delivery or Kafka retention expiry without granting a fresh cleanup failure budget.
A call may return a known committed delete count with an error when one lane
fails and another commits, in either order; an uncertain commit makes the count
a lower bound. The relay logs the known count with the error, and lane logs report
successful selections, deletions and proof mismatches. Failed lanes report their
charged allowance, zero known committed deletions and selected_known to
distinguish observed selections from unavailable counts. A COMPLETED row without exact
proof is corruption and is retained; it can block cleanup progress while ordinary
pruning continues. See cleanup recovery
for the corrupt-prefix diagnostics queries and redrive limits.
Successful initial cleanup publication schedules replay after
OUTBOX_RELAY_REPLAY_INTERVAL (default 5m). Successful replay waves double the
next interval up to OUTBOX_RELAY_REPLAY_MAX_INTERVAL (default 1h) and continue
at the cap. Durable round/due metadata survives restarts. Both settings require
whole milliseconds, 1ms <= interval <= maximum; zero selects defaults.
Transport retries stay within a wave, and lag data is not required. The cap
bounds scheduled waiting after a successful publication, not delivery under
overload, active leases/backoff, key blockers or outages. Failed database marks
or crashes after Kafka acceptance can cause earlier duplicate sends.
Administrative cleanup redrive resets/requeues the retained row under its expected
generation and atomically writes an authenticated actor/reason audit. Repeated
requests with an old generation do not duplicate that transition. The audit history
survives later outbox pruning. Accepted redrive resets pacing for its new generation. Publication DEAD and cleanup execution QUARANTINED
are distinct operational signals; both require operator repair and are excluded from automatic replay.
See cleanup recovery and cleanup metrics.
Scaling guidance
Tune batch size, poll interval, processing lease, Kafka latency, and PostgreSQL query latency together. A shorter poll interval cannot compensate for saturated Kafka or database connections.
Outbox relay repositoryinternal/repository/outboxrelay/outboxrelay.go