I wanted my Go app to try a backup HTTP endpoint when the primary couldn't handle a request. I didn't want to run a separate proxy just to handle failover.
A 503 is easy to handle. But HTTP 200 doesn't always mean the app got the data it needed. The primary might return an application-level error because its data is too stale, while the backup might have newer data.
The primary might also not respond at all. It can hang until the request deadline expires, leaving no time to try a backup.
So I built failnext, a Go library for client-side HTTP failover with no external dependencies. It implements http.RoundTripper, so it works with Go's standard http.Client. It's designed for a small, priority-ordered set of providers, typically a primary and several backups.
I originally built it for go-ethereum over HTTP JSON-RPC, but the library itself is protocol-neutral.
Here's what failnext can do:
- Primary unavailable? Try the next provider.
- Primary hangs? Set a per-attempt timeout to leave time for a backup.
- HTTP 200 with an application-level error? Your app can retry with that provider excluded.
- Provider is stale or degraded? If your app detects it,
failnext can skip it. - Want reads to fail over, but not writes? Your app can allow failover for reads and block it for writes.
- Reading after a write? Try the provider that handled the write first.
- Everything failed? See which providers were tried and what went wrong.
I wanted failnext to handle provider selection, cooldowns, and HTTP-level failover. It doesn't inspect response bodies or detect lagging providers.
The app can use context helpers to control whether a request can fail over, which providers to exclude, and which one to try first. The OnEvent callback reports which providers were tried and which one returned the final response.
I'd be interested in feedback on the RoundTripper approach and any failover cases I might have missed. If you try failnext, let me know what doesn't work or feels awkward.
GitHub: https://github.com/yermakovsa/failnext