← All work

Case study: fintech backend

Ledgerline

A wallet can show $100 even while $40 is reserved for a ride. Follow a $32 fare through a double-entry ledger and see what happens when the client sends it twice.

From reservation to payment

Alice deposits $100 into her wallet and authorizes a ride for up to $40. The posted balance records money that has moved. A hold reserves part of that balance, leaving $60 available for other payments.

The final fare is $32. Capturing it pays Bob and releases the unused $8. Alice’s client then retries the capture with the same request body and idempotency key.

Alice’s wallet after each request (USD)
ActionPostedHeldAvailable
Deposit $100$100.00$0.00$100.00
Reserve $40$100.00$40.00$60.00
Capture $32$68.00$0.00$68.00
Retry capture$68.00$0.00$68.00
Reserve $15$68.00$15.00$53.00
Void the hold$68.00$0.00$68.00

The retry returns the original capture transaction ID and the same response body. Voiding a second ride authorization makes its $15 available again. Only the deposit and the fare reached the journal. Their four entries sum to zero across the three accounts.

When a retry arrives mid-transfer

A separate PostgreSQL test starts with $100 and two HTTP requests. The first request to check the key pauses when it finds no record. A second connection uses the same key to move the entire $100. The paused request resumes against an empty wallet.

Checking the funds at that point used to return “insufficient funds” for a transfer that had already succeeded. Ledgerline now claims the key before checking the balance and saves the response in the same database transaction as the journal entries.

The test asks for both outcomes. Repeating the $100 request returns the original transfer with the replay header. Changing the amount to $5 with that key returns HTTP 409. In both runs Bob receives $100 and his statement has one entry.

Run the wallet demo

From a checkout of Ledgerline with uv installed, run these commands. The demo starts a temporary SQLite service and makes real HTTP requests over loopback.

uv sync --locked
uv run python examples/wallet_demo.py --holds

The JSON receipt reports each balance in integer cents. Look for all three replay checks to be true and a zero USD trial balance. The demo stops its server and removes its database before printing the receipt.

Limits

These are fictional accounts in a local service. Ledgerline has no connection to a bank or card network. Each hold permits one capture and releases any unused amount. Expiry is evaluated when the ledger reads a hold.

SQLite serializes writers. The overlapping requests described above run against PostgreSQL, whose default isolation permits a later statement to see a newly committed write.

Code and docs

Back to all projects