download

Your app keeps flipping between the main and backup model. Why?

System design~12 minReviewed 1 Nov 2026Free · No sign-in

Question

A support bot calls a main model. A health rule moves all traffic to a backup model when the main model's error rate over the last minute is 5% or more. It moves traffic back when the rate is below 5%. A small share of requests always goes to the main model, so its rate is known every minute.

Over ten minutes the rates were 2, 6, 4, 9, 5, 4, 6, 3, 2 and 1%. Users say the answers change style in the middle of a chat.

  1. Which model serves each minute? How many switches?
  2. Why does it flip, and why does that hurt?
  3. Change the rule so it switches only on a clear signal.
Show hints (3)
  1. "REPLAY IT"

    Start on the main model and go minute by minute. Each time the rate crosses 5%, the model changes. Count the changes.

  2. "NOISE NEAR THE LINE"

    For most minutes the rate sits between 3% and 6%, so small noise flips the model. What if leaving the backup needed a lower rate than entering it?

  3. "TWO LINES, MEMORY"

    Go to the backup at 8% or more, come back only at 2% or less. Between the two lines, keep whatever model you are on now.

Show checked explanation

One threshold flips the model whenever a noisy rate wobbles around it: 6 switches in 10 minutes. Use two thresholds with memory (hysteresis): go to the backup at 8% or more, come back only at 2% or less, and stay put in between. That gives 2 switches. A circuit breaker adds trial requests before traffic returns.

"SOLUTION · PYTHON"
rates = [2, 6, 4, 9, 5, 4, 6, 3, 2, 1]  # main model errors, % per minute


def replay(enter, leave):
    """Backup at `enter`% or more, main again at `leave`% or less."""
    on_backup, path = False, []
    for rate in rates:
        if rate >= enter:
            on_backup = True
        elif rate <= leave:
            on_backup = False
        path.append("B" if on_backup else "M")  # in between: no change
    switches = sum(a != b for a, b in zip(["M"] + path, path))
    return " ".join(path), switches


rules = {"5% both ways": (5, 4), "8% in, 2% out": (8, 2)}  # rates are whole %
for name, (enter, leave) in rules.items():
    path, switches = replay(enter, leave)
    print(f"{name}: {path} ({switches} switches)")
"OUTPUT"
5% both ways: M B M B B M B M M M (6 switches)
8% in, 2% out: M M M B B B B B M M (2 switches)

Code run on CPython 3.12 against the rates above. Checked by an editor other than the author.

Walkthrough

Replay both rules minute by minute (M is the main model, B the backup):

minuteerror rate5% both ways8% in, 2% out
12%MM
26%BM
34%MM
49%BB
55%BB
64%MB
76%BB
83%MB
92%MM
101%MM

With one line at 5%, a move from 4% to 6% is enough to switch, and that is just noise. The model changes 6 times. This back-and-forth is called flapping.

With two lines, the app switches only on strong evidence: 8% to leave the main model, 2% to return. Between them it stays where it is. Minute 2 (6%) stays on main, and minutes 5 to 8 stay on the backup. There are 2 switches. If the rate really swings from 9% to 1%, it still switches, and it should.

Many systems wrap this in a circuit breaker (see the Circuit Breaker pattern in the Azure Architecture Center):

  • Closed: the main model serves and failures are counted.
  • Open: after too many failures in a time window, the backup serves and a timer starts.
  • Half-open: when the timer ends, a few trial requests go to the main model. After enough successes in a row, the breaker closes. One failure opens it again and restarts the timer.

Common mistakes

  • Reading the main model's error rate while it gets no traffic. With zero requests the rate looks like 0%, so the app switches back at once and fails again.
  • Sending all traffic back as soon as the main model looks healthy. A model that just recovered can fail again under full load. Start with a few trial requests.
  • Computing the rate from very few requests. With 20 requests a minute, one error moves the rate by 5 points: from 0% straight to the 5% line.

Follow-up questions

  • "Why is a flip worse for an LLM app than for a database replica?" Two replicas return the same rows. Two models differ in tone, length and format, and a prompt tuned for the main model may break the JSON output on the backup. Test your prompts on both, and keep one conversation on one model where you can.
  • "The backup is also failing. What now?" Don't push traffic back to the failing main model. Fail fast with a clear message, serve a cached or simpler answer, or queue the request, and alert a person.

What a strong answer covers

Shows one threshold flapping on noise (6 switches) and fixes it with two thresholds and memory (2 switches). Knows the circuit breaker states and returns traffic through trial requests.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from System Design with AI Interview Guide (Rohit Bhardwaj, 2026); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.