08 - Replication & High Availability
Why Replication?
Replication serves multiple purposes:
- High Availability: If primary fails, replica takes over
- Read Scaling: Distribute read load across replicas
- Disaster Recovery: Geographic redundancy
- Backups: Run backups without impacting primary
- Analytics: Run reports on replica, not production
Types of Replication
1. Physical (Binary) Replication
Replicates the exact disk blocks from primary to replica.
Plain text
┌─────────────────────────────────────────────────────────────┐
│ Physical Replication │
├─────────────────────────────────────────────────────────────┤
│ │
│ Primary Replica │
│ ┌─────────┐ ┌─────────┐ │
│ │ Data │◄─────────────►│ Copy │ Identical bytes │
│ │ Files │ WAL stream │ of │ │
│ │ WAL ────┼──────────────►│ Data │ Block-level │
│ └─────────┘ └─────────┘ │
│ │
│ How it works (PostgreSQL Streaming Replication): │
│ 1. Primary writes to Write-Ahead Log (WAL) │
│ 2. WAL records streamed to replica in real-time │
│ 3. Replica applies WAL to its copy of data │
│ │
│ Characteristics: │
│ - Exact copy (same data files) │
│ - All databases replicated │
│ - Replica is read-only (hot standby) │
│ - Low lag (milliseconds to seconds) │
│ │
└─────────────────────────────────────────────────────────────┘