Webhook icon
Schedule icon
If icon
Fail icon
Script icon
Process icon
OutputValues icon
Set icon
SlackIncomingWebhook icon

Fail Over Between EVM RPC Providers Automatically

Detect lagging, forked or rate-limited EVM RPC providers and switch the provider your flows use, with hysteresis.

Categories
BusinessInfrastructure

Wallets, indexers, payment services and bots all depend on an RPC provider, and providers fail in ways a plain uptime check misses. They fall behind the chain and serve stale balances. They start answering HTTP 429 under load. A misconfigured endpoint can point at another network. And now and then one ends up on a minority fork. This blueprint compares all your providers against each other every minute, picks the one to use, and publishes it in the Kestra KV store, so every other flow can switch with no code change.

How it works

  1. every_minute (Schedule, shipped disabled: enable it once the provider secrets are set) runs the check; rpc_errors_seen (Webhook) lets your application request an immediate re-check when it sees errors. concurrency cancels a run if the previous one is still going.
  2. probe (Python, standard library only) calls every provider in parallel for its chain id and head block, with a timeout, and classifies failures:
    • unreachable, timeout, HTTP 429, HTTP 401/403;
    • invalid answers and JSON-RPC errors;
    • wrong chain: a chain id other than chain_id;
    • lagging: more than max_lag_blocks behind the highest head.
  3. The providers that pass are asked for the hash of the same recent block. One that disagrees with the majority is forked.
  4. Hysteresis decides the active provider, from streaks kept in the KV store:
    • switch away only after fail_after consecutive bad checks;
    • switch back to a more preferred provider only after recover_after consecutive good checks.
  5. save_state writes the streaks; publish_active writes <kv_prefix>_active when the choice changes.
  6. report posts failovers, failbacks and any provider whose status changed. all_down fails the run when no provider is healthy, which pages through the errors handler; the KV store keeps pointing at the last active provider.

Use the active provider in other flows

uri: "{{ secret(kv('evm_rpc_mainnet_active').secret) }}"

The KV store holds the secret's name, never the URL, so API keys stay in your secret backend.

Error handling

  • No flapping. One slow answer or one 429 does not move traffic, and failback waits for a sustained recovery.
  • Errors that are not outages are named, so the alert says what to fix: "rate limited (HTTP 429)", "unauthorized (HTTP 401): check the API key", "serves chain 5, expected 1", "18 blocks behind", "block 1234 is 0xab12… on this provider, 0x9f3c… on the others".
  • A malformed answer marks only that provider as failing. A missing or null result, a batch entry that is not an object, or a non-hex quantity is reported ("malformed answer: no result for eth_blockNumber") and the other providers are still compared.
  • Fork detection needs a majority of in-sync providers, so with two providers that disagree nobody is declared forked by mistake.
  • Nothing healthy, nothing changed. When every provider fails, the active provider stays as it was and the run fails with every provider's reason.
  • Bad configuration fails before probing: a malformed provider list, fewer than two providers, or a missing secret.

What this blueprint teaches about Kestra

  • The KV store as a small service registry: one flow writes the decision, every other flow reads it with kv() and resolves the URL with secret().
  • State machines across executions (streaks and hysteresis) kept in the KV store.
  • Dynamic secrets: secret() with a name that comes from an input.
  • concurrency with behavior: CANCEL for a check that runs every minute.

Prerequisites

  • Two or more JSON-RPC endpoints for the same chain, each stored as a secret.
  • A Slack incoming webhook.
  • Python 3 on the Kestra worker (included in the Kestra image) for the Process task runner.

Secrets

  • One secret per provider, named in providers (for example EVM_RPC_PRIMARY).
  • SLACK_WEBHOOK_URL: Slack incoming webhook.
  • RPC_FAILOVER_WEBHOOK_KEY: a long random string, the key in the webhook URL your application calls.

Inputs

  • providers (ARRAY): Label=SECRET_NAME, most preferred first.
  • chain_id (INT, default 1), max_lag_blocks (INT, default 5).
  • fail_after (INT, default 2), recover_after (INT, default 5): hysteresis.
  • timeout_seconds (FLOAT, default 5).
  • kv_prefix (STRING, default evm_rpc_mainnet): use one per chain.

Outputs

  • active (STRING): the provider in use after the check.
  • providers (JSON): each provider's status, head, lag, latency and streaks.

Quick start

  1. Add one secret per provider and list them in providers, most preferred first.
  2. Run the flow once: it publishes evm_rpc_mainnet_active.
  3. Replace hard-coded RPC URLs in your other flows with secret(kv('evm_rpc_mainnet_active').secret).
  4. Copy the flow per chain, with its own chain_id and kv_prefix.

Plugins used

See How

New to Kestra?

Use blueprints to kickstart your first workflows.