Skip to main content
Back to Community & Earn Hub

Building Multi-Region LLM Failover Gateways: Hedged Requests, Regional Quota Routing, and Sub-Second Circuit Breakers

@devon_brooks3 days ago·2 min read 2156 views
Building Multi-Region LLM Failover Gateways: Hedged Requests, Regional Quota Routing, and Sub-Second Circuit Breakers

Why Naive LLM Retries Break Under Regional Load

If your AI product serves users across US-West, US-East, and APAC, relying on a single inference region or a naive tenacity exponential backoff loop is a recipe for p99 latency spikes > 25 seconds.

When a regional cluster hits 429 Too Many Requests or degrades on TTFT (Time-to-First-Token), retrying the same region 3 times only amplifies the thundering herd.


Three Patterns for Resilient Multi-Region Inference

  1. TTFT-Based Hedged Requests (Tail-At-Scale): If the primary region hasn't emitted its first SSE token within 650ms (for interactive chat), fire a secondary request to a warm backup region and cancel whichever loses the race.
  2. Sliding-Window Provider Circuit Breakers: Trip a region into HALF-OPEN state for 30 seconds the moment its 10-second error rate (429/503/timeout) crosses 8%.
  3. Header-Driven Quota Steering: Parse x-ratelimit-remaining-tokens from every response into a shared Redis/in-memory score so traffic drains smoothly before hitting hard 429 walls.

Async Python Hedged Router + Circuit Breaker

python
import asyncio
import time
from dataclasses import dataclass, field


@dataclass
class RegionEndpoint:
    name: str
    base_url: str
    open_until: float = 0.0
    failures: int = 0
    total: int = 0

    def is_healthy(self) -> bool:
        return time.monotonic() >= self.open_until

    def record(self, ok: bool) -> None:
        self.total += 1
        if not ok:
            self.failures += 1
        if self.total >= 20 and (self.failures / self.total) > 0.08:
            self.open_until = time.monotonic() + 30.0
            self.failures = 0
            self.total = 0


async def call_with_hedge(primary: RegionEndpoint, backup: RegionEndpoint, call_fn, hedge_ms: float = 0.65):
    if not primary.is_healthy():
        return await call_fn(backup)

    primary_task = asyncio.create_task(call_fn(primary))
    done, _ = await asyncio.wait({primary_task}, timeout=hedge_ms)

    if primary_task in done:
        try:
            res = primary_task.result()
            primary.record(True)
            return res
        except Exception:
            primary.record(False)
            return await call_fn(backup)

    # Primary missed TTFT hedge budget — race against backup
    backup_task = asyncio.create_task(call_fn(backup))
    finished, pending = await asyncio.wait(
        {primary_task, backup_task}, return_when=asyncio.FIRST_COMPLETED
    )
    for task in pending:
        task.cancel()

    winner = next(iter(finished))
    return winner.result()

Production Takeaway

Keep hedging disabled for high-cost batch jobs (where 2x token spend on overlap hurts margins) and enable it strictly for interactive user-facing tool calls and chat, where cutting p99 latency from 12s to 1.4s directly protects retention.

2 Comments

Discussion

@aarav_sharma3 days ago

Header-driven quota steering on x-ratelimit-remaining-tokens is huge. Waiting for an actual 429 before shifting weight to a backup region always drops a batch of in-flight user streams.

@maya_lin3 days ago

Great distinction on disabling hedged requests for batch pipelines. For interactive voice/chat agents though, a 650ms TTFT hedge practically eliminates regional tail stalls.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.