ADR 0006: Postgres and Kafka are the only runtime
Status
Accepted (2026-10-17). Supersedes the in-memory and permissive statements of
ADR 0003 and
ADR 0005. Amended the same day
by Amendment 1 (topics, readiness and
the relay writer): where the text below says infrastructure owns the topics or
that /readyz needs them, the amendment governs.
Context
The service shipped with three ways to run without its infrastructure: without
DATABASE_URL cmd/api logged "using in-memory adapters" and served from
memory (no outbox, no relay, no Idempotency-Key); the two consumers were off
unless *_MODE=kafka (permissive, the default, started nothing); and the
outbox relay was off unless KAFKA_BROKERS was set. /readyz reported only
the shutdown drain gate. A deployment that missed one variable therefore came
up "healthy" and silently lost every hint it was meant to publish and every
fact it was meant to consume. The decision of the owner is final: the service
runs on a real database and real Kafka, wired for real.
Decision
- One runtime. The shipped binary runs on Postgres and Kafka only. The
in-memory adapters (
internal/adapters/outbound/memory) stay as test doubles for unit and BDD tests.TestRuntimeBinaryHasNoInMemoryAdaptersfails the build when a non-test file undercmd/**imports them, and a fixtures test proves the sensor bites. The production clock and hint-id generator moved tointernal/adapters/outbound/systemsocmd/**does not need the memory package for them. - Mandatory configuration, boot error otherwise.
DATABASE_URL,KAFKA_BROKERS,FULFILLMENT_CONSUMER_GROUPandLAYOUT_CONSUMER_GROUPhave no default. A missing one is a boot error that names the variable (X is required: this context runs on real Postgres|Kafka), all problems reported together, raised before any connection is opened and before the listener binds. A badHINT_EXPIRY_INTERVALkeeps being a boot error in the same pass. Group ids still come from the environment, never from a literal. - Consumers are always on. The variables
FULFILLMENT_MODEandLAYOUT_MODEstay (the fleet documents and the template rule files name them) but the default iskafka, andpermissiveis rejected at boot with<NAME>=permissive is not supported: this context runs on real Kafka. Any other value is a boot error. Dead-lettering, the shutdown guard and the offset semantics (FetchMessagethenCommitMessages) are unchanged. A consumer is also supervised: when kafka-go reports a group-join failure that outlasted its attempts (an unreachable broker)Runreturns, and the consumer is run again after one second (logconsumer stopped; restarting) until the service stops, so a broker outage at any time never leaves a consumer dead while/readyzturns green again. - The expiry worker and the relay are always on.
HINT_EXPIRY_INTERVALkeeps its default of 5 s;OUTBOX_MAX_ATTEMPTSkeeps its default of 20. A relay that cannot be built (no outbox store, no brokers) fails the boot. The shutdown order is unchanged:/readyzflips, HTTP drains, the consumers stop, the expiry worker stops, the relay stops, the pool closes. /readyzis real. (Amended: the Kafka part is reachability only, see Amendment 1.) It is ready only when the drain gate is open AND Postgres answers a ping AND a Kafka metadata request succeeds with a leader for every topic the service consumes or produces (warehouse.fulfillment.events,warehouse.facility.events,warehouse.task-interleaving.events,warehouse.task-interleaving.analytics). The verdict is cached for 3 seconds and one round of checks is bounded by 2 seconds, so probes cannot hammer either dependency. A failed dependency is503application/problem+jsonwith the new slugdependency-unavailable, naming the dependency (not ready: kafka) and never its cause (the cause is logged). The drain answer is unchanged (503 {"status":"not_ready"})./healthzstays liveness only and does not look at any dependency.- Migrations at boot stay, with backoff (
internal/bootretry) and theMIGRATIONS_DATABASE_URLfallback. - The service never creates topics. (Amended: the statement that
infrastructure owns them and that a missing topic makes
/readyzfail is superseded by Amendment 1.) Infrastructure owns them. At boot the service logs the topics it consumes and produces. A topic that does not exist yet makes/readyzreportkafkanot ready and nothing else; the process keeps running. Idempotency-Keyis required onPOST /recommendationsin every deployment, so the OpenAPI parameter is nowrequired: true(it was optional only because of the in-memory mode).
Missing topics: what kafka-go does (verified)
(Superseded in part by Amendment 1: the 503 on missing topics and the
"infrastructure creates the topics" step below describe the first version. The
consumer and relay observations still hold.)
The end-to-end test boots the real entry point against a broker with topic auto-creation off, before any topic exists:
/readyzis503 dependency-unavailable("not ready: kafka") and no topic is created: the readiness check asks for metadata of the named topics withoutAllowAutoTopicCreation.- The consumers do not crash: kafka-go keeps retrying the group join. It does
that silently, so the consumers install an
ErrorLoggerand every retry is an ERROR log line (kafka reader error; retrying, with the consumer and the topic). - Once infrastructure creates the topics the same process becomes ready and consumes from the first offset, with no restart.
The relay writes with Message.Topic; a write that cannot be delivered (a topic
that does not exist, or an unreachable broker) fails and the row's attempts
grows. Observed with the broker unreachable and the default 1 s interval: the
first row reached the cap of 20 after about 20 s, the relay logged row reached the attempt cap and is excluded from future claims and left the row in
outbox_events. Rows at the cap (OUTBOX_MAX_ATTEMPTS) are kept and excluded
from claims; an operator re-arms them with UPDATE outbox_events SET attempts = 0 WHERE id = ... once the cause is fixed. An outage longer than the cap times
the relay interval is therefore an incident that needs that step; the cap is
unchanged by this ADR.
Consequences
- A misconfigured deployment now fails to boot (or reports not ready) instead of running without a relay or consumers.
- Local runs need Postgres and Kafka:
make run-localbrings both up withdocker-compose.yml(a job pre-creates the four topics locally) and runs the service on them. - The unit and BDD suites keep using the in-memory doubles; no
cmdtest relies on an in-memory runtime of the binary. The command tests are pure configuration tests or run the real entry point on testcontainers. - The
permissivemode of ADR 0003 and the in-memory development mode of ADR 0005 no longer exist. Their bodies stay as written, with a pointer here.
Amendment 1: the fleet topic convention
Found before the first deployment. The fleet convention (warehouse-infra
terraform/kafka.tf) is that the broker auto-creates every business topic on
first write (num.partitions=8), infrastructure declares no topics, and
every service's Writer sets AllowAutoTopicCreation: true. The first version of
this ADR assumed the opposite (infrastructure owns the topics) and broke the
convention twice:
- The relay's writer (
NewWriter) did not setAllowAutoTopicCreation, so on the cluster the service's own topics (warehouse.task-interleaving.eventsandwarehouse.task-interleaving.analytics) were never created: the relay failed withUnknownTopicOrPartitionand rows sat at the attempt cap. /readyzrequired every consumed AND produced topic to exist with a leader. On a fresh cluster the output topics do not exist until the service first publishes, and a not-ready pod receives no traffic, so it could never be driven to publish: a deadlock. The consumed topics (warehouse.fulfillment.events,warehouse.facility.events) only exist once their producers ran, an ordering coupling no other service has.
Decision (this replaces the topic statements above, the rest stands):
- The relay writer sets
AllowAutoTopicCreation: true(NewWriter, next to the DLQ writers that already did), with a unit test (TestNewWriterAllowsAutoTopicCreation). The service still never calls a topic-admin API (CreateTopicsand the like): the broker creates a topic when the first message is written to it. /readyz= drain gate AND Postgres ping AND a Kafka broker answers a metadata request. The request names no topics (an empty, non-nil list; a nil list would mean all topics), so it is a reachability check, cheap, and asking never creates a topic. A missing or leaderless topic, consumed or produced, is not a readiness failure. The 3 second cache, the 2 second bound, the503application/problem+jsonwith thedependency-unavailableslug (not ready: kafka, never the cause) and the drain answer are unchanged.- A missing consumed topic shows as consumer retry logs, not as unreadiness:
kafka-go keeps retrying the group join and the
ErrorLoggerturns each retry into an ERROR line (kafka reader error; retrying, with the consumer and the topic). Once the producer has written, the topic exists and the same process consumes from the first offset with no restart. The boot linekafka topics (...)stays, informational only. - Local runs (
docker-compose.yml) still pre-create the four topics with a job; that is a local convenience, not the contract. - The end-to-end test keeps auto-creation OFF in its testcontainers broker so
it controls when a topic appears:
/readyzis200while the topics do not exist and the broker is up, then the test creates the topics and the existing flow runs unchanged./readyzstill reports503while Kafka is unreachable (TestServiceStaysAliveButNotReadyWhileKafkaIsUnreachable). ThatAllowAutoTopicCreationtakes effect against a broker with auto-creation enabled is covered at unit level only (the shared test broker is configured once per binary); the cluster is where it is proven.
Consequences: a fresh cluster deploys in any order and the pod becomes ready without waiting for any producer; a topic missing for long is visible in the consumer logs rather than by taking the pod out of rotation.