Inventory Reservation System

Correct checkout under contention

LLD · Transaction design

An inventory and order API that keeps stock correct when buyers compete, clients retry, and reservation expiry races with checkout. Domain rules, lock ordering, and an auditable ledger work together.

Design decisions

  • Reserves multi-product baskets in one transaction, with deterministic product locks and database constraints that prevent overselling.
  • Models confirmation, cancellation, and expiry as explicit transitions; a reservation can only reach one terminal outcome.
  • Uses tenant-scoped idempotency keys and bounded transaction retries to keep duplicate requests from duplicating stock movements.
  • Reconciles materialized stock against inventory movements and active reservations, with a restart-safe expiration worker.

Tradeoffs

Pessimistic locks make scarce-stock decisions explicit. Transactions stay short; payment-provider calls are outside this domain.

Local verification

16 tests · 100 buyers, 10 units, exactly 10 reservations

Read the verification notes ↗

Technologies

  • Python
  • FastAPI
  • PostgreSQL
  • Alembic
  • Docker
  • pytest

Source and documentation