learning-it by example

#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.

1. Why can a plain retry put duplicate orders into another system?
2. What is an idempotency key?
3. In a Python demo that pushed the same 20 orders three ways to a flaky stand-in ERP, what did each way end up holding?
4. Which of these is a mistake to avoid with idempotency keys?
5. In a Python stand-in ERP class for a retry demo, what does the key set do inside the create method?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. 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.

  2. 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.

  3. 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.

  4. 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

All Forward deployed engineer interviews videos

Related videos