ADR 0005: the outbox relay, its attempt cap and the CloudEvents encoder
Status
Accepted (2026-10-23).
Context
Every state change appends its events to a transactional outbox (ADR 0002, Transactions). 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 claims up to a batch of unpublished rows in id order, sends them one at a time and marks a 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). The relay never dials at construction: a broker that is down at boot only makes passes fail. - Store.
postgres.Outbox.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 and commits the rows already sent.Appendencodes before it writes, so an event the encoder refuses rolls the whole unit of work back. - At-least-once. A crash between the send and the
UPDATErepublishes that row with the SAME CloudEventsid(minted at append and stored inoutbox_events.event_id); consumers deduplicate on it. - 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_events; the failure that reaches the cap is logged once at ERROR with 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 promise can reach the topic before the capped one, so consumers must not rely on per-key order across a capped row. - Sink.
kafka.RelaySinkwrites each message to the topic it names with one writer that has no fixed topic,RequireAllacks, theHashbalancer on the key andAllowAutoTopicCreation. The first write to a new topic can fail withUnknownTopicOrPartition; that is an ordinary send failure, the row keeps its place and the next pass publishes it./readyztherefore never asks for topics (ADR 0006). - Encoder.
kafka.Encoderbuilds every envelope throughinternal/adapters/kafka/cloudevents(ADR 0004). Integration events go ONLY toretailnetwork.delivery-promise.events, analytics-only events ONLY toretailnetwork.delivery-promise.analytics. The payloads are pinned by golden exact-JSON tests (internal/adapters/outbound/kafka/testdata, one file per event and per variant, e.g.PromiseBroken_late,PromiseBroken_lost,PromiseMade_nominal).
Consequences
- A write is accepted while Kafka is down; the rows wait in
outbox_eventsand leave in id order when the broker is back. PromiseBroken.cause(LATE,LOST,RETURNED) is part of the golden files; a change of the payload fails them.