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.

  • Java 21
  • Spring Boot
  • PostgreSQL
  • Flyway
  • Testcontainers
  • Docker
  • GitHub Actions
key 7f3a…key 7f3a…key 7f3a…key 7f3a…key 7f3a…16 concurrent requestsPostgreSQLclaim15 replays1 transactionDR A 10.00CR B 10.00balanced ✓key 7f3a…×16claimPostgreSQL15 replays1 transactionDR A 10.00CR B 10.00balanced ✓
Illustration of the documented concurrency test. Account names and amounts are examples.

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-Key on 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

  1. ClientSend a payment with an Idempotency-Key header

    If the response is lost, the client retries with the same key.

  2. APIFingerprint the request

    SHA-256 over the request’s canonical fields, stored with the key.

  3. PostgreSQLClaim the key

    INSERT … ON CONFLICT DO NOTHING against a unique constraint, so the database decides which concurrent request wins.

    Key already claimed

    Same fingerprint: return the original transaction. Different fingerprint: reject the request (422).

  4. 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 funds

    The payment is recorded as FAILED with no ledger entries.

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

    Committed

    The result is stored with the key, so a retry returns the original transaction.

How one payment request is handled. A retry takes the 'key already claimed' branch instead of posting again.

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. BigDecimal and NUMERIC(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.