How to Implement Region Failover Config in Python with Primary and Secondary Mock

This Python class simulates regional failover: it tracks active region, switches to secondary on primary failure, and allows manual recovery.

Easy Python 3.9+ Aug 9, 2026 Cloud + Python 15 views 0 copies

Python code

38 lines
Python 3.9+
import time

class RegionFailoverConfig:
    def __init__(self, primary, secondary):
        self.primary = primary
        self.secondary = secondary
        self.active = primary
        self.failover_count = 0
        self.healthy = True

    def check_health(self):
        """Mock health check - returns True if active region is alive."""
        return self.healthy

    def failover(self):
        """Switch active region to the secondary if primary fails."""
        if not self.check_health():
            self.active = self.secondary
            self.failover_count += 1
            return f"Failover triggered -> Active region: {self.active}"
        return f"No failover needed - Active region: {self.active}"

    def recover(self):
        """Recover primary and switch back if it's healthy."""
        self.healthy = True
        self.active = self.primary
        return f"Recovered -> Active region: {self.active}"


if __name__ == "__main__":
    config = RegionFailoverConfig(primary="us-east-1", secondary="eu-west-1")

    print(config.failover())   # No failover (healthy)
    config.healthy = False     # Simulate primary outage
    print(config.failover())   # Failover to secondary
    print(config.failover())   # No failover (secondary healthy)
    print(config.recover())    # Recover primary
    print(f"Total failovers: {config.failover_count}")

Output

stdout
No failover needed - Active region: us-east-1
Failover triggered -> Active region: eu-west-1
No failover needed - Active region: eu-west-1
Recovered -> Active region: us-east-1
Total failovers: 1

How it works

The RegionFailoverConfig class holds primary, secondary, and active region strings, plus a failover count and healthy flag. The failover() method checks health; if unhealthy, it switches active to secondary and increments the counter. Recovery sets healthy back to true and restores primary as active. This provides a minimal mock for testing failover logic without real cloud dependencies.

Common mistakes

  • Not resetting the healthy flag after a failover, causing repeated failovers.
  • Assuming failover always switches even when primary is healthy.
  • Using shared mutable state for active region in multi-threaded contexts without locks.

Variations

  1. Use a dataclass with health status as a separate method that pings a real endpoint.
  2. Add automatic recovery after a cooldown period instead of manual recover().

Real-world use cases

  • Testing cloud infrastructure failover logic in CI without incurring cloud costs.
  • Implementing a lightweight routing layer that switches between primary and standby API endpoints.
  • Simulating regional outages in a local development environment for resilience testing.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Cloud + Python

Related tutorials and quizzes for this topic.