DEV Community

desgh white
desgh white

Posted on

Circuit Breakers for Third-Party Odds Providers

When your product depends on a third-party odds feed that goes down twice a month, you need a circuit breaker — not just a retry loop. Retries without a breaker turn a 30-second outage into a 5-minute request queue that cascades into your own API.

Three states

A circuit breaker sits between your service and the upstream:

CLOSED  →  failures exceed threshold  →  OPEN
OPEN    →  cool-down expires          →  HALF-OPEN
HALF-OPEN → probe succeeds            →  CLOSED
HALF-OPEN → probe fails               →  OPEN
Enter fullscreen mode Exit fullscreen mode
class Breaker {
  private failures = 0;
  private state: 'closed' | 'open' | 'half' = 'closed';
  private openedAt = 0;

  async call<T>(fn: () => Promise<T>): Promise<T> {
    if (this.state === 'open') {
      if (Date.now() - this.openedAt < 30_000) throw new Error('circuit open');
      this.state = 'half';
    }
    try {
      const result = await fn();
      this.failures = 0;
      this.state = 'closed';
      return result;
    } catch (e) {
      if (++this.failures >= 5) { this.state = 'open'; this.openedAt = Date.now(); }
      throw e;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Fallback: stale data with a badge

While the breaker is open, serve the last known odds with a "prices may be delayed" badge. A platform like Tenobet that aggregates multiple providers can fall back to a secondary feed entirely, but most single-provider setups just need an honest staleness indicator.

Monitoring

Log every state transition. A breaker that opens and closes rapidly ("flapping") means your threshold is too low or the upstream is degrading rather than failing outright. Add a minimum time-in-open to prevent flapping.

Takeaway

Retries are for transient errors. A circuit breaker is for when the upstream is genuinely down and you need to stop hammering it, preserve your own resources, and give the user a degraded but honest experience.

Top comments (0)