18. Transactional outbox + relay for atomic event publishing
Status
Accepted. Per-key ordering amended by ADR 0036 (outbox relay attempt cap).
Context
ADR-0009 replaced the never-drained Postgres events table with a direct
kafka.Publisher because a durable-but-undrained outbox is worse than a
working direct publisher: no event had ever reached a downstream consumer.
That fixed reachability, but reopened the atomicity gap the original outbox
existed to close: Repo.Save and Events.Publish are two independent
network calls (Postgres, then Kafka) with no shared transaction. A crash, a
broker hiccup, or a deploy between them either loses an event the aggregate
change implies happened, or (with retries) publishes one for a write that
never committed. inventory-storage, wes-work-planning,
workforce-management, and fulfillment-execution all Conform to this
service's Published Language (ADR-0009) and have no way to tell such a gap
apart from a real state change.
process-path-management already solved exactly this with a
ports.UnitOfWork port, a Postgres OutboxPublisher/OutboxRelay pair, and
an atomically helper in its use cases (its ADR 0003). This ADR adopts that
shape for facility-layout, adapted to two differences already fixed by prior
ADRs here: ports.EventPublisher.Publish takes one event at a time (not
variadic), and this service fans every event out to TWO topics — the
integration topic (ADR-0009) and the analytics topic (ADR-0010) — which the
outbox and relay must preserve without letting the two streams diverge.
Decision
-
ports.UnitOfWork— one new port,Execute(ctx, func(ctx) error) error, brackets a use case's repositorySaveandEvents.Publishcalls in one Postgres transaction. It is optional on every use case (a nilUnitOfWorkfalls back to running the wrapped function directly), so use cases work identically over the in-memory adapters and over Postgres without a database. -
outbox_eventstable replaces the old, never-drainedeventstable (migration 0005). One row is written per (event × topic):Publishenqueues the already Kafka-encoded message for both the integration and the analytics topic in the same INSERT batch, inside the same transaction as the aggregate write. This is what keeps the two topics from disagreeing about what happened — they are written atomically together, not by two independent publish attempts. -
kafkapackage: split Encode from Send.Publisher.EncodeandAnalyticsPublisher.Encodeturn a domain event into its wire-readyEncoded(topic, key, JSON value) without touching the network;PublishcallsEncodethen sends.postgres.OutboxPublishercalls onlyEncode(via the newkafka.Encoderinterface), so the outbox and the direct-publish path share one encoding implementation and can never produce different bytes for the same event. -
postgres.OutboxRelaypollsoutbox_eventsfor unpublished rows (FOR UPDATE SKIP LOCKED, oldest first), sends each through aSink(kafka.RelaySink, a topic-less writer that reads the topic per message offEncoded.Topic), and marks it published in the same row-locking transaction. At-least-once delivery: a crash between Send and the mark redelivers on the next pass.FOR UPDATE SKIP LOCKEDlets two overlapping relay instances (a rolling deploy) drain concurrently without duplicate sends racing each other for the same row. -
Composition root (
cmd/facility/main.go): when bothDATABASE_URLandEVENT_PUBLISHER=kafkaare set,EventPublisheris theOutboxPublisherand a relay goroutine runs alongside the HTTP server, sharing its shutdown signal (OUTBOX_RELAY_INTERVALtunes its poll interval). Every other combination (no database, or no Kafka) is unchanged from ADR-0009/ADR-0010's direct fan-out — there is no transaction to bind an outbox to without both being present.
The old postgres/event_publisher.go (the ADR-0009-era dead outbox) is
deleted outright rather than kept alongside the new table: two events-like
tables competing for the same job would be a standing source of confusion
about which one anything reads from.
Consequences
- An aggregate save and its event(s) reaching Kafka are now atomic from the caller's perspective: either both persist (aggregate row + outbox row(s)) or neither does, and the relay guarantees an outbox row that committed eventually reaches the broker.
- A crash between the relay's Send and its UPDATE redelivers that event.
Consumers on both topics were already required to be idempotent as
Conformists to an at-least-once Published Language (ADR-0009); this ADR
does not change that requirement, only makes redelivery detectable via the
stable
event_idthe outbox and the relay share. - The relay adds one background goroutine and one extra table to operate;
outbox_eventsneeds the estate's usual retention/archival policy for a table that grows unbounded onpublished_at, same asprocess-path-management's. (That retention now exists: ADR-0026's sweeper deletes PUBLISHED rows past a retention, default 7d; unpublished rows are never swept.) EVENT_PUBLISHER=kafkawith noDATABASE_URL(an in-memory-repos, direct-Kafka deployment) is intentionally left non-transactional: there is no Postgres transaction to bind the publish to, so this mode is unchanged from ADR-0009/ADR-0010 and callers should treat it as best-effort, same as before this ADR.