ADR 0010: Stock is stored as current state with an immutable journal, not event-sourced
Status: Accepted. The combined authentication and inventory journal is superseded by ADR 0049.
Context
Almost everything a warehouse does reads current stock. Stock selection, location finding, reservation and replenishment all query the rows as they are now, with joins, ordering and filters, and an operator scanning a location needs the amount that is there at that moment, not a value that will be correct once a projection catches up.
Every stock change also has to be traceable afterwards: who moved what, when, from where to where, and what the amount was after the change. Disputes, counts and investigations depend on it.
Karyo is one process over one database (ADR 0001, ADR 0004), so a stock change and the record of it can commit together in one local transaction.
Decision
- PostgreSQL rows are the source of truth. Stock units, unit loads and every other domain object are ordinary JPA entities updated in place.
- Every stock change writes an
InventoryJournalrow in the transaction of the operation that made the change, throughJournalService. A journal row is a denormalised snapshot: product number and name, lot, serial number, the from and to unit load and location names, the amount moved, the stock unit's amount afterwards, the operator and a correlation string. Record types areCREATED,CHANGED,PICKED,TRANSFERRED,COUNTEDandDELETED. - Journal rows are immutable. Every column is mapped
updatable = false. - A journal row belongs to the goods, not to the actor. Its
client_idis the stock's goods owner, and the acting principal supplies only the operator name, so an owner's audit log shows a movement made by the operating company's staff. - The journal is an audit trail, not an event store. Nothing rebuilds entity state by replaying it. Notifications that leave the process go through the outbox (ADR 0009), which is not a source of truth either.
Consequences
- Operational queries read current state directly, with ordinary SQL, and see every committed change immediately.
- A journal row is self-contained. Names are copied at write time, so renaming a product or a location later does not rewrite history, and an audit query needs no join to current state.
- The stock row and its journal row commit or roll back together; there is no window in which one exists without the other.
- There are no temporal queries for free. "What was on this location at 15:00?" means working back from current state through journal deltas; no snapshot store exists.
- Immutability is enforced by the entity mapping, not by the database: the migration declares no
trigger or grant that would stop an
UPDATEissued outside JPA. - The table is declared
PARTITION BY RANGE (created)with a singleDEFAULTpartition. No monthly partitions and no archival or retention job exist, so every row lands in the default partition. The shape allows partitions to be added later without a rewrite; it is not a retention mechanism. - Authentication events belong to the separate Company-wide sign-in log (ADR 0049). The inventory journal holds operational facts only.
- The
correlation_idcolumn holds the caller's correlation. A service that writes a row for a business reason sets it (an order number, a pick order number,cross-dock:{orderNumber}) and that value stays. A row the service labels with nothing takes theX-Correlation-Idof the request that wrote it, soGET /api/v1/journals?correlationId=finds those rows with the value a client sent. A row written outside a request, such as a purge, has no header to take.
Alternatives considered
- Full event sourcing, with events as the source of truth and current state as a projection. Rejected. Stock selection and location finding need immediately consistent current state and complex SQL over it; maintaining those as eventually consistent projections adds projection management, snapshotting and event versioning for state reconstruction, and the eventual consistency is unacceptable on the paths where an operator is waiting at a location.
- Event sourcing for inventory only, CRUD elsewhere. Rejected. Inventory has the most complex queries, the highest write rate and the strictest consistency requirement in the product, so it is the module where event sourcing would cost most. The journal already provides the audit trail that motivates event sourcing there, and two data patterns across modules would raise the cost of every cross-module change.
Evidence
services/inventory-service/karyo-inventory-core/src/main/kotlin/com/karyo/inventory/domain/model/InventoryJournal.kt,InventoryJournal- the entity, every columnupdatable = falseservices/inventory-service/karyo-inventory-api/src/main/kotlin/com/karyo/inventory/api/vo/JournalRecordType.kt,JournalRecordType- the operational record typesservices/inventory-service/karyo-inventory-core/src/main/kotlin/com/karyo/inventory/service/JournalService.kt,JournalService.record-record, the general write method; the class also has dedicated methods for purges and for trashing and reviving unit loadsservices/inventory-service/karyo-inventory-core/src/main/kotlin/com/karyo/inventory/service/StockService.kt,StockService.createStock- a@Transactionalstock change writing its journal row; every stock-changing method in the class follows the same shapeservices/karyo-app/src/main/resources/db/migration/inventory/V104__create_inventory_journals.sql,inventory_journals, inventory_journals_default- the partitioned table and its one default partition- ADR 0049 - sign-in events moved to the Company log
services/karyo-app/src/main/kotlin/com/karyo/app/auth/TenantFilter.kt,TenantFilter.usableCorrelationId- the correlation header, put intoTenantContext.correlationIdand the log context
Related
- ADR 0004 - the one database the journal lives in
- ADR 0009 - the outbox, which carries notifications rather than state
- ADR 0011 - reporting reads the same current state
- Stock model and states - what the journalled operations do
- Data and persistence - the schema the journal sits in