Personal project · Backend
Idempotent Payment Ledger API
A payment and refund API where a retried or duplicated request is processed exactly once, and every transaction must balance before it commits.
The problem
When a payment response is lost, the client retries, and two identical requests can arrive at the same moment. Handled naively, either case can charge twice or leave the books out of balance.
What the API does
- Records payments and refunds as balanced double-entry transactions between simulated accounts.
- Requires an
Idempotency-Keyon every payment and refund; a retry returns the original result. - Rejects a reused key that carries a different request.
- Allows one successful full refund per payment.
- Includes OpenAPI docs, standard RFC 9457 error responses, Prometheus metrics and a one-command Docker Compose setup.
How it works
- ClientSend a payment with an Idempotency-Key header
If the response is lost, the client retries with the same key.
- APIFingerprint the request
SHA-256 over the request’s canonical fields, stored with the key.
- PostgreSQLClaim the key
INSERT … ON CONFLICT DO NOTHING against a unique constraint, so the database decides which concurrent request wins.
Key already claimedSame fingerprint: return the original transaction. Different fingerprint: reject the request (422).
- API → PostgreSQLPost one debit and one credit
Account rows are locked in a fixed id order, and both entries are written in one database transaction.
Insufficient fundsThe payment is recorded as FAILED with no ledger entries.
- PostgreSQLCheck the books at commit
A deferred constraint trigger requires debits to equal credits, ledger entries are append-only, and CHECK constraints reject overdrawn accounts.
CommittedThe result is stored with the key, so a retry returns the original transaction.
Key engineering decisions
- The database settles races. Keys are claimed with a single
INSERT … ON CONFLICT DO NOTHINGon a unique constraint, never check-then-insert. - A key must mean one request. A SHA-256 fingerprint of the request is stored with the key, so a reused key can't replay the wrong result.
- Accounting rules live in PostgreSQL. Triggers require debits to equal credits and keep entries append-only; constraints block overdrafts and duplicate refunds.
- Exact money.
BigDecimalandNUMERIC(19,4); excess precision is rejected, never rounded.
Results & verification
- CI33integration tests in CI
- JUnit 5 and Testcontainers against real PostgreSQL 16, run by GitHub Actions on every push.
- Integration test16 → 1same-key requests → one transaction
- 16 concurrent requests with one key produced one transaction and 15 replays.
- Integration test20 → 10 + 10no overdraft under concurrency
- 20 concurrent payments of 10.00 against a 100.00 balance: 10 posted, 10 failed, no overdraft.
- Integration testRestartsafe retries across restarts
- After an application restart, a retry still returns the original transaction.
Also includes a repeatable k6 load-test script and a SQL check of the ledger invariants.