ADR 0005: outbox relay, attempt cap, queue sweep and shutdown order
Status
Accepted (2026-10-23).
Context
order-sourcing writes ten event types as a side effect of every transition (ADR 0004) and must never lose one or publish
one for a change that rolled back. It also needs a timer: a QUEUED assignment is released at releaseAt and the others
are re-optimised against current capacity (ADR 0002 A18). Both run in the one binary cmd/api, next to nine consumers.
The alternatives were publishing from the use case after the commit (loses an event on a crash), a separate relay
deployment (a second image and chart for a single table) and a database trigger or change-data-capture (an extra
platform component for a study service).
Decision
- Transactional outbox. A use case saves the assignment and appends the encoded events in ONE Postgres
transaction (
ports.UnitOfWork, tableoutbox_events). The CloudEventsidis minted at append and stored with the row, so a retry or a redelivery carries the same id. - In-process relay, always on.
internal/adapters/outbound/outbox/relay.godrains unpublished rows in id order everyOUTBOX_RELAY_INTERVAL(default 1 s) and writes them toretailnetwork.order-sourcing.eventsor.analyticswith acks from all replicas. Delivery is at-least-once; consumers dedupe on the event id. - Attempt cap. A row that fails
OUTBOX_MAX_ATTEMPTStimes (default 20,0= unlimited) is excluded from later passes, logged once at ERROR and kept withattemptsandlast_error, so one poison row cannot stall the table. An operator re-arms it withUPDATE outbox_events SET attempts = 0 WHERE id = .... While a row is skipped, later events of the same assignment can overtake it. The composition root passesWithMaxAttemptsinline (TestOutboxRelaysPassAttemptCap). - Queue sweep.
usecases.SweepQueueruns everySWEEP_INTERVAL(default 15 s) in the same process: it releases the assignments whosereleaseAthas passed (ReleaseToNode) and re-optimises the others (ReoptimiseAssignment), page by page. A sweep touches an assignment through the same use cases as REST and Kafka, so the version check and the outbox rules are the same; a transition another actor won first is counted as skipped, not as a failure. - Shutdown order.
/readyzflips to 503, the drain delay passes, HTTP drains, then the consumers and the sweep stop, and the relay stops last so that every event committed during the drain is still published. A worker that returns on its own makes the process exit non-zero.
Consequences
A write is accepted while Kafka is down and published when it is back; /readyz reports the broker as unavailable so
the pod leaves the load balancer, but /healthz stays 200 and the pod is not killed. The sweep and the relay run once per
replica: concurrent sweeps are safe because every transition is an optimistic-version save, and concurrent relays are
safe because rows are claimed with FOR UPDATE SKIP LOCKED in internal/adapters/outbound/postgres/outbox.go.
The cost is one table that needs monitoring (published_at IS NULL, attempts > 0) and a runbook, in docs/runtime.md.