Concurrency-safe ledger math
- Problem
- Collection value and profit had to stay correct as people bought and sold, with no double counting when writes overlapped across a 3.5M-row history.
- Approach
- The team kept an append-only ledger with FIFO cost basis and idempotency keys, locked rows on write with SELECT FOR UPDATE, and precomputed rollup tables kept current with ON CONFLICT upserts.
- Result
- The numbers stay accurate, reads skip the full ledger, and a nightly drift check has held at zero.




