KatInterview

Designing a Safe Card-Charging Flow with Retries and Storage

System DesignModel answer
MediumModel answerSystem Designidempotencypaymentsretries

Last updated

Design a backend flow for charging customers through an external payment provider so no one is billed twice and no payment is lost, then pick what to persist and which database to use.

Your backend service must begin collecting money through an external payments provider (think a hosted card-processing API). Each charge pays for exactly one existing order, with a known amount and currency. You are asked to design the integration and then defend how robust it is.

Questions you may want to settle first

  • Does the customer wait on the checkout page for the result, or may the payment finish in the background?
  • Do raw card numbers ever touch your servers, or does the provider's client-side form collect them?
  • Are refunds and subscriptions in scope, or only one-time charges?
  • What does the user see while the result of a payment is still unknown?

Part 1: Failure handling

Describe what can go wrong when your service asks the provider to charge a customer, and how your code reacts to each case. The goals: a customer is never charged twice, and a successful payment is never lost.

Your answer should address:

  • Which failures are safe to retry and which are not
  • How to make a retry safe when you do not know whether the first request succeeded
  • The retry policy, and what happens once retries run out
  • How results that arrive late, duplicated, or out of order get applied

Part 2: Persistence

Which payment data must your service store, and which database would you choose? Justify the choice using ACID guarantees and availability.

Your answer should address:

  • The records and fields, how they connect to your orders and to the provider's identifiers, and what you must never store
  • Database constraints that enforce idempotency and legal state transitions
  • A reasoned database choice with a clear stance on consistency versus availability for payment data

Hints

Hint 1

Start from the hardest case: the request timed out and you cannot tell whether the provider charged the card. What property of the request lets you send it again safely?

Hint 2

Persist your own payment record in a 'pending' state before calling the provider, and update it after. Treat the provider's asynchronous notifications as the source of truth, and make handling them safe to repeat and safe to receive out of order.

Hint 3

For storage, think about which uniqueness rules and conditional updates the database itself can enforce, and why a payment ledger should favor consistency over availability.