Order Systems That Do Not Lose Money: A Reference Design and Fault-Injection Evaluation of Consistency Patterns on a Single Relational Database
Order and payment code in web applications is usually written as a sequence of reads, checks and writes that is correct when one user acts at a time. In production the same code meets concurrent buyers, client retries, payment providers that deliver webhooks more than once and out of order, and processes that crash between two steps. This paper (i) defines a failure model with six failure modes that cost money or customers; (ii) presents a reference design for a complete order life cycle (checkout, stock reservation, payment, fulfilment, partial refunds) that combines an explicit state machine, transactional idempotency keys, conditional stock reservation, a guarded webhook inbox, a transactional outbox, a double-entry ledger with unique references, and reconciliation, all on a single PostgreSQL database; (iii) provides an open-source TypeScript implementation; and (iv) evaluates it against common naive designs in seven fault-injection experiments, each repeated ten times. Under contention the naive designs sold up to 50 units of a product that had one unit in stock, created one order per retried submission, left 54% of orders in a wrong final state and overstated revenue by 151% when webhooks were duplicated and reordered, lost 9.5% of events when the process crashed after commit, and accepted refunds far above the captured amount for 99% of orders. The reference design produced no invariant violations in any run, repaired all drift caused by up to 20% lost webhooks, and did so at the cost of roughly 2.7x the median latency of an unprotected checkout when there is no contention.
Authors
- Nitesh Das
Publication Details
- Journal
- Zenodo (CERN European Organization for Nuclear Research)
- Published
- 2026-12-01
- DOI
- https://doi.org/10.5281/zenodo.24716751
- Primary Topic
- Distributed systems and fault tolerance
- Type
- preprint