You Probably Don't Need a Load Balancer
Cloudflare will sell you a load balancer for five dollars a month. I built the same failover for nothing, and the interesting part is why the load balancer would not have helped.
What I wanted was simple to say: if Cloudflare Pages goes down, serve my site from the Kubernetes cluster in my flat.
The blocker nobody mentions
A load balancer routes between origins that both serve your site. I had one.
My site runs on Pages Functions. Twenty-three of its thirty-two endpoints read from Workers KV, and two more read from R2. Those bindings exist only inside the Workers runtime. My k3s cluster cannot run this site — not misconfigured, not missing a package, simply cannot, because half the application talks to storage that only exists inside Cloudflare.
Point a load balancer at a box that 404s every API call and it fails its health check forever. You have bought a monthly bill and an extra hop.
So the second origin could not be a copy. It had to be a snapshot: every page, every post, every public API payload frozen to disk and served by stock Caddy. Read-only. The contact form returns 503 while it is live.
That sounds like a compromise, and it is exactly the right one. During an outage nobody needs to submit my contact form. They need to read the thing they came for.
The load balancer is a loop you can write
Once a real second origin existed, the failover itself turned out to be about eighty lines.
Poll the primary. If it fails several checks in a row, PATCH a DNS record. Keep polling. When it comes back, PATCH it back. That is what the managed load balancer does for you, and Cloudflare's DNS API is free on every plan.
The detail that makes this better than the alternative I nearly chose: the record stays proxied throughout. I only change what sits behind the orange cloud, never the cloud itself. So WAF, DDoS protection and edge caching keep working during the failover.
The other option — moving my nameservers to a provider with health-checked DNS — would have given all of that up, and would have put my home IP address into public DNS the moment it triggered.
Three decisions that took longer than the code
Probe the origin's own hostname, not your domain. The obvious check is to poll your own site. It is also wrong: the moment you fail over, your domain resolves to the mirror, the check goes green, and the watcher immediately fails back into the outage it just escaped. The primary has to be measured on a hostname that does not move.
Make the thresholds asymmetric. Three failures to move, five successes to move back. Failing over late costs a few extra minutes of downtime. Failing back early bounces visitors between a recovering origin and a stale mirror, which reads as broken rather than slow.
Refuse to fail over to a mirror that is also down. If both origins are unhealthy, stay put. Moving traffic to a dead origin turns "the primary is broken" into "the domain points at nothing", and then something has to notice and undo it.
What none of it buys
A Cloudflare-wide outage.
My watcher's own API call runs on the infrastructure it would need to route around. So does the paid load balancer. If Cloudflare's edge goes down, my nameservers are Cloudflare's and my records are Cloudflare proxy IPs, so the domain stops resolving before anything gets a chance to be clever.
That gap is real, it is not closeable for free, and closing it costs the protections above. For a personal site I decided it was not worth it. What matters is that my status page says so in those words, rather than implying more coverage than exists.
Scope stated honestly is worth more than scope overstated. That applies to failovers, and to CVs.