Skip to content
Aadisharma1Public

About

High-concurrency core-banking-style ledger REST API with pessimistic locking, idempotency, and live concurrent-transaction verification

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

LedgerX

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.

Implementation honesty

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.

Architecture

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)]
Loading

API

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

Setup / Usage

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

Concurrency guarantees tested (live, parallel)

  • 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)

Sample output format

=== 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.

Verification

pytest

Known limitations

  • 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).

About

High-concurrency core-banking-style ledger REST API with pessimistic locking, idempotency, and live concurrent-transaction verification

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages