Skip to main content

Read models

Read models (backlog depth, actual rate, plan-vs-actual) are PROJECTIONS — not state on aggregates.

That rule is architectural, not stylistic. This page says what the projections are and why the rule holds.

The projections​

ProjectionKeyed byBuilt fromExposed at
Backlog telemetrypath_idcomputed from WorkPool.entries on readGET /paths/{pathId}/telemetry
Rebalance recommendationpath_idcomputed from the same pool snapshot when decidedPOST /paths/{pathId}/rebalance-decisions
LaborPlanObservedpath_idShiftPlanCommitted events from workforce-management, as reported (a pure projection since ADR-0049)GET /paths/{pathId}/labor-plan-view (driftHeads, driftDetectedAt deprecated read-backs)
UsableInventoryObservedskuStockReserved / ReservationRevoked events from inventory-storageGET /inventory-view/{sku}
Product classification copyskuProductClassified events from product-master, applied only when version is newer (ADR-0035)not exposed; read once per WorkReleased as ProductClassificationView

The analytics throughput_rollup is a further, out-of-process projection owned by cmd/wes-projector (Release throughput report).

The two groups are different in kind:

  • Computed-on-read (telemetry, rebalance) — derived synchronously from aggregate state. Nothing is stored; they cannot go stale.
  • Event-sourced projections (LaborPlanObserved, UsableInventoryObserved) — materialised by consuming another context's events. Stored in their own tables (labor_plan_view, usable_inventory_view), never joined to aggregate tables.

Why not just store the numbers on the aggregate?​

It would be easy to keep a backlogDepth int field on WorkPool and increment it. Three reasons that was not done:

  1. Two sources of truth drift. The entries are the truth. A counter beside them is a cache, and any code path that touches entries without touching the counter silently corrupts it. BacklogDepth() counting pending entries cannot disagree with the entries.
  2. It expands the invariant surface for no benefit. A stored counter introduces "counter equals actual pending count" as a new thing to enforce and test — an invariant that exists only because of the optimisation.
  3. Query needs change faster than the model. Plan-vs-actual, actual rate, per-CPT burn-down — each is a new question, and questions should not each require a new field on an aggregate that has to be migrated.

The measured cost of computing on read is a slice scan per call, on a pool that holds one path's queue. That is the right trade at this size, and if it ever stops being, the fix is a projection built from events — not a counter on the aggregate.

The projections keyed by someone else's key​

Two details here were deliberate and are worth stating plainly.

UsableInventoryObserved is keyed by SKU, not by path​

Inventory reservations are SKU-scoped. Everything else in this context is path-scoped, so there was an obvious temptation to force a path key onto the projection for consistency. That mapping does not exist in the real world — a SKU is picked from many bins across many paths — so inventing it would have produced a projection that was wrong in a way no test could catch.

The projection therefore keeps Inventory's key. Its endpoint is GET /inventory-view/{sku}, deliberately not nested under /paths/.

StockReserved decrements the observed usable count for the SKU; ReservationRevoked increments it back. The port exposes a single ApplyDelta(ctx, sku, delta, observedAt) operation so the adjustment is atomic at the adapter, rather than a read-modify-write in application code.

LaborPlanObserved is a value, not an aggregate​

type LaborPlanObserved struct {
PathId shared.PathId
PlannedHeads int
PlannedRate float64
PlannedHours float64
ObservedAt time.Time
DriftHeads *int // deprecated (ADR-0049): read back only, no longer computed
DriftDetectedAt *time.Time // deprecated (ADR-0049): set only when DriftHeads != 0
}

No constructor, no validation, exported fields. That shape is the point: this type protects nothing, because the facts in it are owned by another bounded context. Giving it invariants would mean this service could reject a fact that Workforce has already committed — which is not a decision this context is entitled to make. Since ADR-0049 nothing here compares it with a plan: the two drift fields only carry a drift recorded before that, until the path's next observed plan clears them. The plan and its drift check are warehouse-planning's.

Projection staleness is explicit​

Both event-sourced projections carry ObservedAt, and both endpoints return it. A consumer can therefore tell how old the observation is rather than assuming it is current. Neither projection is read-repaired, back-filled, or reconciled against the source: it is exactly "the last thing that context told us, and when".

If nothing has been observed yet, the endpoints return 404, not a zero-valued body: "zero" and "unknown" are different facts.