Lesson 5 of 9 · 26m
Operating Replicas, Lag, and Failover
Measure replication health accurately, diagnose lag causes, promote replicas safely, and avoid split-brain and data-loss mistakes.
During failover, nobody cares that the replica dashboard was green yesterday. They care whether the promotion loses money, duplicate-writes orders, or strands transactions. The operational discipline is unglamorous: know your write boundary, measure lag honestly, test promotion, and automate only the steps you understand. A manual but rehearsed failover is better than elegant automation that creates split-brain.
Lag is a pipeline symptom
Replication lag can come from network transfer, relay log I/O, single-threaded apply behavior, lock contention, large transactions, schema changes, or slow queries on the replica. Start by separating retrieval from apply:
SHOW REPLICA STATUS\GCompare Relay_Log_Space, Seconds_Behind_Source, Replica_IO_Running, Replica_SQL_Running, and the GTID sets. In MySQL 8, performance schema tables provide richer detail:
SELECT * FROM performance_schema.replication_connection_status\G
SELECT * FROM performance_schema.replication_applier_status\G
SELECT * FROM performance_schema.replication_applier_status_by_worker\GSeconds_Behind_Source is a hint. GTID differences and heartbeat timing are stronger evidence.
Parallel apply helps only when the workload allows it
Modern MySQL can apply transactions with multiple workers when dependency analysis permits:
replica_parallel_workers = 8
replica_parallel_type = LOGICAL_CLOCKParallel workers do not make every large transaction fast. One million-row update is still one large logical unit of work. The durable fix is often batching writes or changing migration strategy, not turning a knob until errors stop appearing in monitoring.
Failover is a transaction-boundary decision
A safe promotion sequence generally includes:
1. Stop application writes or fence the old source.
2. Confirm candidate replica identity and data currency.
3. Allow remaining relay logs to apply.
4. Verify executed GTID set and consistency expectations.
5. Disable read-only on the new source.
6. Point writes through a controlled mechanism.
7. Rebuild or reparent other replicas.
8. Preserve the old source for investigation and recovery.The order matters. If writes continue on both systems, you now have two divergent sources of truth and no amount of replication tooling will magically reconcile business state.
Fencing beats hope
Use one authoritative write path: DNS alone is often too slow because of TTL and client caching. Proxies, service discovery, orchestrator-controlled virtual IPs, or application configuration can work, but only if they are tested. Old writers, cron jobs, queue consumers, admin tools, and manual consoles all count as write paths.
Replicas have workloads too
A replica serving reads may choose different plans because its buffer pool, schema drift, statistics, or workload differs. A query that is fine on the source can destroy apply capacity on the replica. Replicas are production databases, not disposable copies.
Field notes
- Never promote solely because
Seconds_Behind_Sourceis zero; verify transaction sets and fencing. - A delayed replica is useful for operational mistakes but must not be mistaken for a current failover candidate.
RESET REPLICAdestroys replication metadata; treat it as a controlled operation, not a cleanup habit.- Errant transactions complicate GTID failover and must be inventoried before automation chooses coordinates.
- Test promotion with real application write control. A database-only drill misses the split-brain pathways.
Was this lesson helpful?