KatInterview

Designing a Reliable Checkout and Charge Pipeline

System DesignModel answer
MediumModel answerSystem Designpaymentsidempotencystate-machine

Last updated

Sketch a high-level payment backend that charges customers via a third-party provider, guaranteeing no double charges and no orders marked paid without collected funds.

You are asked to sketch, at a high level and within a short time box, the backend for taking payments in an online product. A customer goes through checkout, gets charged, and the purchase is only confirmed after the money has actually been collected.

Your system does not talk to card networks directly. It relies on an external payment service provider (PSP) that handles the card or wallet transaction and reports results back to you.

Requirements

  • Correctness matters more than speed. A customer must never be charged twice for one purchase, and an order must never be marked as paid unless the funds were really collected.
  • Stay at a high level: describe the main components, the end-to-end flow, the data you persist, and how you deal with failures.

What to cover

  1. The flow from the moment the user clicks "pay" until the order is confirmed.
  2. How retries are made safe at each step.
  3. How you model a payment's lifecycle and record money movement.
  4. How you handle the PSP's asynchronous result notifications.
  5. How you detect and repair mismatches with the PSP's records.
  6. What happens when something fails midway (timeouts, crashes, duplicate or missing notifications).

Questions you may want to ask first

  • Is this only money coming in from users, or do we also pay out to sellers?
  • Do we ever touch raw card numbers, or does the provider's hosted page or SDK tokenize them?
  • Which payment methods and currencies are in scope?
  • What is the expected volume and peak rate?
  • Does the user wait for a synchronous result, or can confirmation arrive later?

Hints

Hint 1

Generate a unique key per payment attempt on your side and persist it before calling the provider, so any retry can reuse it.

Hint 2

Model the payment as a state machine (created, pending, succeeded, failed, and so on) and only allow forward transitions; treat provider notifications as events that drive those transitions.

Hint 3

Assume every message can be delayed, duplicated, or lost: make handlers idempotent, verify notification signatures, and run a periodic job that compares your records with the provider's.

Hint 4

Store money as integer minor units with a currency code, and write ledger entries append-only rather than editing balances.