ADR 0005: the outbox relay, its attempt cap and the CloudEvents encoder
Status
Accepted (2026-10-23).
Context
Every integration and analytics event is published through the transactional outbox: a use case saves the aggregates and
appends the encoded events in ONE Postgres transaction (ports.UnitOfWork, ports.EventOutbox). Something has to drain the
rows to Kafka, and the harness (TestOutboxRelaysPassAttemptCap) forbids a relay that one poison row can block forever.
Decision
- Relay.
internal/adapters/outbound/outboxholdsNewOutboxRelay(store, sink, logger, opts...). A pass (RelayOnce) claims 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. It never dials at construction. - Store.
postgres.OutboxRepoclaims 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 and commits the rows already sent. - At-least-once. A crash between the send and the
UPDATErepublishes that row with the SAME CloudEventsid, minted once at append time and stored inoutbox_events.event_id. Consumers deduplicate on the id. - Attempt cap.
WithMaxAttempts(n)(OUTBOX_MAX_ATTEMPTS, default 20,0= unlimited) 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. 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.cmd/apipassesWithMaxAttempts(...)inline. - Sink.
outboundkafka.RelaySinkwrites each message to the topic the row names with onekafka.Writerthat has NO fixed topic,RequireAllacks, theHashbalancer on the key, a 10 ms batch timeout andAllowAutoTopicCreation. - Two topics.
outbox_events.topicisretailnetwork.linehaul-transportation.eventsfor integration events andretailnetwork.linehaul-transportation.analyticsfor the analytics-only ones (DepartureDeparted,DepartureArrived,LoadClosed). An analytics-only event is never written to the events topic and the reverse (ADR 0004;wire.Stream). - First write to a new topic. The broker creates the topic on the first write, and that first write can fail with
UnknownTopicOrPartition. It is an ordinary send failure: the row keeps its place and the next pass publishes the same id./readyztherefore never asks for topics (ADR 0006). - Encoder.
internal/adapters/kafka/wire.Encoderturns each domain event into the outbox message, building every envelope throughinternal/adapters/kafka/cloudevents:typecom.retailnetwork.logistics.linehaul-transportation.<entity>.<Event>with entitydepartureorload,source/retailnetwork/linehaul-transportation,subjectand Kafka key the aggregate id (forBookingMovedthetoDepartureId),dataschemaurn:retailnetwork:linehaul-transportation:<stream>:<Event>:v1, headercontent-type: application/cloudevents+json; charset=UTF-8. The payloads are those ofapis/asyncapi.yaml, pinned by golden exact-JSON tests (internal/adapters/kafka/wire/testdata/*.golden.json).
Consequences
- A poison row costs at most
OUTBOX_MAX_ATTEMPTSpasses of delay for the rows behind it, never a permanent block. - A write is accepted while Kafka is down; the rows wait in the table and publish when the broker returns (proved by the
Kafka-stopped subtest of
cmd/api/e2e_integration_test.go). - Postgres integration tests prove claiming, failure recording, the cap and re-arming; a Kafka integration test proves that the outbox rows become exactly the CloudEvents on the topic.