Doing it twice safely
After this: Say why a job can arrive twice, and write a handler that charges once.
On this page · 7 sections · 3 checks
In lesson 2, a worker crashed before it acknowledged a job, and the queue handed the job out again. That kept the job from being lost. But look at when the crash happened. If the worker had already sent the email, Ana gets two welcome emails. If the job was "charge the customer £20", the customer pays twice. How do you make a job safe to run twice?
Why a job can come twice
The queue cannot tell "the worker crashed before doing the work" from "the worker crashed after doing the work, before it acknowledged". In both cases it sees no acknowledgement, and it hands the job out again. A retry after a failure, as in the last lesson, can repeat work in the same way. And with a time limit on acknowledgements, a worker that is only slow can see the same job handed to a second worker while it is still working on it.
So a queue promises that each job is delivered at least once: never lost, but sometimes delivered more than once. This promise is called , and it is what most queues give. Repeats are a normal part of it, not a rare accident.
Two payments, one repeat
A worker takes payment jobs from a queue. Each job has an id and an amount: pay-1 for £20 and pay-2 for £35. The customer owes £55.
- The worker takes pay-1 and charges £20.
- The worker crashes before it acknowledges pay-1. The queue hands pay-1 out again.
- The worker takes pay-1 a second time and charges £20 again.
- The worker takes pay-2 and charges £35.
That is 3 charges and £75. The customer paid £20 too much.
Remember what is done
The fix is for the worker to keep a record of the job ids it has finished. Call it the done record. Before doing a job, the worker looks the id up:
- pay-1 is not in the record. Charge £20 and add pay-1 to the record. Then the worker crashes, and pay-1 comes back.
- pay-1 is in the record. Skip it: no charge. Acknowledge it so the queue deletes it.
- pay-2 is not in the record. Charge £35 and add pay-2.
That is 2 charges, £55, and 1 skipped. Running the job a second time changed nothing. A job like this is : doing it twice leaves things the same as doing it once. The id that makes it work, one id per job that stays the same on every delivery, is called an .
Watch it happen
The card starts on no check. Run it and watch the second charge for pay-1. Then switch to done set, the worker with a done record, and run it again on the same three deliveries.
Press Run it, then step through and watch the requests move.
The handler in code
Here is the worker's handler. done is the record, a dictionarymap from job id to the amount charged. The program then delivers pay-1 twice and pay-2 once.
done = {}
def handle(job_id, amount): if job_id in done: print("skip", job_id) return 0 print("charge", job_id, amount) done[job_id] = amount return amount
total = 0total += handle("pay-1", 20)total += handle("pay-1", 20)total += handle("pay-2", 35)print("total", total)charge pay-1 20 skip pay-1 charge pay-2 35 total 55
import java.util.HashMap;import java.util.Map;
public class Main { static Map<String, Integer> done = new HashMap<>();
static int handle(String jobId, int amount) { if (done.containsKey(jobId)) { System.out.println("skip " + jobId); return 0; } System.out.println("charge " + jobId + " " + amount); done.put(jobId, amount); return amount; }
public static void main(String[] args) { int total = 0; total += handle("pay-1", 20); total += handle("pay-1", 20); total += handle("pay-2", 35); System.out.println("total " + total); }}charge pay-1 20 skip pay-1 charge pay-2 35 total 55
- if job_id in done:if (done.containsKey(jobId)) asks whether this id was finished before. If it was, the handler prints skip and returns 0: nothing is charged.
- Otherwise it charges the amount (the print stands in for a real payment), records the id in
done, and returns the amount. - The calls at the bottom add up what was charged. pay-1 counts once, and the total is 55.
Step through it and watch the second call for pay-1 take the skip branch.
done = {}
def handle(job_id, amount): if job_id in done: print("skip", job_id) return 0 print("charge", job_id, amount) done[job_id] = amount return amount
total = 0total += handle("pay-1", 20)total += handle("pay-1", 20)total += handle("pay-2", 35)print("total", total)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Python runs in the browser when you press Run below. Java does not run here: step through the trace above, or paste the code into a Java tool on your computer.
Delete the check and its two lines under it (in Java, its closing brace too), and run it again: the second pay-1 is charged, and the total becomes 75.
Check yourself
The worker has no check. It charges pay-1 (£20), crashes before acknowledging, gets pay-1 again and charges it, then charges pay-2 (£35). How much does the customer pay in all?
pay-7 is delivered again after pay-8. What does this print?
done = {}
def handle(job_id, amount): if job_id in done: print("skip", job_id) return 0 print("charge", job_id, amount) done[job_id] = amount return amount
total = 0total += handle("pay-7", 10)total += handle("pay-8", 5)total += handle("pay-7", 10)print("total", total)import java.util.HashMap;import java.util.Map;
public class Main { static Map<String, Integer> done = new HashMap<>();
static int handle(String jobId, int amount) { if (done.containsKey(jobId)) { System.out.println("skip " + jobId); return 0; } System.out.println("charge " + jobId + " " + amount); done.put(jobId, amount); return amount; }
public static void main(String[] args) { int total = 0; total += handle("pay-7", 10); total += handle("pay-8", 5); total += handle("pay-7", 10); System.out.println("total " + total); }}Complete the check at the top of the handler, so that a job it has already done is skipped and the program prints total 55.
done = {}
def handle(job_id, amount): ________________ # ← the check print("skip", job_id) return 0 print("charge", job_id, amount) done[job_id] = amount return amount
total = 0total += handle("pay-1", 20)total += handle("pay-1", 20)total += handle("pay-2", 35)print("total", total)import java.util.HashMap;import java.util.Map;
public class Main { static Map<String, Integer> done = new HashMap<>();
static int handle(String jobId, int amount) { ________________ // ← the check System.out.println("skip " + jobId); return 0; } System.out.println("charge " + jobId + " " + amount); done.put(jobId, amount); return amount; }
public static void main(String[] args) { int total = 0; total += handle("pay-1", 20); total += handle("pay-1", 20); total += handle("pay-2", 35); System.out.println("total " + total); }}Where this goes next
- Say why a queue can deliver a job twice: it promises at-least-once delivery, and a crash before the acknowledgement or a retry repeats the job.
- Say what idempotent means: doing a job twice leaves things the same as doing it once.
- Write a handler that looks up the job's id in a record of finished jobs and skips it if it is there: 2 charges and 55 instead of 3 charges and 75.
- Say why the record and the effect must be saved together, or the payment provider given the idempotency key.