Sub topic 16
Replication and Read Replicas
Prerequisites and the replication model
Assume you can use Linux commands, edit service configuration, understand IP addresses and ports, and run basic SQL. You should also know how systemd manages services.
Replication keeps one or more copies of a database on other servers. The server that accepts writes is the primary. A copy that receives changes from the primary is a replica. A read replica can handle selected SELECT queries, reducing load on the primary.
Replication is not the same as a backup. A deleted row can be deleted on every replica. Keep independent backups as well.
PostgreSQL streaming replication
PostgreSQL physical streaming replication sends write-ahead log, or WAL, records from the primary to a standby server. The standby replays those records. With hot standby enabled, clients can run read-only queries while replay continues.
This replication is usually asynchronous. The primary confirms a commit before a remote standby has replayed it. If the primary fails immediately afterward, the replica may be missing recent transactions. Synchronous replication can reduce this risk, but it adds write latency and can block writes when the required standby is unavailable.
A PostgreSQL standby normally follows the same major PostgreSQL version as its primary. Configure a replication user, permit replication traffic only over a private network, and protect the connection with TLS or another trusted channel. A replication slot prevents the primary from deleting WAL that a standby still needs. If a standby remains offline, the slot can make the primary's disk fill.
MySQL replication
MySQL replication normally copies binary log events from a source server to a replica. The replica receives the events into relay logs and applies them. GTIDs, or global transaction identifiers, make it easier to identify transactions and reconnect a replica after failover.
MySQL replication is also commonly asynchronous. A replica can show older data than the source, and a read immediately after a write might not find that write on the replica. Applications that require read-after-write consistency should read from the primary or wait until the replica has caught up.
| Concern | PostgreSQL | MySQL |
|---|---|---|
| Change stream | WAL records | Binary log events |
| Replica query mode | Hot standby read-only queries | Read-only replica queries |
| Main lag check | `pg_stat_replication` | `SHOW REPLICA STATUS` |
| Common disk risk | Retained WAL from a slot | Unmanaged binary or relay logs |
Monitoring lag and handling failure
Check replication continuously. A replica that is connected might still be far behind. Monitor replay delay, replication errors, disk space, and the time since the last successful change.
# PostgreSQL: run on the primary
sudo -u postgres psql -c \
"SELECT client_addr, state, write_lag, flush_lag, replay_lag
FROM pg_stat_replication;"
# MySQL: run on a replica
mysql -e "SHOW REPLICA STATUS\G"A replica is not automatically a failover system. Promotion requires deciding which server has the newest valid data, stopping or fencing the old primary, promoting one replica, and redirecting applications. Document and test this procedure. Never allow an old primary to accept writes after a new primary is promoted, or the databases may diverge.
Exercise
An application writes an order to the primary and immediately queries a read replica, but the order is missing. Is replication necessarily broken, and what is a suitable fix?
Show answer
No. Asynchronous replication may be lagging. Read that transaction from the primary, or use a consistency mechanism that waits until the replica has replayed the transaction.