transfer-ledger
In DevelopmentA double-entry ledger where moving money cannot be done twice or by mistake
Role: Solo developer
Solo · Since Nov 2026 · Public repo
A .NET API over PostgreSQL with accounts addressed by alias (a phone number or an email), double-entry bookkeeping, idempotency keys and a reconciliation endpoint that checks the numbers add up. It is inspired by the idea of instant payments by alias and does not follow any real payment system's specification.
Problem
Code that moves money has to survive a client that retries, a thousand requests at once and a bug in its own checks. A balance column that is updated and nothing else gives you nothing to compare against when the numbers stop adding up.
Solution
Every transfer writes two entries that sum to zero, and a deposit is a transfer from a bank account that may go negative. The idempotency key is inserted in the same transaction as the transfer, so a repeated request waits on the unique index and gets the stored answer. The database also refuses what does not add up: a check on negative balances, a deferred trigger on entries that do not sum to zero, and append-only tables.
Not done yet
It follows no real payment specification, has one currency, no fees, limits, fraud checks or verified identity behind an alias, and one shared API key instead of users. Idempotency keys never expire, I did not measure throughput and it is not deployed.
Architecture highlights
- 60 concurrent transfers against a balance that covers 30: exactly 30 succeed, the balance never goes negative and the reconciliation stays clean. Without the row lock the test fails
- 40 simultaneous requests with the same idempotency key leave one transfer. Without the ON CONFLICT on the key, five idempotency tests fail
- Reconciliation reads only the data: all entries must sum to zero and each balance must equal the sum of its entries. A test edits a balance by hand and the check flags exactly that account
- The database is the last defense: a deferred trigger rejects entries that do not sum to zero at commit, and UPDATE or DELETE on entries and transfers fail
- Removing the ORDER BY id on the account lock changed nothing in my cross-transfer test, because Postgres scans rows in a stable order. I kept it as an explicit defense and wrote that in the code instead of claiming a test proves it
- Running the tests the first time exposed two of my own bugs: my validation rejected the bank account's own alias, so no deposit worked, and SUM of bigint returns numeric in Postgres