Data Integrity and Performance Considerations in MySQL Semisynchronous Replication
Tags: mysql, replication, semisynchronous, performance
Published: 2018-07-11 16:31:41

---
title: Data Integrity and Performance Considerations in MySQL Semisynchronous Replication
published: true
description: In this post, you'll learn the MySQL Master and Slave Node configurations that influence the data integrity and performance of semisynchronous replication. 
tags: MySQL, Replication, Semisynchronous, Performance,
canonical_url: https://scalegrid.io/blog/data-integrity-and-performance-considerations-in-mysql-semisynchronous-replication/
---
<link rel="canonical" href="https://scalegrid.io/blog/data-integrity-and-performance-considerations-in-mysql-semisynchronous-replication/" />

<img src="https://thepracticaldev.s3.amazonaws.com/i/t1m090hi0g1bswpqvwmg.png" alt="Data Integrity and Performance Considerations in MySQL Semisynchronous Replication - ScaleGrid Blog">
<p style="text-align: justify;"><a href="https://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html" target="_blank">MySQL semisynchronous replication</a> provides improved data integrity because when a commit returns successfully, it’s known that the data exists in at least two places - the master and its slave. In this blog post, we review some of the MySQL configurations that influence the data integrity and performance aspects of semisynchronous replication. We’ll be using <a title="MySQL Server - Calculating InnoDB Buffer Pool Size" href="https://scalegrid.io/blog/calculating-innodb-buffer-pool-size-for-your-mysql-server/" target="_blank">InnoDB storage engine</a> and GTID-based replication in a 3-node replica set (master and 2 slaves), which will ensure there is redundancy in the slaves. This means that if there are issues with one slave, we can fall back on the other.</p>
<p style="text-align: justify;">Read the original post: <a href="https://scalegrid.io/blog/data-integrity-and-performance-considerations-in-mysql-semisynchronous-replication/" target="_blank">Data Integrity and Performance Considerations in MySQL Semisynchronous Replication</a>

<h2><span style="font-weight: 400;">Configurations Applicable to Both Master and Slave Nodes</span></h2>
<ul>
	<li><a href="https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_flush_log_at_trx_commit" target="_blank">innodb_flust_log_at_trx_commit</a> = 1</li>
	<li><a href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html#sysvar_sync_binlog" target="_blank">sync_binlog</a> = 1</li>
</ul>
<p style="text-align: justify;">These configurations guarantee high durability and consistency settings for data. That is, each committed transaction is guaranteed to be present in binary logs and also the logs are flushed to the disk. Hence, in the case of a power failure or operating system crash, the data consistency of MySQL is always preserved.</p>

<h2><span style="font-weight: 400;">Configurations on the Master Node.</span></h2>
<ul>
	<li><a href="https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_rpl_semi_sync_master_wait_for_slave_count" target="_blank">rpl_semi_sync_master_wait_for_slave_count</a>:</li>
</ul>
<p style="text-align: justify;">This option is used to configure the number of slaves that must send an acknowledgment before a semisynchronous master can commit the transaction. In the 3-node replica set, we recommend setting this to 1, so that we always have an assurance that the data is available in at least one slave while avoiding any performance impact involved in waiting for acknowledgment from both the slaves.</p>

<ul>
	<li><a href="https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_rpl_semi_sync_master_timeout" target="_blank">rpl_semi_sync_master_timeout</a>:</li>
</ul>
<p style="text-align: justify;">This option is used to configure the amount of time that a semisynchronous master waits for slave acknowledgment before switching back to asynchronous mode. We recommend setting this to a large number so that there is no fallback to asynchronous mode which then defeats our data integrity objective. Since we’re operating with 2 slaves and <a href="https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_rpl_semi_sync_master_wait_for_slave_count" target="_blank">rpl_semi_sync_master_wait_for_slave_count</a> is set to 1, we can assume that at least one of the slaves does acknowledge within a reasonable amount of time, thereby minimizing the performance impact of this setting.</p>

<h2><span style="font-weight: 400;">Configurations on the Slave Nodes</span></h2>
<p style="text-align: justify;">In the slaves, it’s always important to track two positions very accurately: the current executed position of SQL thread in relay log, and the current position of the IO thread which indicates how far the mater binary file is read and copied to slave. The consequences of not maintaining these positions are quite obvious. If there's a slave crash and restart, SQL thread can start processing transactions from a wrong offset or the IO thread can start pulling data from a wrong position in the master binary logs. Both these cases will lead to data corruption.</p>
<p style="text-align: justify;">it is important to ensure crash-safety of slaves through the following configurations:</p>

<ul>
	<li><a href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#sysvar_relay_log_info_repository" target="_blank">relay_log_info_repository = TABLE</a></li>
	<li><a href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#sysvar_relay_log_recovery" target="_blank">relay_log_recovery = ON</a></li>
</ul>
<p style="text-align: justify;">Setting <a href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#sysvar_relay_log_info_repository" target="_blank">relay_log_info_repository</a> to TABLE will ensure the position of the SQL thread is updated together with each transaction commit on the slave. However, it’s difficult to maintain the exact position of IO thread and flush to the disk. This is because reading master binary log and writing to slave relay log is not based on transactions. The impact on performance is very high if IO thread position has to be updated and flushed to disk after each write to slave relay logs. A more elegant solution would be to set <a href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#sysvar_relay_log_recovery" target="_blank">relay_log_recovery</a> = ON, in which case, if there’s a MySQL restart, current relay logs will be assumed to be corrupted and will be freshly pulled from the master based on the SQL thread position.</p>
<p style="text-align: justify;">Last but not least, it’s important to note that semisynchronous replication ensures that the data has just ‘reached’ one of the slaves before the master committing the transaction, and does not mean that the transactions are committed on the slave. Hence, it will be good to ensure that the SQL thread works with good performance. In the ideal case, the SQL thread moves hand in hand with the IO thread so we can have the benefit of slave not only receiving the transactions, but also committing them. It’s recommended to go with a multi-threaded slave configuration so that we can get increased slave SQL thread performance. The important configurations for multi-threaded slaves are:</p>

<ul>
	<li style="text-align: justify;"><a href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#sysvar_slave_parallel_workers" target="_blank">slave_parallel_workers</a> :  Set this to &gt; 1 to enable multiple slave SQL thread workers. Based on the number of threads writing on the master, we can decide an optimal number for this so that the slave does not lag.</li>
	<li><a href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#sysvar_slave_parallel_type" target="_blank">slave-parallel-type</a> = LOGICAL_CLOCK</li>
	<li><a href="https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#sysvar_slave_preserve_commit_order" target="_blank">slave-preserve-commit-order</a> =1</li>
</ul>
<p style="text-align: justify;">The above configurations are going to promise parallelism in the slave, while at the same time, preserving the order of transactions as seen on the master.</p>
<p style="text-align: justify;">In summary, by using the above configurations on our MySQL replica set, we’re able to maintain high data integrity along with an optimal performance.</p>
<p style="text-align: justify; padding-bottom: 20px;">As always, if you have any questions, leave us a comment, reach out to us at <a href="https://twitter.com/scalegridio" target="_blank">@scalegridio</a> on Twitter, or send us an email at <a href="mailto:support@scalegrid.io" target="_blank">support@scalegrid.io</a>.</p>