0016. Transactional outbox feeding both Kafka topics from one table
Status
Amended by ADR 0038: the outbox and its relay are no longer opt-in (
EVENT_PUBLISHER=log, the default, and the direct publisher were removed); the relay is always on.
Accepted — implemented in the same change that introduced this record. Relay retry amended by ADR 0036 (attempt cap). Follows process-path-management's ADR 0003 (the fleet's single-topic reference implementation) and adapts it to this service's fan-out.
Context
Every publishing use case in this service had the same shape:
if err := uc.Associates.Save(ctx, shift); err != nil { return err }
return uc.Events.Publish(ctx, shift.PullEvents()...)
Two independent writes to two independent systems, no compensation. A crash, a broker timeout or a pod eviction between them leaves a shift/assignment/plan in Postgres whose event never reached Kafka. For this service that has two distinct victims:
- The integration contract (
warehouse.workforce.events): aShiftPlanCommittedthat never reaches the topic means wes-work-planning never observes the labor plan, and itsLaborPlanObservedread model silently disagrees with what a human actually committed here. - The analytics data product (
warehouse.workforce.analytics, ADR 0010): a missingLaborAssigned/AssociateBreakStartedmakes the Labor Utilization & Staffing report wrong with no error anywhere.
The reverse failure — Publish succeeds, then the HTTP response is lost and the operator retries — was already tolerable (the aggregates are idempotent upserts), but the first one had no answer at all. Worse, with the direct publisher a broker outage turned every mutating request into a 500 after the row was already committed: the worst of both worlds.
Three things distinguish this service from the PPM reference and shape the design below:
ports.EventPublisher.Publishis variadic — one call carries every event an aggregate pulled.- There are two topics, fanned out today by
events.MultiPublisheroverkafka.Publisher(integration) andkafka.AnalyticsPublisher(analytics). Both must be fed from the outbox, or the guarantee holds for only half the consumers. - The integration publisher reads a repository at publish time: a
domain
ShiftPlanCommittedcarries onlybuildingId/shiftId, and the publisher loads the plan'sPathPlanlines to fan one message out per line (INTEGRATION.md). That read must see the row the use case just saved — so encoding has to happen inside the use case's transaction, not later in a relay.
Decision
Adopt the transactional outbox, in a fan-out variant:
-
outbox_eventstable (migration000002_outbox). One row is one already-encoded Kafka message:topic,event_type,key,value(the JSON envelope, byte-for-byte what the topic will carry),headers(JSON array of{key,value}— the W3C trace context captured when the event was raised), pluspublished_at,attempts,last_errorfor the relay. A single domain event therefore becomes N rows: forShiftPlanCommittedwith three lines, three integration rows and one analytics row. A partial index overpublished_at IS NULLkeeps the relay's scan tiny. -
kafkapackage: Encode split from Send.kafka.EncodedcarriesTopic,EventType,Key,Value,Headers;kafka.EncoderisEncode(ctx, events...) ([]Encoded, error). BothPublisherandAnalyticsPublishernow implement it — envelope construction, theshiftPlanslookup, trace-header injection and the "skip events with no analytics payload" rule all moved intoEncode, andPublishis simplyEncode+WriteMessagesinside the existing producer span. The outbox and the direct path can never disagree on wire format because they share the sameEncode. A newkafka.RelaySinkwraps a writer with no fixed topic and setsMessage.Topicper message: kafka-go rejects a message that names a topic when the writer also has one, and vice versa, which is whyEncoded.Topicis carried separately and only applied by the sink. Akafka.Writerinterface and…WithWriterconstructors make all three unit-testable without a broker. -
ports.UnitOfWork— a new driven port,Execute(ctx, fn func(ctx) error) error. Every publishing use case (all nine: StartAssociateShift, CertifyAssociate, StartBreak, EndBreak, CommitShiftPlan, AssignLabor, EndAssociateShift, ProposePathPlan, GetStaffingGap) wraps all of itsSaves and itsPublishin oneatomically(ctx, uc.UnitOfWork, …)scope. Reads that only decide whether to act stay outside; use cases that publish without saving (ProposePathPlan, GetStaffingGap) still wrap the Publish so every publishing path is uniform. The port is optional (nil= "run them back to back"), which is exactly the in-memory test configuration. The domain layer is untouched and the arch-go fitness tests pass unchanged. -
postgres.UnitOfWorkopens apgx.Tx, binds it to the context, and commits or rolls back aroundfn. Every repo resolves its querier from the context (querierFrom): the pool when standalone, the bound transaction inside a unit of work.AssignmentRepo.SaveandShiftPlanRepo.Save, which are multi-statement and used to open their ownpool.Begin, now usebeginOrJoin: they join the outer transaction when there is one (commit/rollback become no-ops owned by the UnitOfWork) and behave exactly as before when there is not. NestedExecutecalls join the outer scope. -
postgres.OutboxPublisherimplementsports.EventPublisher:NewOutboxPublisher(pool, encoders...). For each configured Encoder it callsEncode(ctx, events...)— inside the use case's transaction, so the integration Encoder's plan lookup sees the just-saved rows — andINSERTs one row perEncoded. It never touches the broker. -
postgres.OutboxRelayruns as a goroutine insidecmd/workforcenext to the HTTP server. Each pass claims up to 100 pending rows withSELECT … FOR UPDATE SKIP LOCKED ORDER BY id, sends them to theRelaySinkone at a time in id order, and marks eachpublished_at. On a send failure it stops the pass at that row (a later event for the same aggregate can never overtake a failed earlier one), recordsattempts+1/last_erroron it, commits what was already sent, and retries on the next tick. Sleep between empty passes isOUTBOX_RELAY_INTERVAL(default1s); a full batch is followed immediately by another pass. One relay serves both topics — the topic is a column on the row, not a property of the relay. -
Composition root (
cmd/workforce/main.goonly; the projector, reports and MCP binaries are untouched) picks the mode and logs it at startup asevent publisher configured publisher=kafka mode=outbox|direct:DATABASE_URLEVENT_PUBLISHERPublisher wired Relay unset log(default)log none unset kafkaMultiPublisher(integration, analytics)directnone set loglog none set kafkaOutboxPublisher(pool, integration, analytics)yes This binary requires
DATABASE_URL, so in practice only the last two rows are reachable; the direct row is kept so the wiring documents the fleet-wide matrix and an in-memory composition could still use it. Every publishing use case gets the samepostgres.UnitOfWork. Graceful shutdown stops the HTTP server first, then cancels the relay and waits for its in-flight pass (bounded by the 10s shutdown deadline), so an event committed by a request that completed a moment before SIGTERM is not stranded until the next pod boots.
Delivery semantics (what consumers may now rely on)
- Atomicity: an aggregate change and its events — for BOTH topics —
commit together or not at all. Verified by an integration test that
makes the outbox insert fail after
ShiftPlanRepo.Savehas already run and asserts theshift_planrow is absent (and likewise for the single-statementAssociateRepopath with aNOT NULLviolation). - At-least-once: a crash between a successful
Sendand the row'sUPDATErepublishes that row on the next pass. Both consumers already tolerate this: the analytics projector is idempotent onevent_id(ADR 0010,consumed_events), and the integration consumer keys its read model bypath_id, so a duplicate is an idempotent overwrite. A republished message carries the sameevent_idbecause the envelope is encoded once, at insert time. - Per-key ordering: preserved. Rows are drained in insertion order within one relay, analytics messages are keyed by aggregate id, and a failed row blocks everything behind it rather than being skipped.
- Latency: events reach the topic within one relay interval (≤1s by default) of the HTTP response instead of before it. For shift planning and labor assignment — decisions made at shift cadence — this is invisible.
- Tracing: the
traceparentcaptured when the event was raised is persisted with the row and forwarded untouched by the relay, so a consumer's span still parents onto the originating request's trace.
Consequences
Positive
- No code path persists a workforce change without also persisting its events for both the integration contract and the analytics report.
- No broker dependency on the request path:
POST /shift-planssucceeds when Kafka is down; the events are published once it returns. - Use cases are simpler: one atomic scope, one error.
Encodeis now unit-tested in isolation for both publishers, which the previousPublish-only tests could not do without a fake writer.
Negative / accepted
- One more table, one more goroutine, one more failure mode (the relay)
to observe. The relay logs every failed pass at ERROR with the row id,
topic and broker error;
outbox_events.attempts/last_errorare queryable. (The "outbox-lag metric is a follow-up" note below shipped:workforce.outbox.lag_seconds(RegisterOutboxLagGauge, internal/adapters/outbound/postgres/outbox_metrics.go) reports the age of the oldest unpublished row, 0 when drained. Published rows are bounded by the ADR-0028 sweeper's 7-day retention.) - Events are no longer synchronous with the HTTP response (documented above; acceptable for this domain).
ShiftPlanCommittednow costs N+1 outbox rows and N+1 relay sends where it used to cost one batched integration write plus one analytics write. At shift-planning volumes this is noise.- Encoding inside the transaction slightly lengthens it (one extra
SELECTof the plan lines forShiftPlanCommitted). It is the price of the integration publisher's fan-out-per-line contract; the alternative — moving that lookup into the relay — would read the plan after the transaction and could observe a later revision. - With two pods overlapping during a rolling deploy,
SKIP LOCKEDprevents double-claiming within a pass but does not order rows across the two relays for different aggregates. Per-key order is what consumers depend on, and that is preserved.
Alternatives considered
- Keep Save-then-Publish and add retry around Publish. Does not fix a crash between the two writes, and retries on the request path make the broker's latency the operator's latency. Rejected.
- Publish-then-Save. Inverts the failure: events on both topics for a change that never persisted, which is strictly worse for the analytics report and the integration consumer alike. Rejected.
- One outbox row per domain event, encode in the relay. Simpler table,
but breaks difference 3 above: the integration Encoder's plan lookup
would run outside the transaction, and the analytics envelope's
event_idwould be minted on every redelivery instead of once. Rejected. - Two outbox tables / two relays, one per topic. Doubles the moving parts for no isolation benefit; a topic column and one relay give the same per-topic independence. Rejected.
- Change-data-capture (Debezium). Correct, but adds a Kafka Connect deployment to a kind cluster that already runs Istio, Kong, Kafka, Postgres and the observability stack, and moves envelope encoding out of the service's own code. Deferred fleet-wide, as in PPM ADR 0003.
- A separate relay binary (like
cmd/workforce-projector). Cleaner isolation, but the relay's work is a singleSELECT/UPDATEloop and this service already runs three binaries. In-process, with graceful-shutdown handling, is proportionate; lifting it intocmd/workforce-relaylater would not touch the adapters.
Verification
- Unit (
internal/application/usecases/unit_of_work_test.go): for every publishing use case, everySaveand thePublishrun inside exactly one scope (checked via a ctx marker on the repos and publisher fakes), a publish failure rolls the scope back, a rejected command opens no scope, a begin failure propagates, and a nil UnitOfWork still saves and publishes. - Unit (
internal/adapters/outbound/kafka/encode_test.go):Encodefor both publishers (topic, key, event_type, envelope,traceparentheader present when a span is active, repo-miss error, unknown-event skip);RelaySinksets the topic per message, writes once, forwards persisted headers, rejects a topic-less message and wraps writer errors. All pre-existingPublishtests pass unchanged. - Integration (
-tags=integration, testcontainers Postgrespostgres:16-alpine— the test owns its database, never an externalDATABASE_URL, nevert.Skip):outbox_integration_test.go— commit-together forCommitShiftPlan(2 integration rows + 1 analytics row + persisted trace header) andAssignLabor(associate + assignment- 3 analytics rows), rollback on encoder failure and on
NOT NULLviolation, relay publishes in id order across both topics and marks rows, relay stops at a failed row (attempts=1,last_errorset, later row untouched) and recovers in order, andRundrains with batch size 1 until cancelled. Six tests, all passing locally against real containers.
- 3 analytics rows), rollback on encoder failure and on
- Gates:
gofmt -l .,go vet ./...,go build/vet -tags=integration ./...,make lint,make check,make arch-test; domain+application coverage 97.7% → 97.8%. - Cluster verification (post-merge, not part of this change): after
deploy,
POST /shift-plansthenSELECT topic, event_type, published_at FROM outbox_events ORDER BY id DESC LIMIT 4should show all rows published within one interval, and wes-work-planning'sLaborPlanObservedshould reflect the plan without restart.