Aggregate design canvas
Following the ddd-crew Aggregate Design Canvas v1.1.
This context has one aggregate, InterWarehouseTransfer. The planner, the
planning snapshot and the read models are not aggregates (last section).
1. Name
InterWarehouseTransfer (internal/domain/transfer/saga.go, workflow.go).
Identity TransferID, minted as trf-<uuid> by ApproveTransfer; its single
line is <transfer_id>:1.
2. Description
An operator-approved plan to move a quantity of one SKU from an origin site to a destination site, driven by inventory-storage's and fulfillment-execution's replies and facts. It references the origin reservation and the stow allocations but owns neither. Fields are unexported and change only through the command methods, each of which appends an immutable audit entry.
3. State transitions
Source: internal/domain/transfer/saga.go, internal/domain/transfer/workflow.go
ProposeTransfer records DRAFT and PROPOSED; ApproveTransfer then
applies Approve and RequestAllocation in the same unit of work, so a fresh
approval is persisted in ALLOCATING. RECEIVED, UNFULFILLABLE and
CANCELLED are terminal.
4. Enforced invariants
- Origin ≠ destination; quantity > 0; transfer id, idempotency key, sites,
SKU, policy version, proposal as-of, clock and expiry required; the expiry
must be after now (
ErrProposalExpired). Refusals areErrInvalidProposal. Approveonly fromPROPOSEDand strictly beforeexpires_at.- Every illegal transition returns
IllegalTransitionErrorand changes nothing. MarkAllocated: line<id>:1, same origin and SKU, a reservation id, every allocation with stock unit, bin and positive quantity, allocations summing to the transfer quantity, an expiry.MarkUnfulfillable: closed reason (ORIGIN_SITE_UNKNOWN,INSUFFICIENT_USABLE,IDEMPOTENCY_CONFLICT) and matching line.MarkPicked: only fromALLOCATED;0 < picked ≤ quantity, otherwiseErrFactRefused; a short pick is recorded.MarkArrived: only fromIN_TRANSIT, from eitherTransferArrivedorTransferReceiptStaged; the first wins.MarkStowed: only fromARRIVED; same line, destination and SKU; stowed quantity > 0; at least one stow allocation (ErrFactRefusedotherwise).Cancel: only fromDRAFT,PROPOSED,APPROVED,ALLOCATING, with a reason.CancelAfterAllocation: only fromALLOCATED, with a reason; it moves the transfer toRELEASINGand raisesTransferAllocationReleaseRequested(ADR 0022).MarkReleased: only fromRELEASING; the fact must carry this transfer's line, reservation id, origin, SKU and quantity and an outcome ofRELEASEDorALREADY_RESOLVED(ErrFactRefusedotherwise); it clears the reservation id, allocations and expiry (ADR 0023).- Persistence mirrors the rules with named CHECK constraints (closed state
enum, origin ≠ destination, reservation id only from
ALLOCATEDon, picked quantity only fromPICKEDon and never above quantity), a UNIQUE idempotency key and optimistic concurrency onversion.
5. Corrective policies
- Replies and facts for an unknown transfer, an illegal transition or a refused fact are logged and committed past.
- Transient failures are retried on the same message (200ms doubling to 5s).
- A reply or fact the aggregate refuses (
ErrReplyRefused,ErrFactRefused) is published raw to the topic's.dlqand committed past, never retried (ADR 0013; see Troubleshooting). - Stuck transfers are only observed (
TransferStuckDetected); nothing cancels, retries or releases automatically. allocation_expires_atis stored but no policy acts on it.
6. Handled commands
| Command | Entry point | Methods |
|---|---|---|
| Approve a proposal | POST /v1/transfer-approvals | ProposeTransfer, Approve, RequestAllocation |
| Apply allocation | Kafka TransferStockAllocated | MarkAllocated |
| Apply rejection | Kafka TransferStockAllocationRejected | MarkUnfulfillable |
| Apply pick | Kafka TransferPicked | MarkPicked |
| Apply dispatch | Kafka TransferDispatched | MarkDispatched |
| Apply arrival | Kafka TransferArrived or TransferReceiptStaged | MarkArrived |
| Apply stow | Kafka TransferStockStowed | MarkStowed |
| Cancel | POST /v1/transfers/{id}/cancellation (ADR 0022) | Cancel (before allocation), CancelAfterAllocation (from ALLOCATED) |
| Apply release | Kafka TransferStockAllocationReleased | MarkReleased |
7. Created events
| Domain event | Raised by | Wire type |
|---|---|---|
PlanApproved | Approve | ...transfer.TransferPlanApproved |
AllocationRequested | RequestAllocation | ...transfer.TransferAllocationRequested |
ReleaseRequested | CancelAfterAllocation | ...transfer.TransferAllocationReleaseRequested |
DemandReleased (pick) | ApplyTransferAllocation after MarkAllocated | ...workdemand.WorkDemandReleased |
DemandReleased (dispatch) | ApplyTransferPick after MarkPicked | ...workdemand.WorkDemandReleased |
StateAdvanced | derived from new audit entries (StateAdvancedSince) by every use case that changes a transfer's state | ...saga.TransferStateAdvanced |
8. Throughput and 9. Size
One transfer = one operator approval, four integration events (two of them
work demands), up to five inbound replies and facts. A completed transfer has
nine audit rows (DRAFT … RECEIVED) and one inter_warehouse_transfer row
with JSONB allocations and stow allocations. The repository contains no
throughput or size measurement.
Not aggregates
planning.SiteCapability,SiteSkuDemand,PublishedCapacityPlan: local read-model facts with validation andSupersedes.planning.BuildSnapshot/PlanningSnapshot: a pure domain service and its value.transfer.Planner,Position,Policy,Lane,Proposal,ScoreBreakdown: deterministic value objects and service.transfer.ValidateApproval,StuckCheck,StuckThresholds: pure services.transfer.RebalanceRun: a persisted record of a scheduled pass.