Skip to main content

Order Sourcing runbook

Order Sourcing decides, as late as the promise allows, which node and path will fulfil each shipment group of a placed order. One FulfillmentAssignment per group sits in a virtual queue where its candidate (node, path, cost) is re-optimised against current capacity until the release point, is released to one node at a time, reassigned on refusal or expiry, locked when the node commits, and followed through SHIPPED and DELIVERED; the service also decides cancels and applies revised promises. Everything it knows about the network is a local read model fed by sibling events: it calls no sibling API (Introduction, ADR 0002, ADR 0003).

Components​

ComponentBinaryChart switchPortExposure in the fleet (warehouse-infra helm-values/order-sourcing.yaml)
api Deploymentcmd/apialways on8080 (Service port 80)Kong route /api/order-sourcing, prefix stripped
mcp Deploymentcmd/mcpmcp.enabled (default off, on in the fleet)8090Kong route /api/mcp/order-sourcing, rewritten to /mcp
a2a Deploymentcmd/a2aa2a.enabled (default off, on in the fleet)8093Kong route /api/a2a/order-sourcing, rewritten to /; the Agent Card stays under the same prefix
analytics-projector Deploymentcmd/os-projectoranalytics.enabled (default off)8091 (admin probes only, no Service)none
analytics-reports Deploymentcmd/os-reportsanalytics.enabled (default off)8092 (Service port 80)analytics.reports.route.enabled: Kong /api/order-sourcing/reports, rewritten to /reports; konghq.com/regex-priority: "10" so it outranks the API prefix
frontend Deploymentnginx serving web/frontend.enabled (default off, on in the fleet)8080the web gateway, /mfes/order-sourcing/ (never the API route)

The three Go binaries are in one image (ghcr.io/iqvo/order-sourcing). cmd/api is the only one that writes: one process runs the REST API, nine Kafka consumers, the transactional-outbox relay and the virtual-queue sweep. cmd/mcp and cmd/a2a are read-only and only read Postgres. The analytics read side (ADR 0013) adds cmd/os-projector, the only writer of a SEPARATE analytical database, and the read-only cmd/os-reports; neither opens the OLTP database. See Runtime for how each boots and drains.

Dependencies​

  • Postgres (schema in internal/adapters/outbound/postgres/migrations/0001_order_sourcing_schema.up.sql): assignments, the rm_* read models, outbox_events, processed_events, idempotency_keys. Every binary runs the embedded migrations at boot, then pings, each wrapped in bootretry (5 attempts, 15 s of backoff in total).
  • Analytical Postgres (analytics.enabled): cmd/os-projector runs the embedded analytical migration (analytics/migrations/0001_sourcing_facts.up.sql: queued_groups, sourcing_decisions, unsourceable_declarations and analytics_processed_events, 4 tables) against ANALYTICS_DATABASE_URL; cmd/os-reports only reads it. It is a projection of the context's own topics and can be rebuilt from them. To roll back by hand use 0001_sourcing_facts.down.sql.
  • PgBouncer versus a direct DSN: DATABASE_URL is the pooled DSN the pool uses; MIGRATIONS_DATABASE_URL (an optional key in the same secret) is a direct, session-mode DSN used only by the migration step, because golang-migrate holds a session advisory lock a transaction-pooling proxy cannot honour. If unset, the binary falls back to DATABASE_URL. In the fleet both come from the Secret order-sourcing-db.
  • Kafka (KAFKA_BROKERS, comma-separated; one broker platform-wide): used by cmd/api only (the relay, the nine consumers and the /readyz broker check). Topics are auto-created by the first writer; this service never creates the topics it consumes.

Kafka topics and consumer groups​

DirectionTopicConsumer group (variable, fleet value)
consumesretailnetwork.customer-ordering.eventsCUSTOMER_ORDERING_CONSUMER_GROUP, order-sourcing-customer-ordering
consumesretailnetwork.delivery-promise.eventsDELIVERY_PROMISE_CONSUMER_GROUP, order-sourcing-delivery-promise
consumesretailnetwork.last-mile-delivery.eventsLAST_MILE_DELIVERY_CONSUMER_GROUP, order-sourcing-last-mile-delivery
consumesretailnetwork.linehaul-transportation.eventsLINEHAUL_TRANSPORTATION_CONSUMER_GROUP, order-sourcing-linehaul-transportation
consumesretailnetwork.node-performance.eventsNODE_PERFORMANCE_CONSUMER_GROUP, order-sourcing-node-performance
consumesretailnetwork.node-topology.eventsNODE_TOPOLOGY_CONSUMER_GROUP, order-sourcing-node-topology
consumesretailnetwork.offer-availability.eventsOFFER_AVAILABILITY_CONSUMER_GROUP, order-sourcing-offer-availability
consumesretailnetwork.shipping-and-tracking.eventsSHIPPING_AND_TRACKING_CONSUMER_GROUP, order-sourcing-shipping-and-tracking
consumesretailnetwork.vendor-connectivity.eventsVENDOR_CONNECTIVITY_CONSUMER_GROUP, order-sourcing-vendor-connectivity
producesretailnetwork.order-sourcing.eventsintegration events (9 types: the eight integration events and FulfillmentReleaseWithdrawn)
producesretailnetwork.order-sourcing.analyticsanalytics-only events (FulfillmentQueued, FulfillmentSourcingDecided, FulfillmentDelivered, AssignmentsConsolidated, AssignmentsDeconsolidated, SourcingPolicyPublished)
consumes (cmd/os-projector, ADR 0013)retailnetwork.order-sourcing.events and retailnetwork.order-sourcing.analytics (its own topics)ANALYTICS_CONSUMER_GROUP, order-sourcing-analytics; poison only to retailnetwork.order-sourcing.analytics.dlq, transient failures retry forever
dead letter<consumed topic>.dlqwritten by the consumers, read by no one in this service

The consumer group ids are stable names you choose (the fleet values come from warehouse-infra helm-values/order-sourcing.yaml). Never derive one from a pod name: a rescheduled pod must resume from its committed offsets, and a brand-new group re-reads the topic from the start. The event types per topic are in Events and Integration.

Deployment (Helm chart)​

charts/order-sourcing/ renders the api Deployment plus the optional mcp, a2a and frontend Deployments, a Service for each, a ConfigMap mounted with envFrom, a Secret (or database.existingSecret) for DATABASE_URL, a ServiceAccount, and an HPA, Ingress, Gateway API HTTPRoute (api) and MCP/A2A routes that are off by default. The image is ghcr.io/iqvo/order-sourcing; an empty image.tag resolves to the chart appVersion, which the release job sets to the release version. Every key under config.* maps one-to-one to a cmd/api variable (see Configuration).

ValueVariableRule
database.url or database.existingSecret (+ existingSecretKey)DATABASE_URLMandatory. Always a secretKeyRef; the chart Secret is created only from database.url; the DSN never lands in the ConfigMap.
database.migrationsExistingSecretKeyMIGRATIONS_DATABASE_URLsecretKeyRef with optional: true (the binary falls back to DATABASE_URL); empty drops the variable.
kafka.brokersKAFKA_BROKERSMandatory.
config.<producer>ConsumerGroup (nine)<PRODUCER>_CONSUMER_GROUPMandatory, stable, service-prefixed (order-sourcing-<producer> in ci/default-values.yaml); a value containing $( (pod-name derived) is refused.
config.eventPublisherEVENT_PUBLISHEROnly kafka.
config.outboxRelayInterval, outboxMaxAttempts, sweepInterval, httpAddr, logLevelOUTBOX_RELAY_INTERVAL, OUTBOX_MAX_ATTEMPTS, SWEEP_INTERVAL, HTTP_ADDR, LOG_LEVELOptional ones are not rendered while empty, so the binary default applies.
config.shutdownDrainDelaySHUTDOWN_DRAIN_DELAYWhole seconds (5s), always rendered.
terminationGracePeriodSecondsn/aDefault 60. The chart refuses a value that does not exceed shutdownDrainDelay + 30 s (HTTP 10 s + consumers and sweep 10 s + relay 10 s).
otel.enabled, otel.endpoint, otel.serviceName, environmentOTEL_EXPORTER_OTLP_ENDPOINT, OTEL_SERVICE_NAME, ENVIRONMENT (and SERVICE_VERSION from the image tag)otel.enabled=false renders no OTEL_* variable and the binaries fall back to localhost:4317. The mcp component gets <serviceName>-mcp.
mcp.enabled, mcp.httpAddr, mcp.route.*MCP_ADDRmcp.httpAddr must equal mcp.service.targetPort.
a2a.enabled, a2a.httpAddr, a2a.agentUrl, a2a.route.*A2A_ADDR, A2A_AGENT_URLEmpty agentUrl defaults to the in-cluster Service DNS http://<release>-a2a.<ns>.svc.cluster.local:8093/; set it to the Kong URL when the agent must be reachable off-cluster.
extraEnvn/aRefuses EVENT_PUBLISHER other than kafka, any *_MODE, any *_CONSUMER_GROUP and any variable the chart renders itself.

The pods run as uid 1000 with a read-only root filesystem, allowPrivilegeEscalation: false, all capabilities dropped and the RuntimeDefault seccomp profile. Probes: startup and liveness on /healthz, readiness on /readyz (api); the startup probe allows about 60 s (2 s period, 30 failures). Default replicaCount: 1, requests 100m CPU and 128Mi memory, limits 500m and 256Mi. More than one api replica is safe by design (the relay claims rows with FOR UPDATE SKIP LOCKED, aggregate writes are version-guarded, the consumers have stable groups and an inbox) but has not been load-tested; the optional HPA (autoscaling.api.*) is off by default.

The chart tests are charts/order-sourcing/tests/render_test.sh (needs helm) and test_service_selectors.py (helm and PyYAML); the chart-tests CI job runs both on every push and the helm-lint job runs ct lint on pull requests. The packaging checklist is Before the first release.

helm template order-sourcing charts/order-sourcing -f charts/order-sourcing/ci/default-values.yaml # dummy values, nothing is dialled
bash charts/order-sourcing/tests/render_test.sh
python3 charts/order-sourcing/tests/test_service_selectors.py

Migrations​

Five migrations are embedded in the binary (internal/adapters/outbound/postgres/migrations/): 0001_order_sourcing_schema creates the 14 tables, 0002_idempotency_global_key re-keys idempotency_keys by key alone (adding fingerprint), 0003_rm_lane_wave_cost adds the declared cost columns to rm_lanes and rm_waves, 0004_rm_listings_owner_ref adds rm_listings.owner_ref, and 0005_sourcing_policies adds sourcing_policies (one jsonb snapshot per published policy version). They run at boot in all three binaries through golang-migrate over MIGRATIONS_DATABASE_URL, so a config change or a new image is a rollout that migrates before serving. State lives in schema_migrations; a failed partial migration leaves dirty = true and every later boot fails until the schema is repaired by hand and the version forced with the golang-migrate tooling (take a backup first).

Health​

  • GET /healthz is liveness: always 200 {"status":"ok"}; it never checks dependencies and never flips during shutdown.
  • GET /readyz (cmd/api only) is readiness: 200 {"status":"ready"}; 503 {"status":"not_ready"} once shutdown began draining; 503 problem+json slug dependency-unavailable naming the failing dependency while Postgres (pool ping) or Kafka (broker metadata) is unreachable. Verdicts are cached for 3 s, one round is bounded at 2 s.
  • Writes while a dependency is down (per code): the REST write path never dials Kafka. The aggregate and its outbox_events row commit in one Postgres transaction and only the relay publishes, so the process still accepts and queues writes while Kafka is down; /readyz turning 503 removes the pod from Service endpoints, so new external traffic stops at the router. With Postgres down every write fails (each is a Postgres transaction) and the sweep, relay and consumers only log errors.

Outbox relay​

cmd/api drains outbox_events to Kafka every OUTBOX_RELAY_INTERVAL (default 1 s, batch 100, another pass at once after a full batch), at-least-once, with the CloudEvents id fixed at encode time so a retry republishes the same id.

-- backlog and oldest waiting row
SELECT count(*), min(created_at) FROM outbox_events WHERE published_at IS NULL;
-- rows parked at the attempt cap (use your OUTBOX_MAX_ATTEMPTS, default 20)
SELECT id, topic, event_type, attempts, last_error FROM outbox_events WHERE published_at IS NULL AND attempts >= 20 ORDER BY id;
-- re-arm one row after fixing the cause
UPDATE outbox_events SET attempts = 0 WHERE id = <id>;

A row whose failure count reaches OUTBOX_MAX_ATTEMPTS is excluded from claims, stays in the table with attempts and last_error, and is logged once at ERROR with outbox_id, topic, event_type and key. Later rows overtake it. 0 means unlimited.

Dead letters​

A consumer that cannot apply a message writes it to <topic>.dlq with the original key, value and headers plus x-dlq-source-topic, x-dlq-source-partition, x-dlq-source-offset, x-dlq-error and x-dlq-failed-at. Two paths lead there: a payload that can never be applied (dead-lettered at once) and a transient failure that outlasted 5 attempts. Messages that are not CloudEvents, have a type this service does not handle, or are stale for the aggregate are skipped, not dead-lettered. Nothing in this repository reads or replays a .dlq topic. To replay, fix the cause, then publish the dead-lettered value back to the source topic with your Kafka tooling; the inbox on the CloudEvents id makes a message that was in fact applied a no-op.

Virtual-queue sweep​

Every SWEEP_INTERVAL (default 15 s) the sweep releases due assignments and re-optimises the queued ones page by page (100 rows). It logs queue sweep with released, reoptimised and skipped only when it did something, and queue sweep pass failed at ERROR on a database error. To force one assignment, call POST /assignments/{id}/reoptimisations with an Idempotency-Key header (a refusal other than malformed-request and assignment-locked is not stored: the same key retries it) (API overview). There is no REST operation that releases early.

Alerts and dashboards (from warehouse-infra origin/develop)​

  • Grafana dashboard warehouse-order-sourcing (terraform/dashboards/contexts/order-sourcing.json): Kong request rate, 5xx ratio and p95 latency for the context's routes; service HTTP rate and p95 by process; the business counter sourcing_groups_sourced_total by outcome; Go goroutines and memory; the log stream, log volume by level and an errors-and-warnings panel from Loki ({app="order-sourcing"}). Some panel text still mentions a projector and reports component that this service does not have.
  • Alert rule groups warehouse-order-sourcing (WarehouseKong5xxRatioHigh warning after 5 m, WarehousePodRestarting warning) and warehouse-order-sourcing-slo (WarehouseApiAvailabilityBurnFast and WarehouseApiLatencyBurnFast critical after 2 m, WarehouseApiAvailabilityBurnSlow and WarehouseApiLatencyBurnSlow warning after 15 m) in terraform/alerting/warehouse-alert-rules.json. There is no outbox-lag, consumer-lag or dead-letter alert for this context.
  • Those alerts carry runbook_url https://github.com/IQVO/order-sourcing/blob/develop/docs/operations/runbook.md, a path that no longer exists once this site replaces it; the rules live in warehouse-infra and must be updated there.

See Observability for the instruments, spans and log fields.

Failure modes​

Symptom-by-symptom tables are in Troubleshooting. The short list:

  • Pod crash-loops: a mandatory variable is missing (the error names it), Postgres migrations or ping exhausted bootretry, or a background worker died. kubectl -n warehouse-systems logs deploy/order-sourcing shows the cause.
  • Postgres unreachable: /readyz 503 naming postgres, /healthz stays 200 so the pod is not restarted, writes fail, events already in outbox_events wait.
  • Kafka unreachable: /readyz 503 naming kafka, writes are still accepted and queued in outbox_events, the relay retries every interval. Restore Kafka and the outbox drains by itself.
  • A sibling has never produced: its topic does not exist yet, its consumer retries with only logs. Retries at boot are expected until every sibling has produced once.

Routine procedures​

TaskHow
Restartkubectl -n warehouse-systems rollout restart deploy/order-sourcing (the pod re-runs migrations at boot)
Read logsGrafana Loki {app="order-sourcing"}; JSON fields include consumer, topic, error, outbox_id, trace_id, request_id
Change configurationchange the Helm values (a config change is a rollout); consumer group changes re-read the topic from the start
Inspect an assignmentGET /assignments/{id} or the MCP tool get_assignment (MCP tools)
Publish a sourcing policyPUT /sourcing-policy with an Idempotency-Key and the whole document, for example {"crossRegionSurchargeCents": 250, "maxCostDeviationPercent": 20} (ranges 0..100000 and 0..200, an omitted field is 0 = off; a 422 validation-failed or 409 concurrent-update is not stored, so the retry under the same key runs the publish again; see "Publishing a sourcing policy" below). GET /sourcing-policy shows the current version (version 0 = defaults)
Re-optimise one queued assignmentPOST /assignments/{id}/reoptimisations with Idempotency-Key and optionally {"assignmentVersion": N}
Prune bookkeeping tablesnothing prunes idempotency_keys, processed_events or published outbox_events; plan retention in SQL yourself (keep processed_events as long as a topic can be re-read)

Publishing a sourcing policy​

The sourcing policy (ADR 0014) trades a bounded cost deviation for in-region fulfilment. Until a version is published nothing changes: version 0 is the default (both levers 0, off).

  1. Read the current one: GET /sourcing-policy (version: 0 when nothing was published).
  2. Publish the whole document: PUT /sourcing-policy with Idempotency-Key: <one per logical publish> and a body such as {"crossRegionSurchargeCents": 250, "maxCostDeviationPercent": 20}. crossRegionSurchargeCents (0..100000) is added to the effective cost of a candidate whose node region differs from the delivery area region; maxCostDeviationPercent (0..200) is the budget: an in-region candidate within cheapest + floor(cheapest * percent / 100) beats a cheaper cross-region one. The answer is the new version (n+1). A range error is 422 validation-failed; a lost race with another publisher is 409 concurrent-update (retry with a new key).
  3. The next search of every queued assignment (the sweep re-optimises each one every SWEEP_INTERVAL) uses it; releases and FulfillmentSourcingDecided carry policyVersion, and effectiveCostCents when a surcharge changed the cost.
  4. Watch ordersourcing_sourcing_policy_version (the version last read) and ordersourcing_budget_preferred_total{kind="region"} (releases the budget decided) in Grafana, and the cost-to-serve report of cmd/os-reports.
  5. To switch it off publish {} (both levers 0): that creates a new version, versions are never deleted.

Turning consolidation on (ADR 0015)​

Carrier and follower consolidation merges the QUEUED CONSOLIDATED groups of one shopper into one release. It is OFF until consolidationMaxAssignments is published as 2 or more (0 and 1 are off; the range is 0..10) AND customer-ordering sends a consolidationKey on CustomerOrderPlaced.

  1. Do NOT publish it before the consumers handle a merged release: vendor-connectivity (withdraw consumer), linehaul-transportation (one booking per carrier), delivery-promise and customer-ordering (one tracking number for several groups).
  2. Publish the whole document: PUT /sourcing-policy with Idempotency-Key and a body such as {"crossRegionSurchargeCents": 250, "maxCostDeviationPercent": 20, "consolidationMaxAssignments": 4}. An omitted lever is 0, so repeat the other two.
  3. Watch ordersourcing_consolidated_releases_total, ordersourcing_consolidated_assignments_total (followers absorbed), ordersourcing_consolidation_withdrawn_total{reason}, ordersourcing_merged_cancel_refused_total (shopper cancels refused while a merge is live) and ordersourcing_stale_accept_total (an acceptance for a withdrawn release: expect near zero).
  4. To switch it off publish consolidationMaxAssignments: 0. Releases already merged stay merged until they lock, are refused or are withdrawn; nothing is un-merged by the switch.

No environment variable, topic or chart value is involved; the analytics topic already exists.