KatInterview

Designing Automated Failover for Distributed Databases

System DesignModel answer
MediumModel answerSystem Designdatabasesdistributed-systemsfault-tolerance

Last updated

Design a resilient database layer that automatically handles node failures while preventing data loss and ensuring only one primary accepts writes.

Context

You're building an automated failover system for a PostgreSQL cluster deployed across two availability zones. The cluster has one primary node that handles writes and multiple read replicas. Your system must keep the database available when the primary fails.

Your Task

Design a solution that automatically promotes a replica to primary when the original primary becomes unavailable. Your design should address:

  1. Failure Detection: How does the system distinguish between an actual node failure and a temporary network issue?

  2. Promotion Process: What steps occur when promoting a replica? How do you ensure all replicas remain consistent after promotion?

  3. Write Safety: How do you guarantee that the old primary stops accepting writes before the new one starts? What prevents two nodes from acting as primary simultaneously?

  4. Replication Trade-offs: Should replication be synchronous or asynchronous? How does this choice affect data loss (RPO) and system latency?

  5. Client Routing: How do applications discover which node is currently primary after a failover completes?

Constraints

  • The deployment spans exactly two availability zones
  • Target recovery time under 30 seconds
  • Workload: approximately 5,000 writes/second and 50,000 reads/second
  • Network latency between zones is 1-2 milliseconds

Explain your reasoning for key decisions and mention potential failure modes in your design.

Hints

Hint 1

The core challenge is distributed consensus: who decides which node is primary, not just detecting failures

Hint 2

Two availability zones alone cannot achieve majority quorum - consider what this means for your consensus layer

Hint 3

Preventing split-brain requires multiple defense mechanisms, not just one health check