As a web application expands from thousands to millions of active users, a single database server inevitably exhausts its hardware limits. While write operations must maintain strict atomic coordination, read queries typically constitute 90% to 95% of total database traffic.
Replication is the architectural process of copying data asynchronously or semi-synchronously from one master server (the Primary) to one or more auxiliary servers (the Replicas).
1. The Primary-Replica Topology
[ Application Backend ]
/ (Writes: INSERT/UPDATE) (Reads: SELECT queries)
/ ▼ ▼
┌───────────────────┐ ┌───────────────────┐
│ PRIMARY SERVER │ │ LOAD BALANCER │
│ (Master DB) │ │ (ProxySQL) │
└─────────┬─────────┘ └─────────┬─────────┘
│ Binary Log │
│ (binlog stream) ├──► Replica 1
▼ ├──► Replica 2
┌───────────────────┐ └──► Replica 3
│ Network Stream │
└───────────────────┘
- Primary Server: Handles all state-modifying write operations (
INSERT,UPDATE,DELETE, DDL). - Replica Servers: Handle read-only queries (
SELECT). Adding more replicas scales read throughput horizontally!
2. Under the Hood: The Three-Thread Replication Pipeline
Replication operates through three coordinated background threads:
- Binary Log Dump Thread (on Primary):
- Every time a write query modifies data, the Primary records the event in its Binary Log (binlog) on disk.
- When a Replica connects, the Primary spawns a Binlog Dump thread to stream binlog events across the network.
- Replication I/O Thread (on Replica):
- Connects to the Primary, receives the incoming binlog stream, and writes the raw events to its local Relay Log on disk.
- Replication SQL Applier Thread (on Replica):
- Reads events sequentially from the local Relay Log and applies them to the Replica's local storage engine, keeping its data in sync.
3. Binary Log Formats
| Format | How It Records Changes | Advantages | Disadvantages |
|---|---|---|---|
| Row-Based (RBR) | Records the exact before/after binary row image for every modified row. | Safest: Guarantees identical data on replica even with non-deterministic functions (NOW(), UUID()). | Large log volume when updating 1,000,000 rows at once. |
| Statement-Based (SBR) | Records the exact SQL query string executed on the primary. | Compact log size. | Dangerous: Non-deterministic queries (UUID()) produce different data on the replica! |
| Mixed (MBR) | Uses Statement-based by default; automatically switches to Row-based for non-deterministic queries. | Balanced. | Standard legacy compromise. Modern MySQL 8.0 defaults to Row-Based. |
4. Modern GTID-Based Replication (Global Transaction Identifiers)
In traditional replication, administrators had to manually track binlog filenames and byte offset positions (e.g., mysql-bin.000042, position 1074). If a primary crashed, promoting a replica was error-prone.
GTID (Global Transaction Identifier) assigns a mathematically unique identifier (UUID + Transaction ID, e.g. 3E11FA47-71CA-11E1-9E33-C80AA9429562:23) to every committed transaction across the entire cluster:
-- Enabling GTID on MySQL 8.0:
gtid_mode = ON
enforce_gtid_consistency = ON
-- Configuring a Replica with GTID:
CHANGE REPLICATION SOURCE TO
SOURCE_HOST = '10.0.0.10',
SOURCE_USER = 'repl_user',
SOURCE_PASSWORD = 'ReplStrongPassword!2024',
SOURCE_AUTO_POSITION = 1; -- Automatically synchronizes missing GTIDs!
START REPLICA;
SHOW REPLICA STATUS\G
5. Replication Lag & Application Routing
Because standard replication is asynchronous, a fraction of a second may elapse between when a transaction commits on the Primary and when it executes on the Replica. This delay is known as Replication Lag.
The "Read-Your-Own-Writes" Problem:
A customer updates their profile and the app redirects to the profile page. If the update went to Primary, but the profile page reads from a lagging Replica, the customer sees their old profile data!
Solutions:
- Session Routing: Always send the user's immediate subsequent reads to the Primary for 5 seconds after they perform a write.
- Semi-Synchronous Replication: The Primary waits for at least one Replica to acknowledge receipt of the binlog event in its relay log before returning success to the client application.