Bugpit

Lessons › From one user to millions

Take the money

A sale brings 4,000 req/s, and 4% are people paying: 160 charges a second at the peak. Your app calls the payment provider while each customer waits, and the provider allows 100 a second.

Play this level

Free with an account. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

You can't add machines to someone else's API. Stripe allows 100 requests a second in live mode, and you can ask for more, but you can't just take it. Past the limit, calls are refused.

The fix is to stop making customers wait. Take the order, put it in a queue and answer straight away. A Worker then charges cards at a rate the provider accepts.

Now one charge can run twice: a worker crashes after charging but before saving, and the job comes back. So each charge carries an idempotency key, and the provider ignores repeats.

The same goes for email. Nobody should wait for a receipt, and a slow provider shouldn't be able to slow your checkout.

What you have to achieve

4,000 requests a second at the busiest hour, 4% of them writes.

What you learn

Open "A day of traffic": charges queue up at the evening peak and clear by midnight. Now look at the bill. Card fees dwarf every machine you run, because they grow with sales, not traffic. That's why big shops negotiate rates long before they tune servers.

Check yourself

The payment provider allows 100 charges a second, and 160 arrive. What fixed it?

Show the answer

A queue, with workers charging at a rate it accepts. You can't add machines to someone else's API. Take the order, answer, and charge in the background.

Why does each charge carry an idempotency key?

Show the answer

So a charge that's retried isn't taken twice. A worker may crash after charging but before saving. The provider ignores a repeat with the same key.

Try it yourself

More in From one user to millions

← Find anything, fast When one service takes you down →