Database High Availability
A database that survives losing its host
Database HA means automatic failover with bounded downtime: synchronous standby, health-based promotion, application reconnect, and drained queueing. The trick is testing the failover, not assuming it.
What you will learn
- Explain synchronous vs asynchronous replication for HA
- Design multi-AZ/zone failover
- Make applications survive a DB failover
A standby in another AZ replicates from the primary either synchronously (zero committed-write loss, higher latency) or asynchronously (fast, small lag window). On primary failure, readiness switches, DNS/endpoint repoints, and the app reconnects — if it's designed to.
What is this?
AWS RDS Multi-AZ/ Aurora cluster failover (primary + replica promotion), Azure SQL geo/auto-failover groups, Cloud SQL high-availability configs (primary + failover replica in another zone). Semantics: synchronous standby commits with the primary (zero loss on failover); async replicas risk losing the lag window. RTO targets: seconds-to-minutes depending on detection, DNS TTL, and app reconnect behavior.
Why do you need it?
- A DB outage is a hard service outage; HA shrinks it to seconds.
- Multi-AZ/zone placement protects against rack/power/linkfail.
- Automated failover beats morning-pager-whack-a-mole.
- Well-tested failover = confident releases and calmer incidents.
Mental model
Two pilots watching the same instruments, ready to take the yoke
Database primary + standby
One pilot flies (primary), the co-pilot mirrors every action (replication). If the pilot stops responding, the co-pilot takes over without the passengers even noticing the swap.
How it works
- Synchronous standby (RDS Multi-AZ, Cloud SQL HA): transaction commits only after the standby acknowledges — no write loss, slight latency +.
- Async replicas (read replicas promoted to primary): faster writes, accept up to seconds of lag.
- Detection: health checks/watchdogs on heartbeat; promotion flips the endpoint/DNS.
- Application reconnect: connection pools must retry with backoff and handle the new endpoint.
- Failback: after recovery, re-sync and return roles (or let the promoted replica stay primary).
Failover flow.
Examples
Everyday failover-safe connection pattern
# python: reconnect with retry/backoff through the cluster endpointimport timedef query_with_failover(sql, attempts=5): for i in range(attempts): try: return conn.execute(sql) # conn = cluster endpoint except OperationalError: time.sleep(min(2 ** i, 30)) # exponential backoff raise# writer uses the primary/cluster writer endpoint;# reads may use a read replica endpointRetrying through the cluster endpoint hides the failover window from callers.
In production
- A finance write path uses RDS Multi-AZ Postgres; failover drills keep RTO under 60s.
- Azure SQL failover groups route DNS across regions with automatic failover on outage.
- Cloud SQL HA holds p95 availability while a mobile backend reconnects via an HA proxy.
Best practices
- Use cluster/writer endpoints and let the platform flip them at failover.
- Test failover deliberately: once per release or quarterly — measure RTO.
- Bound connection pool size and retry budget so failover doesn't stampede restarts.
- Keep DNS TTL low on the endpoint for external clients.
Common mistakes
- Async-only HA and a 'we're fine' story — then losing committed writes to a lagging replica.
- Connection pools that crash on transient failures instead of retrying.
- Failover tests skipped 'because it's risky' — the risk is assuming it works.
- Half the estate multi-AZ, the other half single zone, discovered mid-incident.
Common issues
Failover takes minutes, not secondsDNS TTL, connection pools, or detection interval
Lower DNS TTL, use cluster endpoint, and check detection heartbeat cadence.
Data loss on failoverAsync replication + lag window
Move to synchronous standby for write-critical workloads or accept the window explicitly.
Debugging tips
- Failover didn't happen: check the health-check frequency vs TCP timeouts.
- App stuck after failover: DNS TTL cached old IP — use the cluster endpoint and reconnect logic.
- Write loss suspected: verify sync vs async on the replication config.
- Promotion lag: monitor replica lag; set failover thresholds accordingly.
Interview checkpoint
Hands-on lab
- Design HA topology with sync/async semantics
- Simulate failover and reconnect
- Measure implied RTO from the design
Plan and Simulate a Database Failover
25 min · intermediate
Quick quiz
Synchronous replication trades ... for ...