Part 10 · 1 chapters · ~8 min

Capstone: The Ledger API in Python

The shared ledger capstone built with Django, DRF and Celery (or FastAPI and SQLAlchemy as a variant): accounts, balanced postings, idempotent transfers, a payout state machine with a simulated rail and enquiry task, tests with pytest and Hypothesis, and a profile.

16

The build

code
ledger-python/
  config/settings.py, asgi.py          production settings, ASGI entry
  ledger/models.py                     Account, JournalEntry, Posting (CheckConstraint), Payout, IdempotencyRequest
  ledger/services.py                   post_entry(): atomic, select_for_update in id order, balanced check
  ledger/api.py                        DRF views: POST /transfers with Idempotency-Key, problem responses
  ledger/tasks.py                      Celery: submit_payout, enquire_unknown (beat every minute)
  tests/test_idempotency.py            100 concurrent identical requests (pytest + threads) → one entry
  tests/test_invariant.py              Hypothesis: Σ postings = 0 after random operation sequences
acceptance: identical to every other language course (Go course part 10), plus py-spy flame graph of the hot path
compare: requests/s at p99 < 50 ms, memory per worker, image size, lines of code