Specification · 002-trade-ledger
Append-only trade ledger
The single source of truth: an immutable, gap-free log of every trade, unlimited in history, fast to append and complete to replay.
The ledger is the foundation the whole platform folds over (001).
It appends immutable TradeEvents under a monotonic, gap-free sequence, never mutating or deleting history —
corrections are compensating events. History is unlimited: no retention cap, cold events tiered to
secondary storage while replay stays complete. It offers full and ranged replay, and periodic fold snapshots so
consumers bound cold-start. The hot path is a single-writer append measured in microseconds; durability, gap-freedom,
and bounded hot memory are the reliability envelope.
- I Independent
- Depends only on the umbrella's
TradeEventmodel. Knows nothing about positions, weights, or lots. - N Negotiable
- Outcome (immutable, gap-free, unlimited, replayable, snapshottable), not a named storage engine or log library.
- V Valuable
- Unlocks SC-001 — a durable, replayable source of truth every analytic can trust.
- E Estimable
- Append + sequence + replay + snapshot are well-understood log primitives; the perf targets are measurable up front.
- S Small
- One entity, one writer, five functional requirements; tiering-engine choice is deferred.
- T Testable
- Gap-freedom, replay equality, and append latency are all mechanically checkable.
1 · Context
Per the umbrella, every analytic is a deterministic fold over one ledger. That makes the ledger's guarantees the platform's guarantees: if the log can lose an event, reorder one, or let a stale read tear across a snapshot boundary, every downstream number inherits the flaw. HFT ingest rates and unlimited retention put those guarantees under pressure that a naive append-to-a-list would not survive.
This slice owns the log and nothing else: sequencing, replay, snapshotting, unlimited retention with cold-tiering, and idempotent append. The math that reads the log lives in the sibling slices.
2 · User scenarios & testing
Each story MUST be independently testable.
US1 · Append and sequence P1
As an order-management producer, I want each accepted fill appended and stamped with the next sequence number, so downstream folds have a total order to replay.
Acceptance: appending N events yields sequence numbers 1..N with no gaps and no reordering; a prior
event is never mutated or removed.
US2 · Complete replay under unlimited history P2
As an analytic bootstrapping from zero, I want to replay the entire ledger — including events long since tiered to cold storage — in append order, so a from-zero fold is always possible.
Acceptance: a full replay returns every event ever appended, in sequence order, whether hot or cold-tiered; a
ranged replay [a, b] returns exactly that window.
US3 · Snapshot to bound cold-start P3
As an operator, I want to checkpoint a fold and restore from it, so restart time does not grow with lifetime trade count.
Acceptance: restoring from a snapshot at sequence N then replaying N+1.. equals a full
replay from zero.
Edge cases
- Duplicate delivery. An at-least-once producer re-sends an event with the same client key; the ledger appends it once (FR-005), not twice.
- Cold read during hot append. A replay reaching into tiered storage must not block or tear against concurrent appends to the hot tail.
- Crash between append and ack. An event acknowledged as durable is never lost; an un-acked append may be absent, but never leaves a sequence gap.
3 · Requirements
Conformance keywords (
Functional
The ledger TradeEvent receives
the next value of a monotonic, gap-free sequence, and a persisted event
History
The ledger [seqA, seqB] — returning events in append (sequence) order.
The ledger
Append
Non-functional
Single-writer append on the hot path
Sustained ingest throughput
Durability and integrity
Under unlimited history, hot-tier memory
Out of scope (deferred)
- Deriving net position or cost basis from the log — the ledger stores events, it does not fold them.
- Lot construction and FIFO/LIFO relief over the event stream.
- The concrete durable-storage and cold-tiering engine (mmap segment files, an embedded LSM, or a log service) — an implementation choice for the design.
- Geo-replication and multi-writer sequencing — a later reliability slice; this spec assumes a single writer.
4 · Data model
The ledger persists the umbrella's TradeEvent and adds only the machinery to sequence, tier, and checkpoint it.
TradeEvent- As defined by 001; persisted verbatim, immutable, under an assigned
seq. Snapshot{ seq, foldId, opaqueState }— a checkpoint of one analytic's fold at a sequence, restorable to a state bit-identical to replaying[0, seq].- Tier boundary
- The hot-tail / cold-store split. Transparent to replay: a ranged replay spanning the boundary returns a single ordered stream.
Invariant: sequence numbers are dense and monotonic. A gap is not an anomaly to log; it is a correctness fault under NFR-003.
5 · Success criteria
Appending N events then replaying returns exactly those N events, in order, with sequence
1..N and no gaps — including after some events have been cold-tiered.
Restoring a fold from a snapshot at sequence N and replaying N+1..M yields state
bit-identical to a full replay of [0, M].
6 · Assumptions
A single logical writer owns the append path; multi-writer sequencing is deferred to TBD-cross-region-replication.
Secondary (cold) storage is durable and available; the ledger owns tiering policy, not the storage medium's own durability.
7 · Open questions
None outstanding — the append-only, unlimited-retention, snapshot-and-replay contract is fixed here; the concrete storage
engine is explicitly deferred to the design (TBD-storage-engine) rather than left open.
8 · Conformance index
Auto-built from every <spec-requirement> in this document.
9 · Change log
- Initial draft. Child of 001-portfolio-analytics: the append-only, immutable, gap-free ledger (FR-001) with unlimited history and cold-tiering (FR-002), deterministic full/ranged replay (FR-003), fold snapshot/restore (FR-004), and idempotent append (FR-005). Reliability envelope: append latency (NFR-001), ingest throughput (NFR-002), durability/gap-freedom (NFR-003), bounded hot memory (NFR-004).