Payments Ledger

- Backend Engineer
- November 2026
- Completed
In numbers
- Transaction from 50 simultaneous retries with the same key
- 1
- Deadlocks with 32 workers moving money both ways
- 0
- Median post across 1,000 accounts, 8 concurrent clients
- 2.4ms
- p99 when 32 clients fight over 10 hot accounts
- 207ms
About the Project
Payments Ledger is the core of a payments system: accounts, transfers and the guarantees that keep money from appearing, disappearing or moving twice. Every transaction is a set of entries that sum to zero per currency, posted atomically and exactly once per idempotency key.
A transfer is one Postgres transaction. It first inserts the idempotency key, so a concurrent retry with the same key waits and then gets the original result back. It locks the accounts involved in id order, so two opposite transfers can never wait on each other. It checks currency and overdraft, which a database constraint also enforces, writes the entries, updates the balances and inserts an event into an outbox table. Relay workers claim outbox rows with FOR UPDATE SKIP LOCKED and publish each one. It is Go 1.27 with the standard library router, pgx, errgroup and slog, and about 300 lines of hand-written SQL instead of an ORM.
The tests prove the guarantees against a real Postgres, with the race detector on: 50 simultaneous retries with one key produce exactly one transaction, 32 workers moving money in both directions never deadlock and never lose a cent, and no outbox event is published twice. CI fails if any of those tests is skipped. I built it in November 2026 to show correctness under concurrency, the part of backend work that a demo never shows.
How it works
The path a request takes, step by step.
- ClientPOST /transactions with an Idempotency-Key
- APIGo net/http, entries must sum to zero
- PostgresKey, ordered row locks, entries and balances
- OutboxThe event, in the same transaction
- Relay workersFOR UPDATE SKIP LOCKED
- EventPublished once
Decisions
The choices that shaped it, and what each one cost.
Lock accounts in id order
- Every transfer locks its accounts sorted by id, so two opposite transfers can never wait on each other.
- Pessimistic locking: on 10 hot accounts, more concurrency adds no throughput and the p99 jumps to 207 ms.
Claim the idempotency key first
- The key is the first insert of the transaction, so a concurrent retry waits on it and then replays the result. 50 simultaneous retries posted once.
- A failed post must not burn the key, which is one more path to handle and test.
Publish through a transactional outbox
- The event is written in the same transaction as the money, so there is never a transfer without its event or an event without its transfer.
- One more table and a relay to run, and consumers still need to dedupe if a relay dies between publishing and marking.
Hand-written SQL instead of an ORM
- About 300 lines of SQL keep every lock and its order visible in the code.
- Every query is written and mapped by hand.
