Skip to content
Minute Campus.

How real companies do itLesson 031 min

How does Stripe stop you being charged twice?

A payment request can succeed on the server while the reply is lost, so the app retries and risks a second charge. Stripe's fix is an idempotency key: the app attaches a random code, and Stripe saves the first result under it. A repeat with the same code returns the saved result instead of charging again.

Remember

Make retries safe: doing it twice should have the same effect as once.

Transcript

The lesson, word for word.

Read it at your own pace, or search the page for a term.

How does Stripe stop you being charged twice? Picture this. You tap pay. The request reaches the server, your card is charged, and then your connection drops before the reply comes back. Your app has no idea what happened, so it tries again. Without protection, that is a second charge. Stripe's fix is called an idempotency key. Before sending, the app makes up a long random code and attaches it to the payment request. Stripe saves the result of the first request under that code. If the same code arrives again, Stripe does not run the payment a second time. It simply sends back the saved result. So the app can retry as often as it needs to, safely. That is what idempotent means: doing it twice has the same effect as doing it once. It is one of the most useful ideas in building reliable software.

Questions? Comment on the lesson on YouTube

New lessons every day.

Follow Minute Campus where you already scroll.