High Availability

MySQL Semi-Synchronous Replication: Preventing Data Loss at Failover

Enable MySQL semi-synchronous replication to prevent data loss during failover. Configure AFTER_SYNC wait point, tune timeouts, and monitor semi-sync status.

JusDB Team
March 10, 2025
5 min read
182 views

Standard MySQL async replication can lose committed transactions during failover. Semi-synchronous replication adds a durability guarantee: at least one replica must acknowledge each transaction before the primary commits. Here is how to enable and tune it.

How Semi-Sync Works

With async replication, the primary commits and moves on — replicas apply changes later. With semi-sync, the primary waits for an ACK from at least one replica before returning success to the client. If no ACK arrives within the timeout, it falls back to async automatically.

Enable Semi-Synchronous Replication

sql
-- On primary
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
SET GLOBAL rpl_semi_sync_master_enabled = 1;
SET GLOBAL rpl_semi_sync_master_timeout = 1000;  -- ms before fallback to async

-- On each replica
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
STOP SLAVE IO_THREAD; START SLAVE IO_THREAD;  -- reconnect to activate

MySQL 8.0+ Plugin Names

sql
INSTALL PLUGIN rpl_semi_sync_source SONAME 'semisync_source.so';
INSTALL PLUGIN rpl_semi_sync_replica SONAME 'semisync_replica.so';
SET GLOBAL rpl_semi_sync_source_enabled = ON;
SET GLOBAL rpl_semi_sync_replica_enabled = ON;

Monitor Semi-Sync Status

sql
SHOW STATUS LIKE 'Rpl_semi_sync%';
-- Key metrics:
-- Rpl_semi_sync_master_status: ON = semi-sync active, OFF = degraded to async
-- Rpl_semi_sync_master_no_tx: transactions committed without semi-sync ACK
-- Rpl_semi_sync_master_yes_tx: transactions with successful ACK

Tuning the Timeout

The timeout controls the tradeoff between durability and latency:

  • Low timeout (100ms): Falls back to async quickly, lower latency impact but less durability
  • High timeout (10000ms): Waits longer for ACK, better durability but writes stall if replica is slow
sql
-- Adjust based on your network latency between primary and replicas
SET GLOBAL rpl_semi_sync_master_timeout = 2000;  -- 2 seconds
Tip: In after-commit semi-sync mode (default), the client sees commit success before the ACK. Switch to AFTER_SYNC (lossless) for stronger durability: SET GLOBAL rpl_semi_sync_master_wait_point = AFTER_SYNC

Key Takeaways

  • Semi-sync prevents data loss during primary failure at the cost of slightly higher write latency
  • Use AFTER_SYNC wait point for lossless replication
  • Monitor Rpl_semi_sync_master_status — if it shows OFF, you have fallen back to async
  • Set a realistic timeout based on measured primary-replica network RTT

JusDB Can Help

Semi-synchronous replication is a key step toward zero-data-loss MySQL HA. JusDB can help you configure and monitor it correctly.

Share this article

JusDB Team

Official JusDB content team