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
| Component | Binary | Chart switch | Port | Exposure in the fleet (warehouse-infra helm-values/order-sourcing.yaml) |
|---|---|---|---|---|
api Deployment | cmd/api | always on | 8080 (Service port 80) | Kong route /api/order-sourcing, prefix stripped |
mcp Deployment | cmd/mcp | mcp.enabled (default off, on in the fleet) | 8090 | Kong route /api/mcp/order-sourcing, rewritten to /mcp |
a2a Deployment | cmd/a2a | a2a.enabled (default off, on in the fleet) | 8093 | Kong route /api/a2a/order-sourcing, rewritten to /; the Agent Card stays under the same prefix |
analytics-projector Deployment | cmd/os-projector | analytics.enabled (default off) | 8091 (admin probes only, no Service) | none |
analytics-reports Deployment | cmd/os-reports | analytics.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 Deployment | nginx serving web/ | frontend.enabled (default off, on in the fleet) | 8080 | the 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, therm_*read models,outbox_events,processed_events,idempotency_keys. Every binary runs the embedded migrations at boot, then pings, each wrapped inbootretry(5 attempts, 15 s of backoff in total). - Analytical Postgres (
analytics.enabled):cmd/os-projectorruns the embedded analytical migration (analytics/migrations/0001_sourcing_facts.up.sql:queued_groups,sourcing_decisions,unsourceable_declarationsandanalytics_processed_events, 4 tables) againstANALYTICS_DATABASE_URL;cmd/os-reportsonly reads it. It is a projection of the context's own topics and can be rebuilt from them. To roll back by hand use0001_sourcing_facts.down.sql. - PgBouncer versus a direct DSN:
DATABASE_URLis 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 toDATABASE_URL. In the fleet both come from the Secretorder-sourcing-db. - Kafka (
KAFKA_BROKERS, comma-separated; one broker platform-wide): used bycmd/apionly (the relay, the nine consumers and the/readyzbroker check). Topics are auto-created by the first writer; this service never creates the topics it consumes.
Kafka topics and consumer groups
| Direction | Topic | Consumer group (variable, fleet value) |
|---|---|---|
| consumes | retailnetwork.customer-ordering.events | CUSTOMER_ORDERING_CONSUMER_GROUP, order-sourcing-customer-ordering |
| consumes | retailnetwork.delivery-promise.events | DELIVERY_PROMISE_CONSUMER_GROUP, order-sourcing-delivery-promise |
| consumes | retailnetwork.last-mile-delivery.events | LAST_MILE_DELIVERY_CONSUMER_GROUP, order-sourcing-last-mile-delivery |
| consumes | retailnetwork.linehaul-transportation.events | LINEHAUL_TRANSPORTATION_CONSUMER_GROUP, order-sourcing-linehaul-transportation |
| consumes | retailnetwork.node-performance.events | NODE_PERFORMANCE_CONSUMER_GROUP, order-sourcing-node-performance |
| consumes | retailnetwork.node-topology.events | NODE_TOPOLOGY_CONSUMER_GROUP, order-sourcing-node-topology |
| consumes | retailnetwork.offer-availability.events | OFFER_AVAILABILITY_CONSUMER_GROUP, order-sourcing-offer-availability |
| consumes | retailnetwork.shipping-and-tracking.events | SHIPPING_AND_TRACKING_CONSUMER_GROUP, order-sourcing-shipping-and-tracking |
| consumes | retailnetwork.vendor-connectivity.events | VENDOR_CONNECTIVITY_CONSUMER_GROUP, order-sourcing-vendor-connectivity |
| produces | retailnetwork.order-sourcing.events | integration events (9 types: the eight integration events and FulfillmentReleaseWithdrawn) |
| produces | retailnetwork.order-sourcing.analytics | analytics-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>.dlq | written 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).
| Value | Variable | Rule |
|---|---|---|
database.url or database.existingSecret (+ existingSecretKey) | DATABASE_URL | Mandatory. Always a secretKeyRef; the chart Secret is created only from database.url; the DSN never lands in the ConfigMap. |
database.migrationsExistingSecretKey | MIGRATIONS_DATABASE_URL | secretKeyRef with optional: true (the binary falls back to DATABASE_URL); empty drops the variable. |
kafka.brokers | KAFKA_BROKERS | Mandatory. |
config.<producer>ConsumerGroup (nine) | <PRODUCER>_CONSUMER_GROUP | Mandatory, stable, service-prefixed (order-sourcing-<producer> in ci/default-values.yaml); a value containing $( (pod-name derived) is refused. |
config.eventPublisher | EVENT_PUBLISHER | Only kafka. |
config.outboxRelayInterval, outboxMaxAttempts, sweepInterval, httpAddr, logLevel | OUTBOX_RELAY_INTERVAL, OUTBOX_MAX_ATTEMPTS, SWEEP_INTERVAL, HTTP_ADDR, LOG_LEVEL | Optional ones are not rendered while empty, so the binary default applies. |
config.shutdownDrainDelay | SHUTDOWN_DRAIN_DELAY | Whole seconds (5s), always rendered. |
terminationGracePeriodSeconds | n/a | Default 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, environment | OTEL_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_ADDR | mcp.httpAddr must equal mcp.service.targetPort. |
a2a.enabled, a2a.httpAddr, a2a.agentUrl, a2a.route.* | A2A_ADDR, A2A_AGENT_URL | Empty 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. |
extraEnv | n/a | Refuses 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 /healthzis liveness: always200 {"status":"ok"}; it never checks dependencies and never flips during shutdown.GET /readyz(cmd/apionly) is readiness:200 {"status":"ready"};503 {"status":"not_ready"}once shutdown began draining;503 problem+jsonslugdependency-unavailablenaming 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_eventsrow commit in one Postgres transaction and only the relay publishes, so the process still accepts and queues writes while Kafka is down;/readyzturning 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 countersourcing_groups_sourced_totalbyoutcome; 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(WarehouseKong5xxRatioHighwarning after 5 m,WarehousePodRestartingwarning) andwarehouse-order-sourcing-slo(WarehouseApiAvailabilityBurnFastandWarehouseApiLatencyBurnFastcritical after 2 m,WarehouseApiAvailabilityBurnSlowandWarehouseApiLatencyBurnSlowwarning after 15 m) interraform/alerting/warehouse-alert-rules.json. There is no outbox-lag, consumer-lag or dead-letter alert for this context. - Those alerts carry
runbook_urlhttps://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 inwarehouse-infraand 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-sourcingshows the cause. - Postgres unreachable:
/readyz503 namingpostgres,/healthzstays 200 so the pod is not restarted, writes fail, events already inoutbox_eventswait. - Kafka unreachable:
/readyz503 namingkafka, writes are still accepted and queued inoutbox_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
| Task | How |
|---|---|
| Restart | kubectl -n warehouse-systems rollout restart deploy/order-sourcing (the pod re-runs migrations at boot) |
| Read logs | Grafana Loki {app="order-sourcing"}; JSON fields include consumer, topic, error, outbox_id, trace_id, request_id |
| Change configuration | change the Helm values (a config change is a rollout); consumer group changes re-read the topic from the start |
| Inspect an assignment | GET /assignments/{id} or the MCP tool get_assignment (MCP tools) |
| Publish a sourcing policy | PUT /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 assignment | POST /assignments/{id}/reoptimisations with Idempotency-Key and optionally {"assignmentVersion": N} |
| Prune bookkeeping tables | nothing 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).
- Read the current one:
GET /sourcing-policy(version: 0when nothing was published). - Publish the whole document:
PUT /sourcing-policywithIdempotency-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 withincheapest + floor(cheapest * percent / 100)beats a cheaper cross-region one. The answer is the new version (n+1). A range error is422 validation-failed; a lost race with another publisher is409 concurrent-update(retry with a new key). - The next search of every queued assignment (the sweep re-optimises each one every
SWEEP_INTERVAL) uses it; releases andFulfillmentSourcingDecidedcarrypolicyVersion, andeffectiveCostCentswhen a surcharge changed the cost. - Watch
ordersourcing_sourcing_policy_version(the version last read) andordersourcing_budget_preferred_total{kind="region"}(releases the budget decided) in Grafana, and the cost-to-serve report ofcmd/os-reports. - 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.
- 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).
- Publish the whole document:
PUT /sourcing-policywithIdempotency-Keyand a body such as{"crossRegionSurchargeCents": 250, "maxCostDeviationPercent": 20, "consolidationMaxAssignments": 4}. An omitted lever is 0, so repeat the other two. - 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) andordersourcing_stale_accept_total(an acceptance for a withdrawn release: expect near zero). - 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.