#305 Forward deployed engineer interviews 5.7 min
Retry a Flaky API Without Duplicates: Idempotency Keys
A plain retry loop put five duplicate orders into a stand-in system. One key fixed it.
Run the three ways
Copy the code folder first. Run python run.py. Read the three result lines and note how many records the ERP holds, how many duplicates and how many missing for each way. You should see 18, 25 and 20 records, with the plain retry showing 5 duplicates.
A hint
The calls figure is the same for the two retry ways.
Make the ERP go silent more often
In erp.py change silent_every=5 to silent_every=3. Predict whether the plain retry will hold more or fewer duplicates, and whether the retry with a key changes. Then run python run.py. With this change the plain retry holds 30 with 10 duplicates, and the retry with a key still holds exactly 20. Change silent_every back to 5 afterwards, because the next exercise quotes results for the original ERP.
A hint
Silent calls save the order and then raise a timeout.
Give up sooner
In sync.py change both tries=4 to tries=2. Predict how many duplicates the plain retry will make, then run python run.py. With this change the plain retry holds 23 with 3 duplicates and the retry with a key still holds exactly 20, nothing missing. Why do fewer tries mean fewer duplicates?
A hint
A retry only happens after a failed call.
Break the key on purpose
In sync.py, in the push function, change key=order["id"] if use_key else None to key="X" if use_key else None. This uses one key for every order. Predict what the retry with key line will show, then run python run.py. With this change it holds only 1 record and 19 are missing. Relate this to the mistake of reusing one key for a different order.
A hint
The ERP skips any order whose key it has already seen.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
The ERP is a stand-in class with a fixed failure pattern, not a real system. No network, no queue, and nothing survives a restart. The key only helps if the other system honours it.
Up next in Forward deployed engineer interviews
More in Forward deployed engineer interviews
Related videos
- #308 Two Systems Edit One Record: Lost Updates Explained Forward deployed engineer interviews
- #302 Forward Deployed Engineer Interview: What Is Tested Forward deployed engineer interviews
- #303 The FDE Decomposition Round: Vague Ask to a Plan Forward deployed engineer interviews
- #306 Webhooks Done Right: Signatures, Retries and Replays Forward deployed engineer interviews
