Design doc · Payments platform

Idempotent retries for the payouts API

AuthorsPriya Raman, Tomás Ibarra
ReviewersHannah OkaforWei ZhangMarco Lenz (security)
StatusIn review
Created2026-10-02
Updated2026-10-21 · revision 4
Tracking issuePAY-1184

Ledgerline is a fictional company. Figures are illustrative.

0

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.

1

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.

2

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
3

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.

API gatewayAuth, rate limit
Idempotency layerClaim key, compare body hash
Payouts serviceDebit balance, create payout
idempotency_keysPostgres · 24 h TTL
  • replaySeen key, same body: return the stored response
  • 409Seen key, different body: reject
  • 425Key still in flight: client waits and retries
Figure 1. Write path with the idempotency layer. The key is claimed in the same transaction that debits the balance.

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

ColumnTypeNotes
merchant_id, keytext, textPrimary key. Keys are scoped per merchant
body_sha256byteaDetects a reused key with a different request
stateenumin_flight, completed, failed_retryable
responsejsonbStatus code and body, stored on completion
expires_attimestamptzCreated + 24 h. A nightly job deletes expired rows
4

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

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.

6

Rollout plan

  1. Week 1
    Shadow mode

    Record keys and detect would-be duplicates without changing responses.

  2. Week 2
    Enforce for internal and SDK traffic

    Our own SDKs already send keys, covering 71% of write volume.

  3. Week 4
    Enforce 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.

7

Open questions

QuestionOwnerDue
Should replays count against the merchant's rate limitHannah OkaforOct 28
Is 24 hours long enough for clients that retry from nightly batch jobsTomás IbarraOct 30
Do we need a key format check, or accept any string up to 255 charactersMarco LenzNov 4