0030. Site-scoped transfer allocation as a command/reply consumer
Status
Accepted
Context
The network-inventory-planning context plans inter-warehouse transfers
to rebalance stock across the fulfillment network (its transfer saga:
Draft → … → Allocating → Allocated → …). Planning must never touch
physical stock itself — inventory-storage is the authoritative owner of
what is held where and how much of it is usable — so the saga's
allocation step is a command/reply exchange: planning publishes a
TransferAllocationRequested command per transfer line, and
inventory-storage answers allocated or rejected.
Three facts about this service made the naive implementation wrong:
-
No site custody existed. Every
StockUnitrow was site-less: a single-SKU usable count spanning the whole fleet. A global count cannot prevent the wrong facility from donating stock to a transfer — the exact failure the network-transfer design warns about. Site scope had to become a recorded, validated fact BEFORE any transfer could be allocated. -
The existing reservation idempotency was demandRef-shaped.
ReserveStock's guard returns an existing ACTIVE reservation for the samedemandRef+(sku, quantity)— a client-retry idempotency for order-management's synchronous calls. A transfer command is a different contract: planning may retry with a NEW event id for the same line, may replay the SAME id after a broker redelivery, and must get a stable answer for the line either way, forever (the ledger is the audit trail of what was decided, not just a dedupe cache). A demandRef lookup cannot carry that. -
An audit found
ReserveStock.Executesaving its mutated StockUnits OUTSIDE theUnitOfWorkclosure — a failed reservation save left stock decremented with nothing holding it. Any new allocation path built on the same shape would inherit the bug, and a transfer allocation touches MORE state (stock, reservation, ledger, outbox), so the blast radius would be larger.
Decision
-
Site custody is a validated, nullable fact.
StockUnitcarries aSiteID(value object, non-empty); migration 0030 adds nullablestock_units.site_id(andbins.site_idfor the registration path). Legacy site-less rows stay exactly as they are — unallocatable for transfers (FindBySKUAtSiteexcludes NULL-site rows) while continuing to serve ordinary demand unchanged. No default site is ever invented: guessing custody would recreate the wrong-facility failure the column exists to prevent. -
A dedicated
transfer_allocationsledger (migration 0031) holds one row per decided transfer line, DB-unique ontransfer_line_id, with the requested quantity, the outcome (ALLOCATED/REJECTED), the reservation correlation (nullable) and the rejection reason (nullable). Replaying a line returns the original outcome without touching stock again; a same-line-id / different-payload command is answered with anIDEMPOTENCY_CONFLICTrejection while the original decision stands. -
The
AllocateTransferStockuse case draws stock viaStockRepo.FindBySKUAtSiteand holds it in the existingReservationaggregate (correlated to the transfer via the demandReftransfer:<transfer_id>:<line_id>). Stock saves + reservation + ledger row + reply-event outbox rows commit in ONE UnitOfWork; the ledger insert's unique constraint is the idempotency anchor. The closed rejection set isORIGIN_SITE_UNKNOWN,INSUFFICIENT_USABLE,IDEMPOTENCY_CONFLICT. -
A new inbound Kafka consumer (
internal/adapters/inbound/kafka, env-gated byTRANSFER_ALLOCATION_CONSUMER_MODE=kafka+DATABASE_URL, group overridable viaTRANSFER_ALLOCATION_CONSUMER_GROUP) consumescom.warehouse.wes.network-inventory-planning.transfer.TransferAllocationRequestedfromwarehouse.network-inventory-planning.events, following the at-least-once atomicity checklist: FetchMessage (never auto-committing ReadMessage), commit only after the handler settles, capped-backoff retry of the SAME message on transient errors, deterministic poison (malformed command, non-CloudEvents, unknown type) logged and committed past. -
Replies are published on
warehouse.inventory.events(through the transactional outbox) ascom.warehouse.wms.inventory-storage.reservation.TransferStockAllocated(key/subject = reservation id; data carries the transfer correlation, per-unit allocations with pick bins, andexpires_at) and…reservation.TransferStockAllocationRejected(key/subject = transfer_line_id; data carries the closed reason). Both are integration-topic only — the analytics projection has no transfer dimension.ReserveStock's atomicity bug is fixed in the same change (stock saves moved inside its UnitOfWork closure) with a regression test.
Consequences
-
Legacy stock is temporarily invisible to transfers. Until rows gain a site (via site-scoped stow or a later backfill), every transfer command against them answers
ORIGIN_SITE_UNKNOWN. That is the intended fail-closed behaviour, not an outage — the alternative (silently allocating unscoped stock) is precisely the bug this prevents. An operator backfill can flip sites on later; nothing in this design blocks it. -
The ledger is append-only per line. A transfer line can never be re-decided, even after its reservation is revoked or expires: the saga's later states (rejection-after-revoke, re-request) must use a new line id from planning. This keeps the audit trail honest and the reply stream replay-safe, at the cost of planning owning line-id freshness.
-
One more consumer group to operate. The group id is env-configurable so a local process can never steal the live deployment's partitions; the feature is dark by default (
TRANSFER_ALLOCATION_CONSUMER_MODE=off) until a deployment opts in. -
The reply events lengthen the integration contract. Both new types are additive and documented in
apis/asyncapi.yamland the CloudEvents ADR catalogue; existing consumers ignore unknown types by rule, so the fleet-wide rollout is decoupled.