Fail over across RPC providers
Two endpoints are not redundancy if one is behind or forked. Rank providers by health, detect
divergence explicitly, and fall back on failure without hiding the fact that you did.
Procedure
Keep an ordered list of endpoints across independent operators (Alchemy, Infura, QuickNode,
Ankr, a public node, or your own Erigon/reth).
RPCS="https://eth-mainnet.g.alchemy.com/v2/$ALCHEMY_KEY \
https://mainnet.infura.io/v3/$INFURA_KEY \
https://rpc.ankr.com/eth \
https://ethereum-rpc.publicnode.com"
Health-check each before use: chain id, head height, and head age.
for u in $RPCS; do
printf '%s chainid=%s head=%s\n' "$u" \
"$(cast chain-id --rpc-url $u)" \
"$(cast block-number --rpc-url $u)"
done
Validate that every endpoint reports the same hash at the same height. Divergence means a
fork, a stale node, or a lying endpoint — drop the minority.
H=$(cast block-number --rpc-url "$(echo $RPCS | awk '{print $1}')")
for u in $RPCS; do echo "$u $(cast block $H --json --rpc-url $u | jq -r .hash)"; done
In application code, use viem's ranked fallback transport so a failing endpoint is demoted
and the fastest healthy one is preferred:
import { createPublicClient, fallback, http } from 'viem'
const client = createPublicClient({
transport: fallback(
[http(a), http(b), http(c)],
{ rank: { interval: 30_000, sampleCount: 5 }, retryCount: 2 },
),
})
In scripts, wrap the call in a loop that rotates the endpoint on error and logs which one
answered, so a silent failover is visible in the output.
Pitfalls
- A provider returning HTTP 200 with a JSON-RPC error, or worse a stale block, does not raise
an exception; only an explicit hash comparison catches a lagging node.
- Quotas are per-provider and not shared: a fallback list multiplies your effective rate-limit
headroom but also means rate-limit errors must be distinguished from outage errors.
- Load-balanced RPC DNS names (e.g. free public gateways) can route each request to a different
backend, so two calls in one logical operation may hit different heights — pin a block number
for multi-call reads.
- Different providers compute gas estimates and
eth_call return data slightly differently on
state-changing edge cases; never mix providers within one simulation.
- Silently switching to a second endpoint can mask a real outage; log the switch with a warning.
Verification
for u in $RPCS; do cast block $H --json --rpc-url $u | jq -r .hash; done
# all lines identical, and head ages under ~30s
Report each endpoint, its head height, and whether hashes agree; state which endpoint answered
the final call.
1---2name: failover-across-rpc-providers3description: Use when a workflow depends on a single RPC endpoint and you need redundancy that does not silently serve stale or forked data.4metadata:5 source: https://skillmd.com/skills/jiayaoqijia/failover-across-rpc-providers6---78# Fail over across RPC providers910Two endpoints are not redundancy if one is behind or forked. Rank providers by health, detect11divergence explicitly, and fall back on failure without hiding the fact that you did.1213## Procedure14151. Keep an ordered list of endpoints across independent operators (Alchemy, Infura, QuickNode,16 Ankr, a public node, or your own Erigon/`reth`).1718 RPCS="https://eth-mainnet.g.alchemy.com/v2/$ALCHEMY_KEY \19 https://mainnet.infura.io/v3/$INFURA_KEY \20 https://rpc.ankr.com/eth \21 https://ethereum-rpc.publicnode.com"22232. Health-check each before use: chain id, head height, and head age.2425 for u in $RPCS; do26 printf '%s chainid=%s head=%s\n' "$u" \27 "$(cast chain-id --rpc-url $u)" \28 "$(cast block-number --rpc-url $u)"29 done30313. Validate that every endpoint reports the *same hash* at the same height. Divergence means a32 fork, a stale node, or a lying endpoint — drop the minority.3334 H=$(cast block-number --rpc-url "$(echo $RPCS | awk '{print $1}')")35 for u in $RPCS; do echo "$u $(cast block $H --json --rpc-url $u | jq -r .hash)"; done36374. In application code, use viem's ranked fallback transport so a failing endpoint is demoted38 and the fastest healthy one is preferred:3940 import { createPublicClient, fallback, http } from 'viem'41 const client = createPublicClient({42 transport: fallback(43 [http(a), http(b), http(c)],44 { rank: { interval: 30_000, sampleCount: 5 }, retryCount: 2 },45 ),46 })47485. In scripts, wrap the call in a loop that rotates the endpoint on error and logs which one49 answered, so a silent failover is visible in the output.5051## Pitfalls5253- A provider returning HTTP 200 with a JSON-RPC error, or worse a stale block, does not raise54 an exception; only an explicit hash comparison catches a lagging node.55- Quotas are per-provider and not shared: a fallback list multiplies your effective rate-limit56 headroom but also means rate-limit errors must be distinguished from outage errors.57- Load-balanced RPC DNS names (e.g. free public gateways) can route each request to a different58 backend, so two calls in one logical operation may hit different heights — pin a block number59 for multi-call reads.60- Different providers compute gas estimates and `eth_call` return data slightly differently on61 state-changing edge cases; never mix providers within one simulation.62- Silently switching to a second endpoint can mask a real outage; log the switch with a warning.6364## Verification6566 for u in $RPCS; do cast block $H --json --rpc-url $u | jq -r .hash; done67 # all lines identical, and head ages under ~30s6869Report each endpoint, its head height, and whether hashes agree; state which endpoint answered70the final call.