KatInterview

Build a Reliable Card Payment Backend with External PSPs

System DesignModel answer
MediumModel answerSystem Designpaymentsidempotencyledger

Last updated

Design a backend that takes a card payment from checkout through an external processor to a recorded, reconcilable transaction, with no double charges and no lost successes.

Build a Reliable Card Payment Backend

You are designing the payment backend for an online business, such as a marketplace checkout or a usage-based billing product. Customers pay by credit or debit card. Your system does not move money itself. An external payment service provider (PSP) does that, and the card is tokenized on the client side so your servers only ever see an opaque token.

Your backend orchestrates everything from the moment a customer presses Pay until a confirmed transaction is recorded. That means:

  • calling the PSP,
  • keeping an authoritative internal record of every charge,
  • making sure retries never charge a customer twice,
  • supporting full and partial refunds,
  • reconciling your records against what the PSP says really happened.

Walk through the end-to-end design: the high-level architecture, the core data model, the write and read paths, failure and retry handling, and how the system scales.

Scope and assumptions

State your own numbers if you prefer. A reasonable starting point:

  • About 10 million customers. Peak load is around 1,000 payment requests per second, with bursts during sales or billing runs, and a much lower average.
  • PSP calls go over HTTPS, have p99 latency in the hundreds of milliseconds, and can time out.
  • Correctness matters more than speed. A payment can take a second or two, but it must never be charged twice and a successful charge must never be lost.
  • Multiple currencies are supported. Full and partial refunds are required. Chargebacks and disputes can be handled out of band.
  • The system must keep an auditable financial record and reconcile against PSP settlement reports.
  • Explicitly out of scope: storing card numbers and PCI scope (delegated to the PSP via tokenization), fraud scoring, and tax calculation.

Questions worth raising up front

  • Are we handling one-off purchases, subscriptions, metered billing, or payouts to third parties?
  • One PSP or several?
  • Must the user see a final result synchronously, or is "we'll confirm shortly" acceptable?
  • What are the refund, dispute, and reporting needs?
  • Are there audit, immutability, retention, or data-residency rules?

What is expected

A structured design that covers the API, data model and state machine, the write path step by step, webhook handling, reconciliation, scaling, and the trade-offs you are making. Pay particular attention to what happens when a PSP call times out or a process crashes midway.

Hints

Hint 1

Rough numbers show that throughput is modest. The difficulty is correctness under partial failure: crashes and timeouts between your database and the PSP. Say this early and design around it.

Hint 2

Use idempotency at every hop: a client-supplied key for client to you, a deterministic key derived from your payment ID for you to PSP, and event-ID dedupe plus monotonic state transitions for PSP to you.

Hint 3

Record the intent (a PROCESSING row) and commit it before calling the PSP, and treat a timeout as 'unknown' rather than 'failed'. A reconciler then resolves unknown payments by querying the PSP.

Hint 4

Model money with integer minor units plus a currency code, and keep an append-only double-entry ledger written in the same transaction as the state change. Fix mistakes with reversing entries, never edits.