Workflow

Search API Failover with Circuit Breaker

Automate search API failover with a circuit breaker pattern. Detect provider failures and route to backup search endpoints automatically.

Overview

Monitors primary search API health and automatically routes queries to a backup provider when the primary fails. Implements a circuit breaker that trips after consecutive errors and resets after a cooldown period with successful health checks.

Trigger

Every search API call (wrapper function)

Schedule

On every search call (middleware pattern)

Workflow Steps

1

Check circuit breaker state

Before each search call, check if the circuit breaker is open (tripped). If open and cooldown has not elapsed, route directly to the fallback provider.

2

Execute primary search

Send the query to the primary search API (Scavio). Track response time and HTTP status code.

3

Record outcome

On success with acceptable latency, clear the failure counter. On error or timeout, increment the failure counter.

4

Trip circuit breaker if threshold reached

After 3 consecutive failures, trip the circuit breaker and log an alert. Set the cooldown timer.

5

Route to fallback

When the circuit is open, send queries to the backup search provider. Normalize the response to match the primary format.

6

Health check and reset

After cooldown elapses, send a canary query to the primary. If it succeeds, close the circuit and resume primary routing.

Python Implementation

Python
import requests, os, time

H = {"x-api-key": os.environ["SCAVIO_API_KEY"]}
failures = []
circuit_open = False
circuit_opened_at = 0
COOLDOWN = 60

def search(query, platform="google"):
    global circuit_open, circuit_opened_at
    if circuit_open and time.time() - circuit_opened_at < COOLDOWN:
        return fallback_search(query, platform)
    if circuit_open:
        circuit_open = False
        failures.clear()
    try:
        r = requests.post("https://api.scavio.dev/api/v1/search", headers=H,
            json={"platform": platform, "query": query}, timeout=10)
        r.raise_for_status()
        failures.clear()
        return r.json()
    except Exception:
        failures.append(time.time())
        if len(failures) >= 3:
            circuit_open = True
            circuit_opened_at = time.time()
            return fallback_search(query, platform)
        raise

def fallback_search(query, platform):
    return {"source": "fallback", "query": query, "results": []}

JavaScript Implementation

JavaScript
const H = {"x-api-key": process.env.SCAVIO_API_KEY, "Content-Type": "application/json"};
let failures = 0;
let circuitOpen = false;
let circuitAt = 0;

async function search(query, platform = "google") {
  if (circuitOpen && Date.now() - circuitAt < 60000) {
    return {source: "fallback", query, results: []};
  }
  if (circuitOpen) { circuitOpen = false; failures = 0; }
  try {
    const r = await fetch("https://api.scavio.dev/api/v1/search", {
      method: "POST", headers: H,
      body: JSON.stringify({platform, query})
    });
    if (!r.ok) throw new Error("HTTP " + r.status);
    failures = 0;
    return r.json();
  } catch (e) {
    if (++failures >= 3) { circuitOpen = true; circuitAt = Date.now(); }
    throw e;
  }
}

Platforms Used

Google

Web search with knowledge graph, PAA, and AI overviews

Amazon

Product search with prices, ratings, and reviews

YouTube

Video search with transcripts and metadata

Reddit

Community, posts & threaded comments from any subreddit

Frequently Asked Questions

Monitors primary search API health and automatically routes queries to a backup provider when the primary fails. Implements a circuit breaker that trips after consecutive errors and resets after a cooldown period with successful health checks.

This workflow uses a every search api call (wrapper function). On every search call (middleware pattern).

This workflow uses the following Scavio platforms: google, amazon, youtube, reddit. Each platform is called via the same unified API endpoint.

Yes. Scavio's free tier includes 500 credits per month with no credit card required. That is enough to test and validate this workflow before scaling it.

Search API Failover with Circuit Breaker

Automate search API failover with a circuit breaker pattern. Detect provider failures and route to backup search endpoints automatically.