Start learning free

LESSON 180 OF 188 · BACKEND: INTERVIEW QUESTIONS

A Circuit Breaker in Python

Practice

The interview question: “A supplier your API depends on goes down for a minute. Every request to your endpoint now waits for it and fails, and each one adds load to a server that is trying to recover. What do you do?”

The expected answer: a circuit breaker. Count the supplier's failures in a row; a good answer sets the count back to 0. After 3 failures the breaker opens: for the next 30 seconds your app doesn't call the supplier at all and answers 503 at once. When the time is up, one request goes through to test it: a success closes the breaker, a failure opens it again for 30 seconds.

“Closed until a time”, on another route: a coffee machine that needs a minute between cups.

machine = {"busy_until": 0}

@app.post("/coffee")
def make_coffee():
    if now() < machine["busy_until"]:
        return jsonify({"error": "Still brewing"}), 503
    machine["busy_until"] = now() + 60
    return jsonify({"coffee": "ready"})

Time comes from now(), given in the starting code; the tests set it with an X-Test-Clock header, so nothing really waits. The test supplier answers 500, 200, 500, 500, 500, 500, and 200 after that. Your app must answer:

GET /products/1/supplier-stock, at second:
1000  →  502  {"error": "The supplier is not answering"}
1001  →  200  {"product_id": 1, "stock": 120}
1002  →  502  {"error": "The supplier is not answering"}
1003  →  502  {"error": "The supplier is not answering"}
1004  →  502  {"error": "The supplier is not answering"}   3 in a row: open until 1034
1020  →  503  {"error": "The supplier is down, try again later"}
1034  →  502  {"error": "The supplier is not answering"}   the test call fails: open until 1064
1040  →  503  {"error": "The supplier is down, try again later"}
1064  →  200  {"product_id": 1, "stock": 120}
GET /check/supplier  →  200  {"calls": 7}

Your task: the route, the call, the 502 for a 5xx and the other answers are written, and breaker holds "failures" and "open_until". Count each failure, open the breaker when the count reaches FAILURE_LIMIT, answer 503 at the top of the function while it's open, and set the count back to 0 when the supplier answers.


Assignment
Follow the Coding Tutorial to add a circuit breaker to supplier_stock.


Hint
The breaker is open while now() is before breaker["open_until"], so at exactly second 1034 a test request goes through. Put the reset below the 5xx if: it runs only when the supplier really answered.


STEP 1 OF 6
    Ctrl + Enter to check

    Bq Circuit Breaker in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free