How does semi-synchronous replication work internally in MySQL?

Semi-synchronous replication in MySQL is a method of database replication that strikes a balance between the performance of asynchronous replication and the safety of synchronous replication. In semi-synchronous replication, the master waits for acknowledgment from at least one slave server before confirming that a transaction has been committed. This ensures that data is not lost in case of a master failure, while still allowing for some concurrency advantages over fully synchronous systems.

How It Works Internally

When a transaction is executed on the master server:

  1. The master processes the transaction and writes it to its own binary log.
  2. The master then sends the log event to the slaves that are configured to receive it.
  3. The master waits for an acknowledgment from at least one of the slaves to confirm that it has received and logged the event.
  4. Once acknowledged, the master can proceed with the commit operation.
  5. If no acknowledgment is received within a specified timeout period, the master may fall back to asynchronous replication, allowing transactions to complete without waiting for the slave.

This replication method allows for better data integrity by ensuring that at least one copy of the data exists on a slave before confirming the operation.

Example Configuration

-- On the master CHANGE MASTER TO MASTER_HOST='slave_host', MASTER_USER='replication_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='recorded_log', MASTER_LOG_POS=recorded_position; START SLAVE; -- On both master and slave SET GLOBAL rpl_semi_sync_master_enabled = 1; -- Enable semi-synchronous replication on the master SET GLOBAL rpl_semi_sync_slave_enabled = 1; -- Enable semi-synchronous replication on the slaves

Semi-Synchronous Replication MySQL Database Replication Data Integrity Transaction Acknowledgment MySQL Configuration