Explore / Backend system design

Design a Payment System

Overview

Design the payment system for a marketplace such as an online store with third-party sellers, a ride-hailing app or a booking site. A buyer pays at checkout, the platform keeps a fee, and the seller is paid out later. Card details and bank transfers go through an external payment service provider (PSP) such as Stripe, Adyen or PayPal; you don't build a card network.

This question is not about throughput. Payment volume is small next to a social feed or a chat app. It is about correctness when every network call can fail: a request to the PSP times out and you don't know if the card was charged, the user double-clicks Pay, a webhook arrives twice and out of order, and at the end of the day your numbers must match the bank's to the cent. Write your answer in the Write tab and sketch the flow in the Diagram tab.

Requirements

Functional

  • Charge a buyer for an order with a card or a saved payment method, through one or more PSPs.
  • Support authorize now, capture later (for example, capture when the item ships), and cancel an authorization.
  • Full and partial refunds.
  • Pay sellers their share minus the platform fee, on a schedule.
  • Show payment status to buyers, sellers and support staff, and keep a full history.

Non-functional

  • Never charge a buyer twice for the same order, whatever retries happen.
  • Never lose money's trail: every cent in and out is recorded and can be explained.
  • Strong consistency for payment state and balances. Eventual consistency is fine for dashboards and analytics.
  • High availability of checkout (a failed checkout is lost revenue), with latency of a few seconds at most, mostly spent at the PSP.
  • Security and compliance: card numbers never touch our servers; every change is auditable.

Estimates

Assume 10 million payments a day, peaks of 10× the average during sales, about 5 ledger entries per payment (charge, fee, seller share, later payout and so on), and that financial records are kept for 7 years. Work out:

  • Average and peak payments per second.
  • Ledger rows per day and storage per year.
  • Whether this needs a sharded NoSQL store, or whether one relational database can do it.

Hints

  • A user clicks Pay twice, or your server retries after a timeout. What makes the second attempt harmless?
  • The PSP call times out. Was the card charged? What state do you record, and what do you do next?
  • Should a database transaction stay open while you call the PSP?
  • Why do accountants record every movement twice? What does that give an engineer?
  • The PSP sends "payment succeeded" by webhook before your synchronous call returns. Then it sends it again. What happens?
  • How would you notice that a payment you recorded as failed was actually charged?

Follow-ups

  • The PSP is down in one country for 30 minutes. What happens to checkout there?
  • How do you handle a buyer paying in euros to a seller who is paid in dollars?
  • A seller disputes their payout amount. How do you prove it is right?
  • How would you add a second PSP, and route payments between them?
  • A refund is issued after the seller has already been paid out. Where does the money come from?

Write your answer step by step and compare it with the reference solution on GrindPrep.