High-concurrency core-banking-style ledger REST API: accounts, deposits, withdrawals, transfers, transaction history, idempotency keys, DB constraints, ACID transactions with pessimistic write locking, multi-instance Docker compose with Nginx load balancing, and live concurrent-transaction tests that fire genuinely parallel HTTP requests and verify the final accounting state.
Status: code complete, NOT YET EXECUTED. Run the verification on your machine; the concurrency report prints measured outcomes only.
The brief specifies Spring Boot. This environment has no JDK/Maven, so the service is implemented in Python/FastAPI with equivalent guarantees. Every component maps 1:1 to a Spring Boot port — see docs/spring_boot_mapping.md. Nothing here pretends to be Spring Boot.
- Locking: SQLite WAL +
BEGIN IMMEDIATE(write lock taken up-front; writers serialize; readers never see uncommitted state). Justification: SQLite's single-writer model with an immediate transaction gives exactly the pessimistic-write semantics the brief wants; the Postgres adapter (SELECT ... FOR UPDATE) is documented, not faked. - Money is stored as integer cents; a
CHECK (balance_cents >= 0)constraint is the backstop behind the application check.
flowchart TD
C[Clients / load test threads] --> N[Nginx upstream LB<br/>docker-compose]
N --> A1[app instance 1<br/>FastAPI + uvicorn]
N --> A2[app instance 2<br/>FastAPI + uvicorn]
A1 --> DB[(SQLite WAL<br/>BEGIN IMMEDIATE<br/>CHECK constraints)]
A2 --> DB
DB -. documented adapter .-> PG[(PostgreSQL<br/>SELECT FOR UPDATE)]
| Method | Path | Notes |
|---|---|---|
| GET | /health |
liveness |
| POST | /accounts |
create (201; 409 duplicate) |
| GET | /accounts/{id}/balance |
|
| POST | /accounts/{id}/deposit |
X-Idempotency-Key honored |
| POST | /accounts/{id}/withdraw |
409 insufficient funds |
| POST | /transfers |
single tx, paired ledger rows |
| GET | /accounts/{id}/history |
newest-first |
| GET | /reports/total |
sum of all balances |
cd ledgerx
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
python scripts/serve.py --port 8100 # run the API
python scripts/smoke_test.py # live happy-path + decline + replay
python scripts/concurrency_test.py # 300 parallel requests + invariants
python -m pytest tests -q # unit + live concurrency suites
# multi-instance + Nginx (requires Docker daemon)
docker compose up --build
curl -s localhost:8080/health- two concurrent withdrawals: exactly
floor(balance/amount)succeed, rest 409 - simultaneous transfers both directions: money conserved
- duplicate idempotency under concurrency: one ledger effect, identical responses
- failed transfers roll back with zero partial ledger rows
- a polling reader only observes consistent committed totals
- no negative balances ever (service check + DB constraint)
=== LEDGERX SMOKE TEST ===
health: {'status': 'ok', 'service': 'ledgerx'}
create account: 201 {...}
...
SMOKE OK
scripts/concurrency_test.py prints a JSON report with
invariants.total_conserved / transfer_pairs / no_negative_balances — each
pass value computed from the live run, and exits non-zero on any violation.
pytest
- SQLite = single writer per database; the compose multi-instance demo shares one SQLite volume, so it demonstrates LB but not write scaling (Postgres path documented above, not implemented).
- Docker images are provided but were not built here (daemon unavailable in the authoring environment).