โ† Back to Case Studies ๐Ÿ›ก๏ธ High Availability & Distributed Orchestration

Single-Leader Failover Engine

A zero-cost, split-brain immune active-passive failover system. Built for mission-critical e-commerce order fulfillment engines, single-leader background queues, and self-hosted automation engines (n8n/Python) without enterprise Kubernetes overhead.

Uptime SLA

> 99.9% (High Availability)

Protection

Zero Split-Brain

Failover Switch

Configurable TTL (< 2m Auto / ~15s Manual)

Tooling Cost

$0 / Month

The Engineering Challenge

Why Standard Load Balancing Fails for Single-Leader Workloads

Most web apps use active-active load balancing where multiple servers share incoming traffic. However, critical background automation workloads โ€” such as order fulfillment engines, transactional webhook consumers, and queue orchestrators (like n8n) โ€” operate under a strict Single-Leader constraint.

If two instances run concurrently on separate servers, catastrophic failures occur:

  • Duplicate Execution & Double Spending: Two instances ingest the same order webhook and dispatch duplicate vouchers or charge customers twice.
  • Database Race Conditions: Concurrent token refresh operations overwrite database states, locking API sessions.
  • The Split-Brain Trap: When a network partition happens, both servers assume the other is dead and promote themselves as Leader simultaneously.

Enterprise solutions (Kubernetes multi-node clusters, managed Consul, or Cloudflare Load Balancers) require expensive infrastructure tiers ($50โ€“$250+/month) and high operational complexity. The objective was to engineer a lightweight, production-grade alternative that guarantees single-leader exclusivity with 100% free-tier cloud resources.

System Architecture

The failover architecture decouples ingress traffic routing from local service lifecycle management using three lightweight components:

                      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                      โ”‚      Inbound Webhook Event      โ”‚
                      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                       โ”‚ HTTPS POST
                                       โ–ผ
      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ”‚                 Cloudflare Worker (Edge Router)                 โ”‚
      โ”‚                      Key Cache: 5s at Edge                      โ”‚
      โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚                          โ”‚                          โ”‚
            โ–ผ Read Lease Key           โ–ผ Route to Active          โ–ผ Route if Promoted
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Upstash Redis Cloud โ”‚   โ”‚  Cloudflare Tunnel   โ”‚   โ”‚  Cloudflare Tunnel   โ”‚
โ”‚  (Distributed Lease) โ”‚   โ”‚   Node A (Primary)   โ”‚   โ”‚   Node B (Secondary) โ”‚
โ”‚  failover:leader     โ”‚   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚  TTL: 120s (Auto)    โ”‚               โ”‚                          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜               โ–ผ                          โ–ผ
            โ”‚              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    HTTPS REST Lease       โ”‚ SERVER NODE A (VPS)  โ”‚   โ”‚ SERVER NODE B (NAS)  โ”‚
    Renew / Poll 10-60s    โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚   โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚
            โ”‚              โ”‚ [Failover Watchdog]  โ”‚   โ”‚ [Failover Watchdog]  โ”‚
            โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚ Status: LEADER       โ”‚   โ”‚ Status: STANDBY      โ”‚
            โ”‚              โ”‚                      โ”‚   โ”‚                      โ”‚
            โ”‚              โ”‚ [Order Engine Stack] โ”‚   โ”‚ [Order Engine Stack] โ”‚
            โ”‚              โ”‚ State: RUNNING [ON]  โ”‚   โ”‚ State: STOPPED [OFF] โ”‚
            โ”‚              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚                                                     โ–ฒ
            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Core Reliability Mechanisms

1. Zero Split-Brain Protection via Self-Fencing

Watchdog daemons run on each node, communicating with Upstash Redis through atomic REST leases. If Node A loses internet connectivity or fails to renew its lease before TTL expiry, Node A executes immediate local self-fencing (invoking docker compose stop) before Node B is permitted to claim leadership. This mathematical guarantee prevents two leaders from ever co-existing.

2. Sticky Active (Anti-Flapping)

When Node A recovers after a power or network failure, it checks the distributed state before taking any action. Since Node B has successfully assumed leadership, Node A gracefully settles into Standby mode. This prevents destructive flip-flopping ("flapping") back and forth between servers.

3. Instant Edge Routing (0s DNS Propagation Delay)

Traditional DNS-based failovers take 5 to 30 minutes to propagate through internet ISPs. This engine uses a Cloudflare Worker as an edge reverse proxy. It inspects the active leader key from Redis cache and immediately proxies requests to the healthy server tunnel with zero DNS propagation lag while preserving exact request headers and signatures.

Production Visuals & Control Center

Failover Control Center Dashboard

Real-Time Control Center Dashboard: Live leader monitoring, TTL countdown timer, and 1-click manual switchover interface.

Cloudflare Worker Edge Router Configuration

Edge Routing Layer: Cloudflare Worker proxying inbound webhook traffic dynamically to healthy tunnel endpoints.

Distributed Atomic Lease in Upstash Redis

Distributed Atomic Lease State: Stored in serverless Redis cloud with millisecond precision and TTL auto-expiry.

Container Resource Metrics via Docker Stats

Live Container Telemetry (docker stats): Demonstrating negligible resource footprint (< 0.1% CPU, ultra-lean RAM) while actively monitoring distributed Redis leases and node health.

Scalability & Platform Adaptability

While originally deployed to safeguard a mission-critical e-commerce order fulfillment engine, this architecture was engineered as a reusable, modular failover pattern. It seamlessly orchestrates:

  • Self-Hosted n8n: Failover paired with an external PostgreSQL database (Supabase/Neon), guaranteeing workflow queue execution resilience.
  • Python Background Workers: Custom Celery, RQ, or cron-based ETL scripts running on Docker without multi-instance collision.
  • Hybrid Infrastructure: Pairs heterogeneous nodes effortlessly โ€” e.g., Cloud VPS (Primary) โ†” On-Premise Synology NAS / Private Server (Secondary).
  • Remote Control: Includes REST APIs and ready-to-use n8n Telegram Bot templates for 1-click manual switchover on mobile devices.

Reliability SLA & Resource Metrics

Metric Traditional Cloud Clustering RafiqFlow Failover Engine
Monthly Tooling Cost $50 โ€“ $250+ / month $0 / month (100% Free Tiers)
Memory Footprint 500MB โ€“ 2GB (K8s/Consul agent) < 100MB RAM Total
Split-Brain Immunity Requires 3+ Quorum Nodes Guaranteed via 2-Node Self-Fencing
Inbound Firewall Ports Must open internal cluster ports 0 Ports Opened (Outbound Tunnels)
Uptime SLA 99.9% (Standard Multi-Node) > 99.9%
Next Steps

Facing Single-Leader Workload Challenges?

Let's discuss how to deploy a zero-cost, split-brain immune failover architecture for your critical background queues, webhooks, or automation engines.

View HA & Failover Setups โž” Book Technical Audit Explore Catalog