System Design · Practice problem · Medium
Charge a Card Exactly Once
Give each charge its own ID and send that same ID on every retry, so the provider treats a repeat as the charge it already made. A timeout then becomes safe to retry. Build that here, then break it.
Medium means two needs that pull against each other. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.
What is the situation?
You run checkout for an online shop. When a customer presses Pay, the page sends POST /charges with an ID it made for this charge, and your system charges their card through a payment provider. The provider usually answers in about 100 ms and has far more room than your 200 charges a second need. Your checkout page waits 5 seconds, then tells the customer something went wrong. Providers have bad moments, and when one goes quiet your server cannot tell whether the card was charged. Design the path from POST /charges to the provider, so that every customer hears back quickly, every card is charged, and no card is charged twice. Two facts about this canvas. The Provider is a Database standing in for the provider's own record of charges, and a card counts as charged when the charge lands there. Its setting "If the same record is saved twice" is how this problem models an idempotency key, which is an ID the provider promises to act on only once: "Keeps one" means your calls carry the charge's ID and the provider honours it, and "Saves it again" means they do not, so a repeat is a second charge. It starts on "Saves it again", because a provider can only honour a key your calls send. And you may tell a customer their payment is pending: once a Queue holds the charge, that counts as an answer, as long as the card is charged in the end.
What must your design do?
- Customers hear back within 1.5 seconds at the 99th percentile on an ordinary day, and within 3 seconds while the provider is in trouble.
- At least 99.9% of customers get an answer, which may be that their payment is pending.
- Every card is charged by the time the run ends, and no card is charged twice.
- The slowest card is charged within 10 seconds normally, 25 after a long provider outage, and 30 if your own machines also crash during one.
- The system costs $525 a month or less.
What does Run test?
Run works without an account. It checks your design against 2 tests you can read before you start:
- An ordinary hour. 200 charges a second for 30 seconds.
- A slow patch. At second 10, the provider became about 300 ms slower for 10 seconds, then recovered.
What does Submit add?
Submit needs an account. It runs 4 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.
Which parts can you use?
Already on your canvas: Provider.
- Load Balancer: about 50,003 a second, $18 a month each
- App Server: about 2,000 a second, $45 a month each
- Queue: effectively unlimited, $25 a month each
- Worker: about 200 a second, $45 a month each
- Database: about 5,000 a second, $120 a month each
- Outbox Relay: about 5,000 a second, $40 a month each
Every connection takes 1 ms, and the budget is $525 a month.
What does this problem leave out?
- How long a provider remembers a key, and what it does when one key arrives with a different amount. This stand-in remembers every ID for good.
- Finding out later what happened. Webhooks, asking the provider for a charge's status and reconciling against its report are not in this simulator, so no test grades them.
- Telling a customer on pending how their payment ended.
- Declined cards, refunds and fraud checks. Every charge here succeeds once it reaches the provider.
- An answer lost on the way back. Here the provider goes slow or goes down, which leaves your server in the same place: it stopped waiting and it does not know.
- A new ID for each attempt. Every attempt and every repeated press here carries the same ID, so that mistake cannot be made on this canvas.
- The provider's rate limit and what each call costs you.
- An App Server crashing. No test here crashes one.
Where does this problem fit?
Its category is Money & transactions: Correctness comes before speed. It practises queues & async, idempotency and reliability & ops.
For the whole system, read the interview breakdown Design a Payment System (Stripe, PayPal).
By GOAT Engineer. Explore System Design · Privacy Policy · Contact.