KatInterview

Exactly-Once Disbursements Across Flaky Banking Rails

System DesignModel answer
MediumModel answerSystem Designidempotencypaymentsdistributed-systems

Last updated

Design a payout service that never moves money twice despite client retries, parallel workers, and gateway timeouts during peak payroll bursts.

Problem

You are designing the payout backbone for a platform that pays employees and contractors on fixed schedules (for example, the 1st and 15th of each month). In those windows, scheduled batch jobs, partner API clients, and employers pressing "retry" all fire payment requests at once. The money moves over external rails such as ACH, instant-payment networks, or card-style gateways, which can be slow and can drop connections without telling you whether the transfer happened.

The core promise: no payee is ever paid twice for the same logical payment, even if clients retry aggressively, two workers pick up the same job, or the bank call times out.

Functional requirements

  • Submit a payment with an idempotency key, source account, destination account, amount, and currency.
  • Idempotent replay: repeating a request with the same key must return the same response as the first call (whether it succeeded or is still in progress) and must not trigger a second transfer.
  • Status lookup: an endpoint that returns the current state of any payment.
  • Resolve uncertain outcomes: payments whose result is unknown because of gateway timeouts or network partitions must be driven to a definitive state.

Non-functional requirements

  • Correctness: zero duplicate payments under any combination of failures and concurrency.
  • Availability: 99.99% for submission, 99.999% for status reads.
  • Latency: the client gets an acknowledgment in under 250 ms; the actual bank interaction may be asynchronous.

Scale assumptions

  • Peak: 5,000 payment requests per second during cutoff windows.
  • Up to 20 million disbursements on a peak day.
  • Each bank interaction takes roughly 0.5 s to 5 s to complete.

What to cover

  1. API and idempotency keys: request and response shapes, how keys are produced, and where idempotency records live.
  2. Data model and state machine: the states a payment passes through (for instance created, pending execution, sent to bank, settled, failed, indeterminate) and the schema behind them.
  3. Concurrency control: how two identical requests landing on different servers at the same instant are prevented from both proceeding. Compare distributed locks with database uniqueness guarantees.
  4. Failure handling: gateway timeouts, looking up the real outcome, reconciliation jobs, and keeping the ledger consistent.

Hints

Hint 1

Start with the invariant and the trade-off: when the system is unsure, should it retry or wait? Use Little's Law (rate x latency) to see how many bank calls are in flight at peak, and let that drive the sync vs. async decision.

Hint 2

Make the database the arbiter. A unique constraint on (tenant, idempotency key) plus compare-and-set state transitions gives you correctness without relying on a lock that can expire. Think about how batch jobs derive the same key deterministically.

Hint 3

Record intent before calling the bank, and treat a timeout as 'unknown', not 'failed'. Design a dedicated indeterminate state and the ways out of it: query the gateway by your reference, retry with the same gateway key, and daily statement reconciliation.