Skip to main content

Runtime and integration mechanics

Layers​

Hexagonal architecture, enforced by TestHexagonalArchitecture and the fitness tests in internal/architecture:

LayerPath
Domaininternal/domain/product
Application (use cases, ports)internal/application/{usecases,ports,repository,outbox}; analytics read model in internal/analytics
Inbound adaptersinternal/adapters/inbound/http (REST and the reports router), internal/adapters/inbound/kafka (the analytics consumer, wired only by cmd/product-projector), internal/adapters/inbound/mcp (MCP tools: read-only but for the two pack level writes of ADR 0011)
Outbound adaptersinternal/adapters/outbound/{postgres,memory,kafka,outbox,clock,telemetry,analyticsstore}
CloudEvents helperinternal/adapters/kafka/cloudevents
Composition rootscmd/api/main.go, cmd/mcp/main.go, cmd/product-projector/main.go, cmd/product-reports/main.go

The domain imports nothing internal but itself; adapters never import each other; only cmd/* wires them. The rest of this page describes cmd/api after the overview of every binary; the MCP server is covered by MCP tools and ADR 0005, the analytics binaries by ADR 0006 and the runbook.

Binaries​

All four are built into one image by the root Dockerfile (/app/<name>); each is its own Deployment in charts/product-master.

BinaryRolePortReadsWrites
cmd/apiREST API and the outbox relay; needs DATABASE_URL and KAFKA_BROKERS or it does not boot:8080 (HTTP_ADDR): REST, /healthz, /readyz (Postgres ping and Kafka broker check), /metricsOLTP PostgresOLTP Postgres; warehouse.product-master.events and warehouse.product-master.analytics (relay)
cmd/mcpMCP server, eight tools (six read-only, the two pack level writes of ADR 0011), Streamable HTTP at / and /mcp:8090 (MCP_ADDR)OLTP Postgresnothing (it runs the idempotent OLTP migrations at start)
cmd/product-projectorAnalytics writer: projects the analytics topic into the analytical database:8091 (ADMIN_ADDR): /healthz, /readyz onlywarehouse.product-master.analytics (group ANALYTICS_CONSUMER_GROUP)analytical Postgres; warehouse.product-master.analytics.dlq
cmd/product-reportsRead-only reports API (/reports/master-data-quality, /reports/freshness):8092 (HTTP_ADDR)analytical Postgres (read-only pool)nothing

The console remote productmaster_mfe (web/) is a separate image that calls the REST API. Every variable of every binary is on Configuration.

Container view​

Source: cmd/api/main.go, cmd/mcp/main.go, cmd/mcp/router.go, cmd/product-projector/main.go, cmd/product-reports/main.go, internal/adapters/outbound/kafka/encoder.go, internal/adapters/outbound/kafka/analytics_encoder.go, internal/adapters/inbound/kafka/analytics_consumer.go. cmd/api consumes no topic: the ADR 0003 legacy importer was removed at stage E (2026-10-17). Omits: Kong and the Nginx web gateway, the OpenTelemetry Collector and the nine downstream contexts that consume warehouse.product-master.events (see Downstream consumers).

Data stores​

StoreTablesWritersReadersSchema
OLTP Postgres (DATABASE_URL)products, product_pack_levels (ADR 0011), outbox_events, processed_events (no writer since stage E)cmd/api, cmd/mcp (declare_pack_level, retire_pack_level only)cmd/api, cmd/mcpinternal/adapters/outbound/postgres/migrations (embedded, applied by cmd/api and cmd/mcp at start)
Analytical Postgres (ANALYTICS_DATABASE_URL)analytics_processed_events, product_facts, profile_statescmd/product-projectorcmd/product-reportsanalytics/migrations (embedded, applied by the projector at start)

Postgres and Kafka are the only runtime (ADR 0010): no binary has an in-memory mode. cmd/api and cmd/mcp exit at boot without DATABASE_URL, cmd/api without KAFKA_BROKERS, the analytical binaries without ANALYTICS_DATABASE_URL, and the projector without ANALYTICS_CONSUMER_GROUP; the error names the variable and is raised before the listener binds. The in-memory adapters in internal/adapters/outbound/memory are test doubles, and TestRuntimeBinaryHasNoInMemoryAdapters fails if any cmd/** file imports them.

Writing: one unit of work per command​

Every write use case (internal/application/usecases/writer.go) runs inside one ports.UnitOfWork: load the Product, apply the aggregate command, save it guarded by the loaded version, encode the raised events (EventEncoder) and insert them into outbox_events. A command that raises no event saves and enqueues nothing. With Postgres the unit of work is one transaction (internal/adapters/outbound/postgres/unit_of_work.go); the in-memory adapters provide the same contract to unit and BDD tests.

Publishing: transactional outbox​

  • The encoder (internal/adapters/outbound/kafka/encoder.go) mints the CloudEvents id once, at encode time, and the row stores it, so a relay retry republishes the same id.
  • The relay (internal/adapters/outbound/outbox/relay.go) drains every OUTBOX_RELAY_INTERVAL (default 1s); a full batch (100 rows) is followed immediately by another pass. OutboxRepo.Drain claims rows with FOR UPDATE SKIP LOCKED, sends them one at a time in id order, marks each published, and stops at the first failure (recording attempts and last_error) so a later event of a SKU never overtakes an earlier one that is still being retried. A row that has failed OUTBOX_MAX_ATTEMPTS times (default 20, 0 = unlimited) is no longer claimed and is logged once at ERROR (ADR 0007).
  • The relay always publishes through RelaySink: a topic-less kafka-go writer with RequireAll acks, a 10 ms batch timeout, the Hash balancer on the key (the SKU) and auto topic creation. There is no log sink; EVENT_PUBLISHER=log stops the boot.
  • /readyz is the drain gate plus a Postgres ping plus a Kafka broker check (a fresh connection that asks for the broker list, so it needs no topic and creates none). The verdict is cached 3 s, one round bounded to 2 s; a failure is a 503 dependency-unavailable problem naming postgres or kafka.

Consuming: the analytics projector​

  • cmd/product-projector is the only consumer of this context (the ADR 0003 legacy importer was removed at stage E, 2026-10-17). It reads warehouse.product-master.analytics in the group ANALYTICS_CONSUMER_GROUP, which is required: there is no default group id.
  • FetchMessage, handle, then CommitMessages; never ReadMessage.
  • A message that is not a CloudEvent is skipped and committed past (one WARN, then at most one per minute with the count suppressed); a CloudEvent of another type is ignored and committed past. A known type with an unusable payload, or one the store rejects deterministically, is dead-lettered to warehouse.product-master.analytics.dlq and committed past (internal/adapters/inbound/kafka/analytics_consumer.go).
  • A transient failure retries the same message with capped exponential backoff (200 ms doubling to 5 s) until it succeeds or the process stops. A failed offset commit is retried the same way.

CloudEvents envelope​

Structured content mode, Kafka header content-type: application/cloudevents+json; charset=UTF-8, source=/warehouse/product-master, subject = Kafka key = the SKU, dataschema=urn:warehouse:product-master:events:<EventName>:v1 (internal/adapters/kafka/cloudevents/cloudevents.go, ADR 0004).

Shutdown order​

On SIGINT/SIGTERM cmd/api: (1) flips /readyz to 503, (2) waits SHUTDOWN_DRAIN_DELAY (default 5s), (3) drains HTTP (10s), (4) stops and awaits the relay last (bounded to 10s), then closes the pool, so nothing committed is stranded (serveUntilSignal in cmd/api/main.go).

Observability​

OTLP traces and metrics (OTEL_EXPORTER_OTLP_ENDPOINT), otelchi route spans and request-duration metrics on every REST route, and GET /metrics with the Go runtime and process collectors (internal/adapters/outbound/telemetry). One business counter, product_master.products.registered (by outcome), is pushed over OTLP. Details, log fields and suggested alerts: Observability.