The whole course ▾

System Design · Mock interview 5 of 20

Design a payment system

Updated 22 Oct 2026 · By Akshay, software engineer, India · 9 min read

Design the payment system for a food delivery app.

Try it first

Before you read the interview, answer the question yourself. Give yourself 10 minutes: say your design out loud, or sketch it on paper. What would you ask? Which numbers matter? Which boxes, and what's the hard part? Then read on, and score yourself with the feedback at the end.

The video for this mock interview is coming soon.

New System Design videos go up at 6 pm. Subscribe on YouTube so you don't miss it.

Got a doubt? Ask in the comments under the video.Subscribe

Requirements

Requirements
Clarifying questions
->
Who moves the money? -> A payment provider
->
Refunds? -> Yes, full and partial
->
Pay restaurants? -> Yes, once a day
->
Scale? -> About 20M payments a day
Functional requirements
Charge the customer at checkout
Refund, in full or in part
Pay each restaurant daily
Show every payment's status
Non-functional requirements
->
Never charge twice
->
Never lose a payment record
->
Strongly consistent
->
Every change explained: auditable
Out of scope
Card networks and banks
Fraud detection models
Many currencies
interviewer
Hi. Today's one is about money. Ready?
candidate
Ready. I'll be careful with this one.
interviewer
Design the payment system for a food delivery app, like QuickBite.
candidate
Okay. Before I draw card networks and banks, I'd like to find out which part of the money flow is actually ours to build. Can I ask a few questions?
interviewer
Please.
candidate
Do we connect to card networks ourselves, or use a payment provider, like Stripe or Razorpay?
interviewer
A provider. We never touch card numbers.
candidate
Do we handle refunds?
interviewer
Yes, full and partial.
candidate
Do we pay restaurants their share?
interviewer
Yes, once a day.
candidate
And what scale?
interviewer
About twenty million payments a day.
candidate
So our job isn't moving money. It's keeping perfect records of money someone else moves.
candidate
Let me write what it must do. Charge the customer at checkout. Refund, in full or in part. Pay each restaurant its share daily. And show the status of every payment, to the customer, to support agents, and to the finance team.
candidate
And how well, which matters more than anything here. Never charge anyone twice. Never lose a record of a payment. Strongly consistent, because money is exactly where stale reads do real harm. And auditable: every change to every balance can be explained, months later, to an auditor.
interviewer
What about speed?
candidate
It matters, but all of those come first.
candidate
I'll leave out the card networks and banks, which the provider handles, fraud detection models, and multiple currencies. Does that match what you had in mind?
interviewer
Yes. And assume the provider is sometimes slow, and sometimes doesn't answer at all.
candidate
Noted. That's going to matter.

Estimation

Estimation
Back of the envelope
20M payments ÷ 100,000 s = 200/s
Peak ~ 3 × = 600 payments/s
~4 ledger rows each -> 80M rows a day
~ 30 billion rows a year
Webhooks: about one per payment
WHAT THE NUMBERS DECIDE
600asecondissmall:the
hardpartiscorrectness.
Theledgergrowsforever:
append-only,partitioned.
candidate
A quick estimate first.
interviewer
How much load is this?
candidate
Let me do the maths, and then say what it means.
candidate
Twenty million payments over a hundred thousand seconds is about two hundred a second, with a peak around six hundred. Each payment writes about four rows to the ledger, which I'll design in a minute. That's eighty million rows a day, and about thirty billion a year, kept for years, because financial records must be. Plus about one webhook call back from the provider for every payment.
candidate
So six hundred payments a second is small. One well-run database handles it. The hard part here isn't scale, it's correctness: retries, unknown outcomes, and records that must always add up. And the ledger only ever grows, so it's append-only, and partitioned by time. I'd still use a queue for things that can wait, like receipts and emails. Never for the charge itself.
interviewer
Good. I like that you're not over-engineering it.

API and data

API and data
Payments
id, order, amount,
state, key
Ledger entries
account, debit or
credit, amount
Accounts
customer, restaurant,
QuickBite
1 : many
many : 1
Endpoints
1
POST /payments +
Idempotency-Key
2
GET /payments/{id} -> status
3
POST /payments/{id}/refunds
4
POST /webhooks <- provider
events
candidate
Next, the API, and then the most important table.
interviewer
What does the API look like?
candidate
Four endpoints, and one header that matters more than all of them.
candidate
POST slash payments takes the order, the amount, and an idempotency key header, which the app generates once per checkout. GET returns the payment's current status. POST slash refunds refunds part or all of it, with its own key. And slash webhooks is where the provider calls us back when it knows how a payment ended.
interviewer
Why does the app make the key, and not the server?
candidate
Because the app is the one that might send the same request twice, on a bad network.
candidate
Three tables. Payments: one row per payment, with its amount, its state, and its idempotency key, under a unique constraint. Accounts: who money can belong to: the provider, each restaurant, and QuickBite itself. And the heart of it, ledger entries: every movement of money, recorded as entries that are never edited and never deleted.
interviewer
How do you fix a mistake, then?
candidate
By adding new entries that reverse it. And I'd use Postgres: transactions, constraints and strong consistency are exactly what money needs.

High-level design

High-level design
Customer app
Payment
service
Payments DB
state machine
Payment
provider
Ledger
append-only
POST + key
1
created
2
charge,
same key
3
succeeded
4
entries
5
paid
6
Payment states
1
Created: saved, not yet sent
2
Pending: sent, outcome unknown
3
Succeeded or failed
4
Refunded, in part or in full
TWO RISKS
Theprovider'sanswer
mayneverarrive.And
ourrecordsmustmatch
theprovider's.
interviewer
Okay. Show me the flow.
interviewer
Walk me through one payment, from tapping Pay to the money being recorded.
candidate
I'll number every step, because here the order matters.
candidate
The customer app sends POST slash payments, with its idempotency key, to the payment service. Before calling anyone, the service saves a row in the payments DB, in state created. Then it asks the payment provider to charge the card, passing the same key along. The provider answers succeeded. The service moves the payment to succeeded, writes the matching entries to the ledger in the same transaction, and tells the app: paid.
interviewer
Why save before calling the provider?
candidate
If we crash right after the call, that row tells us there's a payment to check.
candidate
Every payment moves through a state machine, and only forwards. Created: saved, not yet sent. Pending: sent, outcome not yet known. Then succeeded, or failed. And later, maybe refunded, in part or in full. It can never jump backwards, which protects us when messages arrive late, or twice.
interviewer
And failed is final?
candidate
Yes. The customer can try again, but that's a new payment, with a new key.
interviewer
What worries you about this flow?
candidate
Two things. The provider's answer may never arrive, so we don't know if the card was charged. And every day, our records have to match the provider's.
interviewer
Let's take them one at a time.

Deep dive: never charge twice

Deep dive: never charge twice
Retry as a new request
The provider sees a new charge
The customer may pay twice
The unknown is ignored
Retry with the same key
The provider recognises the key
Returns the first result
Charged exactly once
THE CHOICE
Passourkeytothe
provider,keepthe
paymentpending,andask
beforeassuming.
High-level design
Customer app
Payment
service
Payments DB
state machine
Payment
provider
Ledger
append-only
POST + key
1
created
2
charge,
same key
3
succeeded
4
entries
5
paid
6
webhook
pending -> succeeded
Payment states
1
Created: saved, not yet sent
2
Pending: sent, outcome unknown
3
Succeeded or failed
4
Refunded, in part or in full
TWO RISKS
Theprovider'sanswer
mayneverarrive.And
ourrecordsmustmatch
theprovider's.
interviewer
The first one, then.
interviewer
The call to the provider times out. Was the card charged or not? Do you retry?
candidate
Honestly, we don't know. The charge may have gone through and only the reply was lost. So the payment stays pending, and the question is how to retry safely.
candidate
Option one: retry as a brand-new request. If the first charge did go through, the provider sees a second one, and the customer pays twice. Option two: retry with the same idempotency key. The provider recognises it, doesn't charge again, and returns the result of the first attempt. Providers like Stripe support exactly this.
candidate
So I pass our key through to the provider, keep the payment pending while the outcome is unknown, and retry with backoff. If the retries run out, I ask the provider for the payment's status before deciding anything. Never assume failed, never assume succeeded.
interviewer
What does the customer see meanwhile?
candidate
Confirming your payment. And support gets a list of payments stuck in pending, so nothing is quietly forgotten.
candidate
There's also a second path for the answer: the webhook. When the provider knows how a payment ended, it calls our webhook endpoint, even if our own request timed out long ago. We check its signature, so nobody can fake one, then move the payment from pending to its final state. Webhooks can arrive late, twice, or out of order, so the handler is idempotent, keyed on the provider's event ID.
interviewer
How do you check the signature?
candidate
The provider signs each call with a secret we share. We compute it again and compare, and refuse anything that doesn't match.
interviewer
What if the webhook says succeeded before your own call has even returned?
candidate
That's fine, because of the state machine. The webhook moves the payment to succeeded and writes the ledger entries. When our call finally returns succeeded too, there's nothing left to do. Whichever arrives first wins, and the second changes nothing.
interviewer
And if the customer double-taps Pay?
candidate
The app made one idempotency key when the checkout screen opened, so both taps carry the same key. The first creates the payment. The second hits the unique constraint and gets back the very same payment. One charge, two taps.
interviewer
Nice.

Deep dive: does the money add up?

Deep dive: does the money add up?
Double-entry rules
1
Every movement: equal debit and credit
2
Entries never change; fixes are new entries
3
All entries always sum to zero
4
Balances are sums of entries
High-level design
Customer app
Payment
service
Payments DB
state machine
Payment
provider
Ledger
append-only
POST + key
1
created
2
charge,
same key
3
succeeded
4
entries
5
paid
6
webhook
pending -> succeeded
Reconciliation job
every night
Settlement
report
from the provider
our records
their
records
mismatch -> alert, then
fix
Payment states
1
Created: saved, not yet sent
2
Pending: sent, outcome unknown
3
Succeeded or failed
4
Refunded, in part or in full
TWO RISKS
Theprovider'sanswer
mayneverarrive.And
ourrecordsmustmatch
theprovider's.
interviewer
Now, the second risk.
interviewer
How do you make sure the money always adds up, across millions of payments, refunds and payouts?
candidate
Two ideas, both much older than computers.
candidate
First, double-entry bookkeeping. Every movement of money is a debit and an equal credit. Say the customer pays four hundred and fifty rupees: the provider owes us four fifty, we owe the restaurant four hundred and five, and our commission is forty-five. Entries never change; a correction is a new pair of entries. So all the entries always sum to zero, and any gap is a bug you can find. Balances are just sums of entries.
interviewer
Why cache the balances at all?
candidate
Summing millions of entries on every read would be slow. And since the cache is built from the entries, it can always be checked.
candidate
Second, reconciliation. Every night, a reconciliation job compares our ledger, line by line, with the provider's settlement report, their list of every payment and refund they actually processed. Any mismatch, say a payment they charged but we still show as pending because a webhook never came, is flagged, investigated, and fixed with new entries.
interviewer
So it's a safety net?
candidate
Yes. It catches whatever everything else missed.
interviewer
How does a partial refund of a hundred rupees look in the ledger?
candidate
As new entries, never edits. A hundred moves back from what we owe the restaurant and from our commission, in proportion, and the provider returns it to the card. The original entries stay untouched, so the history shows exactly what happened, and when.
interviewer
You pay every restaurant daily. How do you make sure none is ever paid twice?
candidate
The same tools. Each payout gets an idempotency key built from the restaurant and the date. Running the payout job twice, or retrying a transfer, can't pay twice, because the key is already used. The payout itself is a saga: write the ledger entries, ask the provider to transfer, and reverse the entries if the transfer fails.
interviewer
Good.

Wrap-up

Wrap-up
Next steps
1
Fraud checks on payment events
2
A second provider behind a breaker
3
Watch success rate and stuck payments
4
Never store card numbers: use tokens
INTERVIEWER'S FEEDBACK
Strong:idempotencyendtoend,astate
machine,adouble-entryledger,reconciliation.
Nexttime:thinkofthecustomer'sscreen
earlier.Verdict:stronghire.
interviewer
A few minutes left.
interviewer
What would you improve with more time?
candidate
A few risks I'd harden next.
candidate
First, fraud checks: payment events go to Kafka, where a fraud service scores them. Second, a second provider, behind a circuit breaker, so one provider's outage doesn't stop payments. Third, I'd watch the success rate, payments stuck in pending for too long, and reconciliation mismatches. And fourth, never store card numbers: the provider gives us tokens, which keeps us out of most of the card industry's security rules.
interviewer
Which would you do first?
candidate
The watching. Stuck payments are money, and trust, leaking quietly.
interviewer
Okay, let's stop there. Very good. Your idempotency key travels from the tap all the way to the provider, your state machine makes late and duplicate messages harmless, the ledger always sums to zero, and you reconcile every night. And you said correctness over speed early, which is the right instinct. One thing: think about the customer's screen a bit earlier, not only when I ask. I'd say a strong hire.
candidate
Thank you. That's good to hear.
Interviewer's feedback

Strong: idempotency end to end, a state machine, a double-entry ledger, reconciliation. Next time: think of the customer's screen earlier. Verdict: strong hire.

Score yourself

Interviewers score these seven things. Give your own attempt a tick for each one you did.

  • Asks before drawing. Agrees what the system does, and how well: scale, speed, consistency.
  • Turns users into requests a second and bytes a day, and says what the numbers decide.
  • A small, clear contract, and a data model that fits how the data is read.
  • A working path end to end before any detail. Every box has a reason.
  • Picks the hard parts, compares two options, and chooses one with a trade-off said out loud.
  • What breaks, what happens then, and how users notice. Retries, duplicates, limits.
  • Thinks out loud, checks in with the interviewer, and takes a hint without losing the thread.

0 of 7

Your progress stays on this device.
New System Design videos at 6 pm

Lessons on weekdays, the week's revision on Saturday, a mock interview on Sunday.