Design doc · Payments platform
Idempotent retries for the payouts API
Ledgerline is a fictional company. Figures are illustrative.
TL;DR
Clients retry POST /v2/payouts after timeouts, and 0.04% of retries create a second payout. We will require an Idempotency-Key header, store the first response for 24 hours, and replay it for any retry with the same key and body. Duplicate payouts drop to zero for compliant clients; p95 latency grows by an estimated 6 ms.
Context and scope
The payouts API moves money from a merchant balance to a bank account. When a request times out at the edge, clients cannot tell whether the payout was created, so most SDKs retry. In September we created 212 duplicate payouts worth $1.38M in total. Each one needed a manual reversal by the treasury team, which took 2.5 days on average.
This doc covers the write path for /v2/payouts and /v2/transfers. Reads, webhooks and the batch file importer are out of scope.
Goals and non-goals
Goals
- A retried request with the same key never creates a second payout
- Retries return the original response, status code included
- Added p95 latency under 10 ms on the write path
Non-goals
- Exactly-once delivery to the bank rails
- Deduplicating requests that arrive without a key
- Changing the public response format
Proposed design
An idempotency layer sits between the gateway and the payouts service. It claims the key in Postgres before any money moves, and records the response when the request finishes.
- replaySeen key, same body: return the stored response
- 409Seen key, different body: reject
- 425Key still in flight: client waits and retries
API sketch
POST /v2/payouts
Idempotency-Key: 6f1c2e0a-8b7d-4c35-9a0e-2d4b1f7c9e11
{ "amount": 125000, "currency": "USD", "destination": "ba_9Qx2..." }
// Retry within 24 h, same key and body
HTTP/1.1 201 Created
Idempotent-Replayed: true
Data model
| Column | Type | Notes |
|---|---|---|
| merchant_id, key | text, text | Primary key. Keys are scoped per merchant |
| body_sha256 | bytea | Detects a reused key with a different request |
| state | enum | in_flight, completed, failed_retryable |
| response | jsonb | Status code and body, stored on completion |
| expires_at | timestamptz | Created + 24 h. A nightly job deletes expired rows |
Alternatives considered
- Deduplicate on amount, destination and a 60-second window
- Rejected. Merchants legitimately send identical payouts minutes apart, for example payroll splits, and we would block them silently.
- Store keys in Redis instead of Postgres
- Rejected. The claim must commit atomically with the balance debit. A separate store reopens the gap we are closing.
- Make the bank rail calls idempotent only
- Rejected. Duplicates are created before the rail call, when the second payout row is inserted.
Cross-cutting concerns
Security
Keys are scoped to the authenticated merchant, so one merchant cannot replay another's response. Stored responses never include full account numbers.
Privacy
Response bodies hold payout metadata only. Rows expire after 24 hours and are excluded from analytics exports.
Observability
New metrics: idem.replay, idem.conflict_409, idem.in_flight_425. Alert if conflicts exceed 0.5% of writes for 15 minutes.
Cost
About 3.1M rows per day at peak, roughly 9 GB of table storage. Estimated $140 per month on the current cluster.
Rollout plan
- Week 1Shadow mode
Record keys and detect would-be duplicates without changing responses.
- Week 2Enforce for internal and SDK traffic
Our own SDKs already send keys, covering 71% of write volume.
- Week 4Enforce for all merchants
Requests without a key are still accepted and logged for 90 days, then rejected with 400.
Rollback The payouts.idempotency flag turns the layer into a pass-through within one deploy. Rows written in the meantime are harmless and expire on schedule.
Open questions
| Question | Owner | Due |
|---|---|---|
| Should replays count against the merchant's rate limit | Hannah Okafor | Oct 28 |
| Is 24 hours long enough for clients that retry from nightly batch jobs | Tomás Ibarra | Oct 30 |
| Do we need a key format check, or accept any string up to 255 characters | Marco Lenz | Nov 4 |