ADR 0005: the outbox relay, its attempt cap and the CloudEvents encoder
Status
Accepted (2026-10-23).
Context
ADR 0003 decided that every integration event is published through the transactional
outbox: the recompute use case saves the scorecards 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, logger, 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.RelayStore.Drainclaims withFOR UPDATE SKIP LOCKED(two replicas never claim the same row), stops at the first send failure (a later event of a scorecard 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. The write side (postgres.OutboxRepo.Append) encodes before it writes, so an event the encoder refuses rolls the whole unit of work back. - 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 scorecard can reach the topic before the capped one, so consumers must not rely on per-key order across a capped poison row.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 a topic on its first write, and that
first write can fail with
UnknownTopicOrPartitionorLeaderNotAvailable. 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). - Encoder.
kafka.Encoderimplementsoutbox.Encoderfor the two events and builds every envelope throughinternal/adapters/kafka/cloudevents:typecom.retailnetwork.partner.node-performance.scorecard.<Event>,source/retailnetwork/node-performance,subjectand Kafka key = the scorecardId,dataschemaurn:retailnetwork:node-performance:<events|analytics>:<Event>:v1, Kafka headercontent-type: application/cloudevents+json; charset=UTF-8.NodeScorecardUpdatedgoes ONLY toretailnetwork.node-performance.eventsandNodeThresholdBreachedONLY toretailnetwork.node-performance.analytics(selected byAnalyticsOnly()). The payloads are the ones ofapis/asyncapi.yaml, pinned by golden exact-JSON tests (internal/adapters/outbound/kafka/testdata).
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 right topics.
Addendum 2026-11-13: current state
The encoder encodes three events, not two: NodeTierEligibilityChanged goes to retailnetwork.node-performance.events like NodeScorecardUpdated (internal/adapters/outbound/kafka/encoder.go:131, ADR 0013). NodeThresholdBreached is still the only one selected by AnalyticsOnly() for the analytics topic.