Skip to main content

Runtime: configuration, probes, shutdown and the outbox

cmd/api is the only binary. It runs on real PostgreSQL and real Kafka; there is no alternative runtime and no log sink.

Environment​

VariableDefaultMeaning
DATABASE_URLrequiredPostgres DSN of the service's pool. A boot error names it when unset.
MIGRATIONS_DATABASE_URLDATABASE_URLDirect DSN for the boot migrations (golang-migrate holds a session advisory lock, which breaks behind a transaction-pooling proxy).
KAFKA_BROKERSrequiredComma-separated host:port list (relay, consumer, /readyz). A boot error names it when unset or blank.
NODE_TOPOLOGY_CONSUMER_GROUPrequiredConsumer group id of the node-topology consumer. A boot error names it when unset or blank; there is no default, so a local process cannot join a live cluster's group by accident.
SELLER_INBOUND_CONSUMER_GROUPrequiredConsumer group id of the seller-inbound consumer (ADR 0013); same rules.
CUSTOMER_RETURNS_CONSUMER_GROUPrequiredConsumer group id of the customer-returns consumer (ADR 0015); same rules.
HTTP_ADDR:8080Listen address.
OUTBOX_RELAY_INTERVAL1sPoll interval of the relay (Go duration or seconds).
OUTBOX_MAX_ATTEMPTS20Failures before the relay excludes a row; 0 = unlimited (ADR 0005).
RETENTION_INTERVAL1hHow often the janitor deletes idempotency keys older than 24 h and feeds and processed events older than 30 days.
SHUTDOWN_DRAIN_DELAY5sWait between flipping /readyz to 503 and closing the listener; 0 disables.
LOG_LEVELinfodebug, info, warn, error.
OTEL_SERVICE_NAMEoffer-availabilityservice.name of spans and metrics (ADR 0009).
OTEL_EXPORTER_OTLP_ENDPOINTlocalhost:4317Collector OTLP/gRPC, host:port or URL; export never blocks.
SERVICE_VERSION / ENVIRONMENTdev / localservice.version / deployment.environment.name.

Configuration errors are raised before anything is opened or bound. An invalid optional value logs a warning and falls back to its default.

Boot and shutdown​

  1. Validate the environment. 2. Run the embedded migrations and ping Postgres, both with exponential backoff (5 attempts, 1 + 2 + 4 + 8 s). 3. Start the outbox relay, the node-topology consumer and the janitor (none dials at construction; a broker that is down only makes passes fail until it is back). 4. Serve.

On SIGINT/SIGTERM: /readyz flips to 503 not_ready, wait SHUTDOWN_DRAIN_DELAY, drain HTTP (10 s), cancel and await the three loops (10 s each), close the pool. If a loop returns on its own the process exits non-zero so the orchestrator restarts it. Room for the whole sequence: 5 + 10 + 10 s, so a pod needs a termination grace of 30 s or more.

Probes​

  • GET /healthz is liveness: 200 {"status":"ok"} while the process serves, even while draining or while a dependency is down.
  • GET /readyz is 200 {"status":"ready"} only if the drain gate is open, Postgres answers a ping and a Kafka broker answers a metadata request. The request names no topics (an empty, non-nil list), so it never creates a topic and does not need the output topic to exist; a fresh connection is dialled on every check. While draining it answers 503 {"status":"not_ready"}; while a dependency is down, 503 application/problem+json of type dependency-unavailable naming it. Verdicts are cached 3 s.

The outbox in operation​

  • Events are written in the same transaction as the aggregates and published later, so a write is accepted while Kafka is down; outbox_events.published_at IS NULL rows wait and are published, in id order, when the broker is back.
  • Delivery is at-least-once with a stable CloudEvents id; consumers deduplicate.
  • A row that failed OUTBOX_MAX_ATTEMPTS times is skipped (logged once at ERROR with id, topic, type and key) and stays in the table with attempts and last_error. Fix the cause, then re-arm it: UPDATE outbox_events SET attempts = 0 WHERE id = <id>;. While a row is skipped, later events of the same aggregate can overtake it.

The consumer in operation​

  • retailnetwork.customer-returns.events is read under CUSTOMER_RETURNS_CONSUMER_GROUP the same way (ADR 0015): ReturnCompleted adds each line's qtyRestockable to the owner listing at the node; unsellable units never touch a listing.
  • retailnetwork.seller-inbound.events is read under SELLER_INBOUND_CONSUMER_GROUP the same way (ADR 0013): SellerSuspended and SellerReinstated are applied, and InboundShipmentReceived creates the seller's listings of the received stock (ADR 0014; skipped lines and events without received are counted by reason); every other type is counted in availability.seller_events.ignored (reason=type_not_consumed) and skipped.
  • retailnetwork.node-topology.events is read under NODE_TOPOLOGY_CONSUMER_GROUP; the offset is committed after the event was handled, and each event is applied in one transaction together with its processed_events row, so a redelivery changes nothing.
  • A transient failure is retried five times with a capped backoff and then the message is published to retailnetwork.node-topology.events.dlq with the headers x-dlq-source-topic, x-dlq-source-partition, x-dlq-source-offset, x-dlq-error and x-dlq-failed-at. An event that can never be applied (no nodeId, no instant, a value the use case refuses) is dead-lettered at once. A message that is not a CloudEvent, or a type this service does not consume, is skipped. Nothing here drains the DLQ topic: that is an operator task.

Deployment (Helm chart)​

charts/offer-availability deploys the one binary as a single api Deployment with its Service, ConfigMap, ServiceAccount and database Secret; the HPA, Ingress and Gateway API HTTPRoute exist but default off. The image is ghcr.io/iqvo/offer-availability; an empty image.tag means v + the chart appVersion, the tag the release job pushes. The pod runs as uid 1000 with a read-only root filesystem, all capabilities dropped and the RuntimeDefault seccomp profile; /healthz backs the startup and liveness probes, /readyz the readiness probe.

ValueEnv varNotes
database.url or database.existingSecret (+ existingSecretKey, default DATABASE_URL)DATABASE_URLRequired; always a secretKeyRef.
database.migrationsExistingSecretKeyMIGRATIONS_DATABASE_URLOptional secretKeyRef (optional: true); the binary falls back to DATABASE_URL.
kafka.brokersKAFKA_BROKERSRequired.
config.nodeTopologyConsumerGroupNODE_TOPOLOGY_CONSUMER_GROUPRequired, no default; stable and service-prefixed.
config.sellerInboundConsumerGroupSELLER_INBOUND_CONSUMER_GROUPRequired, no default; stable and service-prefixed.
config.customerReturnsConsumerGroupCUSTOMER_RETURNS_CONSUMER_GROUPRequired, no default; stable and service-prefixed (proposed offer-availability-customer-returns).
otel.endpoint, otel.serviceName (<name>-mcp for the mcp component, <name>-a2a for the a2a component), image.tag or appVersion, environmentOTEL_EXPORTER_OTLP_ENDPOINT, OTEL_SERVICE_NAME, SERVICE_VERSION, ENVIRONMENTotel.enabled=false renders no OTEL_* env; extraEnv may not set these four.
config.httpAddr, logLevel, outboxRelayInterval, outboxMaxAttempts, retentionInterval, shutdownDrainDelayHTTP_ADDR, LOG_LEVEL, OUTBOX_RELAY_INTERVAL, OUTBOX_MAX_ATTEMPTS, RETENTION_INTERVAL, SHUTDOWN_DRAIN_DELAYDefaults repeat the binary's; the render test checks they agree.

The chart fails at render for: no database source, no or blank kafka.brokers, an empty config.nodeTopologyConsumerGroup, any *_MODE variable in extraEnv, and a terminationGracePeriodSeconds below SHUTDOWN_DRAIN_DELAY + 25 s (the shutdown budget above is drain delay + 10 s HTTP + 10 s loops; the default is 60 s, 30 s is the floor at the default 5 s drain). config.shutdownDrainDelay must be 0 or a whole number of ms, s or m so the chart can add it up.

helm lint charts/offer-availability -f charts/offer-availability/ci/default-values.yaml
bash charts/offer-availability/tests/render_test.sh # env contract, secrets, hardening, refusals
python3 charts/offer-availability/tests/test_service_selectors.py # the Service selects exactly one Deployment

CI runs helm-lint (ct lint plus the selector test) on pull requests and chart-tests on every push and PR; the release job packages offer-availability-<version>.tgz and pushes it to oci://ghcr.io/iqvo.

Tests​

make check (format, vet, build, lint, unit + handler + BDD tests), make coverage (90% over internal/domain and internal/application), make mutation (gremlins per domain package), make arch-test (fitness), make integration (testcontainers Postgres + Kafka: repositories, Drain, the relay, the middleware, the consumer flow and the real-wiring test cmd/api/e2e_integration_test.go; needs Docker).