36. Outbox relay attempt cap
Status
Accepted (2026-10-12, ecosystem gap fix D12, widened to every relay in
the fleet). Amends the per-key ordering consequence of
ADR 0018; everything else in ADR 0018 (one
row per event and topic, written in the use case's transaction, drained in
id order, FOR UPDATE SKIP LOCKED) is unchanged.
Context
The outbox relay (ADR 0018) sends rows one
at a time in id order and stops a pass at the first failed row, so a later
row never overtakes an earlier one. It had no attempt cap: a failed row was
retried every OUTBOX_RELAY_INTERVAL, forever.
That is right for a broker outage, which ends. It is wrong for a poison
row, one the broker will never accept: a message over the broker's size
limit, a topic that does not exist and cannot be created, a value the sink
rejects every time. Such a row fails every pass, every pass stops at it, and
every row behind it, for every site and both topics
(warehouse.facility.events and warehouse.facility.analytics), waits
forever. Nothing pages: the relay logs outbox relay pass failed once per
pass and the process stays Ready. Downstream contexts (inventory-storage,
wes-work-planning, network-inventory-planning, the analytics projector) stop
hearing about any layout change.
The fleet already settled this in its inventory reference implementation: an option on the relay that excludes a row after N failed sends, with an operator re-arm. This ADR adopts the same convention here.
Decision
- The relay takes an attempt cap.
postgres.WithMaxAttempts(n): withn > 0, a row whoseattemptshas reachednis excluded from the relay's claim (AND attempts < $max). The relay's own zero value stays0(unlimited), so the option is always an explicit choice. - Every composition root that builds a relay passes it.
cmd/facility(the only binary here that runs a relay;cmd/mcpwrites no outbox rows and relays nothing) readsOUTBOX_MAX_ATTEMPTSand passes it aspostgres.WithMaxAttempts. Default20(postgres.DefaultOutboxMaxAttempts);0means unlimited, the old behaviour. An invalid or negative value logs a WARN and uses the default, so a typo cannot silently remove the cap. The Helm chart exposes it asconfig.outboxMaxAttempts, rendered into the api ConfigMap. - The excluded row is kept, not moved. It stays in
outbox_eventswithpublished_at IS NULL, itsattemptsand itslast_error, for inspection. The housekeeping sweeper (ADR 0026) deletes only published rows, so it never removes an excluded row. There is no DLQ topic: a message the broker rejects would most likely be rejected on a DLQ topic of the same broker too, and the table already is the inspectable store. - The failure that reaches the cap is logged once at ERROR, after the
attempt is committed: message
outbox relay: row reached the attempt cap and is excluded from future claims; ...withoutbox_id,topic,event_type,key,attempts,max_attemptsanderror. Failures below the cap keep the existingoutbox relay pass failedlog. - Re-arming is an operator action. After fixing the cause,
UPDATE outbox_events SET attempts = 0 WHERE id = <id>;makes the row claimable on the next pass (runbook, "Outbox relay"). - Sensor. The architecture fitness test
TestOutboxRelaysPassAttemptCapfails when a non-test file undercmd/callsNewOutboxRelaywithout aWithMaxAttempts(...)argument;TestOutboxRelayCapSensorFailsOnBadFixturesproves it can fail.
Consequences
- Per-key ordering is no longer absolute. Once a row is excluded, later
rows overtake it, including rows with the same key (the same site code
or aggregate identity, ADR 0032):
a consumer can see a later event for an aggregate whose earlier event never
arrived, or receive the earlier event late once it is re-armed. This is the
deliberate trade: the cap exists so one poison row cannot stop the whole
relay, for every key, forever. Consumers are already idempotent on the
CloudEvents
idand must already tolerate redelivery (ADR 0018); a re-armed row is a late delivery of the sameid. - Ordering among rows that are not excluded is unchanged: within a pass the relay still stops at the first failure, so a transient failure below the cap never lets a later row overtake.
- With the defaults (1 s interval, cap 20) a row that fails every send is
excluded after roughly 20 seconds of continuous failure. A broker outage
longer than that also excludes the rows at the head of the queue, one per
pass, as each reaches the cap; each is logged at ERROR and must be
re-armed. Raise
OUTBOX_MAX_ATTEMPTS(or set0) where long broker outages are expected and a stalled relay is preferable to manual re-arming. - Excluded rows are listed by
SELECT id, topic, event_type, key, attempts, last_error FROM outbox_events WHERE published_at IS NULL AND attempts >= 20;(replace20with the configured cap).
Verification
go test -tags=integration ./internal/adapters/outbound/postgres/ -run TestOutboxRelay -count=1:TestOutboxRelay_MaxAttempts_PoisonRowExcludedThenGoodRowPublishes,TestOutboxRelay_MaxAttempts_ReArmedRowIsClaimedAgain,TestOutboxRelay_NoCap_PoisonRowBlocksLaterRows.go test ./cmd/facility/ -run 'TestOutboxMaxAttempts|TestDefaultOutboxMaxAttemptsIsTwenty'.go test ./internal/architecture/ -run OutboxRelay.bash charts/facility-layout/tests/render_test.sh(default20,config.outboxMaxAttemptsrendered asOUTBOX_MAX_ATTEMPTS).