Crash-resistant replication: How to avoid MySQL replication errors
Join the DZone community and get the full member experience.Join For Free
percona server’s “crash-resistant replication” feature is useful in versions 5.1 through 5.5. however, in percona server 5.6 it’s replaced with oracle mysql 5.6′s “crash safe replication” feature, which has it’s own implementation (you can read more about it here ).
a mysql slave normally stores its position in files master.info and relay-log.info which are updated by slave io_thread & slave sql_thread respectively. the file master.info contains connection info & replication coordinates showing how many events were fetched from the master binary log. the relay-log.info, on the other hand, represents info showing the positions where the slave applied those events. you can read more about slave status logs here
the relay-log.info file contains the position of the slave relay log, which the slave is applying. somehow, if a crash occurs on the slave between transaction commit and update of relay-log.info, the replication can be inconsistent – indicating that the relay-log.info file may not be in sync on the disk and contains old information. as a result, when the slave starts again it will read old events from the relay log. and because of this the transaction can be applied multiple times.
if your table has a primary key or a unique key defined, it then stops replication with error 1062 “duplicate key error,” which requires manual intervention to skip a problematic event via sql_slave_skip_counter and resume replication. if there is no primary key or unique key defined then it’s even worse because insert may be re-executed and you will get multiple rows with the same data – which again means you’ve got inconsistent data with the master. if you are lucky (i.e. if relay-log.info is already is in sync during crash) then there should be no problem with replication – but that’s not the case most of the time. for update/delete it may cause different errors or no errors on same row.
how to solve this problem?
fortunately, percona server has a feature to deal with these type of situations. as i mentioned above, the “crash-resistant replication” feature first appeared in version 5.1 — i.e. innodb_recovery_update_relay_log needs to be enabled on the slave side. when innodb_recovery_update_relay_log is enabled, it only updates innodb/xtradb tables and will not bother with myisam tables or other storage engines. enabling innodb_recovery_update_relay_log requires a server restart. for more about the crash-resistant replication feature you can refer to documentation here.
let’s illustrate this through two examples, one without and another using this feature to see the difference it makes.
without percona crash-resistant replication:
i am using the mysqlslap utility and will insert data on the master and will kill the slave mysqld process during the middle of insertion. let’s take a look at the impact on the slave. replication is already configured between nodes. let’s start inserting data on the master via the mysqlslap utility. i am going to use,
this setting of 100000 total queries with 100 concurrent clients will run 100000/100 = 1000 queries per client 10 times in this test. it’s running and replicating to the slave eventually, but let me kill the slave mysqld process and start mysql again to see the impact. started mysqld again on slave node.
now let see the slave status….
it’s broken, the slave sql thread failed to start. why ?
the problem would be when mysqld crashed on the slave, the transaction and update of the relay-log.info file was committed to the slave relay log, which didn’t get updated with the latest info.
there are two causes of this problem.
1) if the slave sql_thread refuses to start because of a duplicate key error, the slave relay log will not get updated and it therefore contains old information even though the slave already played the event. you can inspect the master binary log and verify that problematic event already played to slave via select query. one easy solution for this is to skip the event via sql slave skip counter and resume replication – and to be on the safe side also use percona toolkit’s pt-table-checksum to check for data incompatibilities.
2) if the table doesn’t contain a primary key then the slave will run fine and will re-execute the already played event silently (as the slave process didn’t stop) and yields data inconsistencies between master and slave. again this can be solved via pt-table-checksum tool or with manual intervention to verify whether or not the record has been duplicated.
this problem simply brings up data inconsistencies and delays in replication because of errors. this is where percona server’s crash-resistant replication feature comes into play, saving you from encountering this problem by enabling innodb_recovery_update_relay_log on the slave.
let me illustrate this by example….
with percona server’s crash-resistant replication:
first, you need to enable innodb_recovery_update_relay_log on the slave. set it to innodb_recovery_update_relay_log=1 in my.cnf and restart mysql. this will verify whether it’s enabled or not.
this will also be written in the error log when innob_recovery_update_relay_log is enabled.
now, let me re-run the same mysqlslap command that i executed earlier with the same concurrency. i’ll start the slave with –skip-slave-start to prevent the start of the slave on mysql startup and will then crash the slave once again to see the change in behavior. the result is that the relay log position has been overwritten to ‘ 17048324 ‘ from ‘ 17047697 ‘ so replication will resume from the correct position 17048324 . this can also be verified from the mysql error log.
let’s take a look at the slave status at this point…
as you can see, the exec_master_log_pos has been updated to the correct position to resume replication i.e. 17048324. further, as you can see in the error log, the binary log overwritten message is also there.
and if the slave relay-log.info already has the correct information and has been synchronized with the disk after the crash, this will also appear in the error log.
using percona server’s “crash-resistant replication” feature will result in avoiding replication errors. using it will save you from hidden replication errors and thus ultimately saves you from data inconsistencies. overall, this is a nice feature that prevents replication delays and slave-synchronization issues.
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
13 Impressive Ways To Improve the Developer’s Experience by Using AI
The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
How To Scan and Validate Image Uploads in Java
Constructing Real-Time Analytics: Fundamental Components and Architectural Framework — Part 2