ADR 0005: the outbox relay, its attempt cap and the CloudEvents encoders
Status
Accepted (2026-10-23).
Context
ADR 0003 decided that every integration event is published through the
transactional outbox: the use case saves the aggregates and appends the encoded
events in ONE Postgres transaction. Something has to drain the rows to Kafka.
The harness template (TestOutboxRelaysPassAttemptCap) forbids a relay that one
poison row can block forever.
Decision
- Relay.
internal/adapters/outbound/outboxholdsNewOutboxRelay(store, sink, opts...). A pass (RelayOnce) asks the store to claim up to a batch of unpublished rows in id order, sends them one at a time and marks each row published only after the send returned nil.Runrepeats until its context is cancelled: a full batch is followed at once by another pass, anything else sleepsOUTBOX_RELAY_INTERVAL(default 1 s); a failed pass is logged and retried after the interval. The relay never dials: a broker that is down at boot only makes passes fail. - Store.
postgres.OutboxRepo.Drainclaims withFOR UPDATE SKIP LOCKED(two replicas never claim the same row), stops at the first send failure (a later event of an aggregate never overtakes an earlier one that is being retried), recordsattemptsandlast_erroron the failed row, commits the rows already sent and returns an*outbox.SendFailureError. A cancelled context still commits what was sent. - Delivery is at-least-once. A crash between the send and the
UPDATErepublishes that row with the SAME CloudEventsid(minted once, at append, and stored inoutbox_events.event_id), so consumers deduplicate on the id. - Attempt cap.
WithMaxAttempts(n)(envOUTBOX_MAX_ATTEMPTS, default 20,0= unlimited, a negative or unparsable value falls back to 20 with a warning) excludes a row from future claims once it failedntimes. The row stays inoutbox_eventswithattemptsandlast_error; the failure that reaches the cap is logged once at ERROR with the row id, topic, type and key; rows behind it, including rows with the same key, overtake it. An operator re-arms it withUPDATE outbox_events SET attempts = 0 WHERE id = .... Trade-off: after the cap a later event of the SAME aggregate can reach the topic before the capped one, so consumers must not rely on per-key order across a capped poison row. The cap is deliberate at the composition root:cmd/apipassesWithMaxAttempts(...)inline. - Sink.
kafka.RelaySinkwrites each message to the topic it names with onekafka.Writerthat has NO fixed topic,RequireAllacks, theHashbalancer on the key, a 10 ms batch timeout (the default 1 s would cap a relay that sends one row per call at one event per second) andAllowAutoTopicCreation. - First write to a new topic. The broker creates
retailnetwork.node-topology.eventson the first write, and that first write can fail withUnknownTopicOrPartitionorLeaderNotAvailable. This is an ordinary send failure: the row keeps its place, the next pass retries it and publishes the same id./readyztherefore never asks for topics (ADR 0006). - Encoders.
kafka.Encoderimplementsoutbox.Encoderfor the nine events and builds every envelope throughinternal/adapters/kafka/cloudevents:typecom.retailnetwork.logistics.node-topology.<entity>.<Event>(entitynode,lane,servicearea,region),source/retailnetwork/node-topology,subjectand Kafka key = the aggregate id,dataschemaurn:retailnetwork:node-topology:events:<Event>:v1, Kafka headercontent-type: application/cloudevents+json; charset=UTF-8. All nine events go toretailnetwork.node-topology.events; nothing is analytics-only. The payloads are the ones ofapis/asyncapi.yaml, pinned by golden exact-JSON tests.operatingHoursByDaylists weekdays Monday first so the bytes are stable.
Consequences
- A poison row costs at most
OUTBOX_MAX_ATTEMPTSpasses of delay for the rows behind it, never a permanent block. - Postgres integration tests prove Drain (order, failure recording, the cap and re-arming, concurrent drains); a Kafka integration test proves that the outbox rows become exactly the CloudEvents on the topic.
Addendum 2026-11-13: current state
Item 7 names nine events. kafka.Encoder now encodes ten: lane.LaneCostDeclared was
added by ADR 0014 (internal/adapters/outbound/kafka/encoder.go, case lane.LaneCostDeclared).
All ten go to retailnetwork.node-topology.events; nothing is analytics-only. The relay,
the attempt cap and the envelope rules are unchanged.