A customer at the chai stall pressed pay once and was charged twice, and nothing in the system was broken. This lesson is why that is guaranteed to happen to you, and what to build so it stops.
Before you start.The cut phone line (CAP, done properly) gave you the one fact everything here rests on: a network drops messages, and it drops them silently, so you have to retry. This lesson is the bill for that advice, because a retry means the work can happen a second time.
Lesson 10 of 115 in System Design
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.
Or start free: a free account opens lessons 2 and 3 of every track.
In this lesson
The evening a customer paid 500 and lost 1,000
1975: two gangs who could not agree on a time
The two honest choices: at-most-once and at-least-once
June 30, 2017: two posts on the same day, both right
Idempotent: doing it twice leaves the till the same
The idempotency key: name the intent before you send it
The evening three ways, and what the retries stopped costing
In a queue: the idempotent consumer
The case those four lines do not cover
What the three strategies cost
Terms it explains: two generals problem, at-most-once, at-least-once, producer, broker, exactly-once processing, idempotent, same, idempotency key, 7,000 against 6,000 owed, the same four retries, idempotent consumer, atomic, TTL, transactional outbox, status column, 409.
Lab
The reply never arrived. Did the money move, and what should the client do?
Pick what your app does when the reply never comes, then walk the three tabs and watch the charged total.