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.
Python code
38 linesimport 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
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
- Use a dataclass with health status as a separate method that pings a real endpoint.
- 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
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Cloud Storage Helper Class in Python easy
- Create a Data Helper Class for Beginners in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
Keep learning
Related tutorials and quizzes for this topic.