Skip to main content
Back to Community & Earn Hub

Multi-Region Active-Active AI Gateways on AWS us-east-1 & us-west-2: Zero-Downtime Failover With Cloudflare Workers and LiteLLM

@marcus_chen3 days ago·1 min read 2003 views
Multi-Region Active-Active AI Gateways on AWS us-east-1 & us-west-2: Zero-Downtime Failover With Cloudflare Workers and LiteLLM

Surviving Regional Cloud Degradation During Peak US Hours

In production US SaaS environments, relying on a single inference region (us-east-1) means any regional Bedrock/OpenAI gateway brownout or NAT failure drops customer-facing agents nationwide.

Deploying Active-Active Gateways behind a Cloudflare Workers edge router gives sub-50ms automatic regional failover between us-east-1 (Virginia) and us-west-2 (Oregon):

text
[US East / West Clients]
         │
         ▼
[Cloudflare Worker Edge Router (<15ms)]
  ├── Primary Pool: `us-east-1` LiteLLM + Bedrock/vLLM (Health OK)
  └── Failover Pool: `us-west-2` LiteLLM + Bedrock/vLLM (Auto-switch on 5xx / >2.5s TTFB)

Edge Failover Worker (worker.ts)

typescript
interface Env {
  US_EAST_GATEWAY: string;
  US_WEST_GATEWAY: string;
}

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const reqClone = request.clone();
    const primaryUrl = `${env.US_EAST_GATEWAY}${new URL(request.url).pathname}`;
    const secondaryUrl = `${env.US_WEST_GATEWAY}${new URL(request.url).pathname}`;

    try {
      const controller = new AbortController();
      const timer = setTimeout(() => controller.abort(), 3000); // 3s TTFB guard

      const res = await fetch(primaryUrl, {
        method: request.method,
        headers: request.headers,
        body: request.body,
        signal: controller.signal,
      });
      clearTimeout(timer);

      if (res.status < 500 && res.status !== 429) {
        return res;
      }
    } catch {
      // Primary timed out or unreachable -> fail over immediately to us-west-2
    }

    return fetch(secondaryUrl, {
      method: reqClone.method,
      headers: reqClone.headers,
      body: reqClone.body,
    });
  },
};

Key Takeaway

Cloning the request before the 3-second AbortController race lets your edge gateway reroute 429 rate-limit spikes and 503 regional brownouts to us-west-2 seamlessly without client retries.

2 Comments

Discussion

@sarah_jenkins3 days ago

Cloning the request before the 3s AbortController race is super clean. During last month's us-east-1 latency spike, automatic failover to us-west-2 kept our p99 under 4 seconds.

@chloe_martinez3 days ago

Just remember to keep us-east-1 and us-west-2 prompt caches warmed or gracefully expect a single cold-cache prefill on the failover region!

Join the developer discussion

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