Start learning free

LESSON 179 OF 188 · BACKEND: INTERVIEW QUESTIONS

Retry with Backoff

Practice

The interview question: “Your API calls a supplier that is sometimes busy for a moment. How do you make your endpoint survive that, without making the supplier's trouble worse?”

The expected answer: retry with exponential backoff. Retry only the answers that may change: a 5xx is the supplier's own trouble and may pass, while a 4xx such as 404 will be the same next time. Wait before each retry and double the wait every time, 1, 2, then 4 seconds, so a struggling server gets more and more room. Stop after a fixed number of tries.

Doubling, on another value: a loop that doubles a number until it reaches a limit.

size = 1
while size < 10:
    print(size)
    size = size * 2
# prints 1, 2, 4, 8

Nothing really sleeps in this lesson: wait(seconds) sleeps for real, but the check swaps it for a clock that only writes the seconds down. Your endpoint also returns the waits it made. The test supplier is busy twice for product 1, busy all the time for product 3, and doesn't sell product 6:

GET /products/1/supplier-stock  →  200  {"product_id": 1, "stock": 120, "waits": [1, 2]}
GET /products/3/supplier-stock  →  502  {"error": "The supplier is not answering", "waits": [1, 2, 4]}
GET /products/6/supplier-stock  →  404  {"error": "The supplier doesn't sell this product", "waits": []}
GET /products/4/supplier-stock  →  200  {"product_id": 4, "stock": 500, "waits": []}
GET /check/supplier  →  200  {"calls": 9, "waited": [1, 2, 1, 2, 4]}

Your task: the route, the first call to the supplier and the three answers are written. Add the retry loop: start delay at 1, and while the answer is a 5xx and fewer than MAX_TRIES - 1 waits have happened, wait, write the wait down in waits, double delay and call the supplier again.


Assignment
Follow the Coding Tutorial to add a retry loop with exponential backoff to supplier_stock.


Hint
Four tries have three waits between them. The last line of the loop must call the supplier again, or the loop keeps checking the same old answer.


STEP 1 OF 5
    Ctrl + Enter to check