Skip to content

Lesson 5 of 9 · 24m

Replication Lag, Failover, and Split Brain

How to diagnose lag, choose a failover candidate, promote cleanly, and avoid the two-writer disaster that makes recovery political instead of technical.

The scary part of failover is not promotion. Any competent script can run the commands. The scary part is deciding which copy is authoritative, proving the old writer cannot come back, and knowing how much committed data you are about to lose. If those decisions are improvised during an outage, the outage is already bigger than the database.

Lag is a symptom, not a diagnosis

SHOW REPLICA STATUS\G

Seconds_Behind_Source is useful but incomplete. It measures time according to the events currently being processed, not a promise that every event has arrived. A replica can show zero lag while its receiver thread is disconnected, because there is no newer event to measure. First check thread state, then compare executed GTID sets.

-- MySQL 8
SELECT CHANNEL_NAME, SERVICE_STATE, RECEIVED_TRANSACTION_SET
FROM performance_schema.replication_connection_status;

SELECT @@GLOBAL.gtid_executed;
SELECT CHANNEL_NAME, WORKER_ID, SERVICE_STATE, LAST_APPLIED_TRANSACTION
FROM performance_schema.replication_applier_status_by_worker;

Large transactions are a classic cause: nothing moves for ten minutes, then lag collapses when the transaction finally commits. Batch jobs, schema changes, and row events without good indexing on the replica can create the same shape.

Parallel appliers help only when the workload allows it

MySQL 8 can apply transactions in parallel using logical clock or write-set dependency information, controlled by settings such as replica_parallel_type and replica_parallel_workers. MariaDB has its own parallel replication modes. If the workload is one huge serialized stream of dependent writes, adding workers will not invent parallelism that the data does not contain.

A safe failover sequence

  1. Stop application writes at the traffic layer or service control plane.
  2. Fence the old source: power it off, block network access, revoke virtual IP ownership, or confirm it is truly gone.
  3. Choose the most advanced replica using executed GTID sets, not wall-clock lag alone.
  4. Wait for the candidate to apply everything it has already received.
  5. Disable read-only modes and promote it.
  6. Re-point writes through DNS, proxy, or service discovery.
  7. Rebuild the old source from a fresh backup or clone once it returns.

Do not skip fencing. If the old source still accepts writes after promotion, you now have two timelines. Reconciling them is manual, application-specific, and usually ugly.

Orchestrators are useful, not omniscient

Tools such as Orchestrator, replication-manager, MHA, and MaxScale automate detection and promotion. They still need correct fencing, quorum decisions, and network partition behavior. Automation that promotes during a brownout can turn a short source stall into a split-brain incident. Test the tool by killing machines, not by reading its dashboard.

Split brain leaves forensic evidence

After an accidental dual-write period, compare GTID sets, row counts, checksums, and application-level invariants. pt-table-checksum can help find differences, but it does not tell you which row is semantically correct. The business decides whether an order placed on the old source or a password reset on the new one wins. This is why prevention is cheaper than repair.

Field notes

  • Keep a written failover runbook with hostnames, credentials locations, fencing steps, and rollback criteria.
  • Measure replication in dashboards continuously; discovering lag during failover means your monitoring failed earlier.
  • Never point writes back at an old source just because it is up. Rebuild it from the promoted source.
  • Semi-sync reduces data loss but does not eliminate the need for fencing or consistency checks.

Was this lesson helpful?

Sign in to rate