# Replication, Failover and Connection Management

> Keep databases available with replicas and automated failover, scale reads, and protect them from connection storms. Free notes, key concepts, resources and a...

Canonical: https://www.internhack.in/roadmaps/tracks/devops/database-replication-ha
Last updated: 2026-11-28

Keep databases available with replicas and automated failover, scale reads, and protect them from connection storms.

Advanced · about 6 hours · difficulty 4/5

Learn first: [Database Backups and Restores](https://www.internhack.in/roadmaps/tracks/devops/database-backups)

A single database server is a single point of failure and eventually a bottleneck. Replication addresses both, with trade-offs you must understand before an outage tests them.

### Replication modes

- **Asynchronous**: the primary commits, replicas catch up. Fast, but a failover can lose the last few transactions. Watch **replication lag**.
- **Synchronous** or semi-synchronous: commits wait for at least one replica. No data loss on failover, higher write latency.
- **Logical replication** streams row changes, useful for upgrades, migrations between versions and feeding other systems (change data capture).

### High availability

- A primary with replicas in other zones, plus a manager that detects failure and promotes a replica: **Patroni** for Postgres, **Orchestrator** or group replication for MySQL, or a managed **Multi-AZ** option.
- Clients must find the new primary: DNS endpoints, a virtual IP or a proxy.
- Guard against **split brain** (two primaries) with consensus-based leader election and fencing.
- Test failover regularly; measure how long writes are unavailable.

### Read scaling

Send read-only queries to replicas, but remember lag: a user may not see their own write. Route read-after-write queries to the primary.

### Connections

Databases handle a limited number of connections, and each app replica has its own pool. Use a pooler such as **PgBouncer**, **RDS Proxy** or **ProxySQL**, size pools deliberately, and set statement timeouts.

### On Kubernetes

Operators such as **CloudNativePG**, Zalando Postgres Operator or Percona operators handle replication, failover and backups. Many teams still prefer managed databases for anything critical.

**Good enough to move on:** you can build a primary with a replica, fail over and measure the write gap, put a connection pooler in front, and explain when read replicas can return stale data.

## Key concepts

- **Async vs sync replication**
- **Replication lag**
- **Automated failover: Patroni, Multi-AZ**
- **Split brain and fencing**
- **Read replicas and read-after-write**
- **Connection pooling: PgBouncer, RDS Proxy**
- **Logical replication and CDC**
- **Database operators on Kubernetes**

## Mini project

Deploy Postgres with CloudNativePG or Patroni (one primary, two replicas), put PgBouncer in front of it, run a write loop while killing the primary, and report how many seconds writes failed.

## Check yourself

Your app creates 20 connections per pod and scales to 50 pods, but the database allows 500 connections. What happens, and how do you fix it?

## Free resources

- [PostgreSQL: high availability and replication](https://www.postgresql.org/docs/current/high-availability.html) (PostgreSQL)
- [CloudNativePG documentation](https://cloudnative-pg.io/documentation/current/) (CloudNativePG)
- [PgBouncer](https://www.pgbouncer.org/) (PgBouncer)

Previous: [Database Backups and Restores](https://www.internhack.in/roadmaps/tracks/devops/database-backups) · Next: [Zero-Downtime Schema Migrations](https://www.internhack.in/roadmaps/tracks/devops/schema-migrations)

## More in Running Databases in Production

- [Database Backups and Restores](https://www.internhack.in/roadmaps/tracks/devops/database-backups)
- [Replication, Failover and Connection Management](https://www.internhack.in/roadmaps/tracks/devops/database-replication-ha)
- [Zero-Downtime Schema Migrations](https://www.internhack.in/roadmaps/tracks/devops/schema-migrations)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DevOps Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/devops)
