Self-hosting a public site on a Raspberry Pi means accepting that the Pi will, occasionally, not be there. Power, a botched deploy, a tunnel hiccup. The answer is a warm standby on managed hosting and something to flip between them. The something is a Cloudflare Worker, and the part worth writing down is where it keeps its state: nowhere I had to provision.
The Setup#
The primary is the Pi behind a Cloudflare Tunnel. The standby is the same site deployed to a managed platform. A Worker runs a health check on a schedule and points DNS at whichever target is healthy. Two origins, one switch, automatic.
Asymmetric by Design#
The interesting decision is that the two directions are not symmetric, on purpose. Failing away from the Pi is deliberately slow: it takes three consecutive failed health checks — on the order of fifteen minutes of sustained downtime — before the Worker gives up and points DNS at the standby. That delay exists so a transient blip doesn't flap the site back and forth. Failing back to the Pi is immediate: the moment the tunnel reports healthy again, DNS swings home. No counter, no threshold, no waiting. Slow to abandon, quick to return.
State With No Extra Bindings#
A failure counter implies storage. The obvious move is a KV namespace, which means another binding, another thing to set up, another thing to forget exists. Instead the counter lives in the comment field of the DNS record the Worker already edits. The Worker reads the record, parses a small fail:N marker out of the comment, increments or resets it, and writes the record back. The state rides along with the exact object the Worker already has a write path to. One successful health check resets the marker to zero. A manual reset endpoint forces the Pi back as primary on demand.
Why Not Just Run on the Managed Platform Full Time#
Fair question, and the same one I answer about the whole self-hosting setup: the Pi-behind-a-tunnel deployment is the point. It's the demonstration. The managed copy is insurance, not the home. Failover lets me keep the self-hosted origin as the face of the site while guaranteeing it's never the single point that takes the whole thing down.
Lessons That Generalize#
Three things from this are worth stealing even if you never touch Cloudflare. Debounce the direction that hurts — flapping is worse than a short outage, so make abandonment require persistence. Make recovery instant, because there's no downside to coming home the moment home is healthy. And keep state where you already have a write path: if a process already reads and writes an object every cycle, that object can carry a few bytes of state and save you a whole storage dependency you'd otherwise have to manage and secure.