From MySQL 5.7, we had a Multi-threaded Slave (MTS) Applier mechanism called LOGICAL_CLOCK
to overcome the problems of parallel replication within a database.
To further improve the parallelisation mechanism, from MySQL 8 (5.7.22) we have write-set replication, so before going further , lets look at the difference between Logical clock (LC) and Writeset.
LOGICAL_CLOCK
Transactions that are part of the same binary log group commit on a master are applied in parallel on a slave. The dependencies between transactions are tracked based on their timestamps to provide additional parallelisation where possible.
WRITESET
Write-set
is a mechanism to track independent transactions that can be executed in parallel in the slave. Parallelising on write sets has potentially much more parallelism than logical_clock
,since it does not depend on the commit history, and as such, applying binary logs on a slave, it mean that replication can become much faster when compared to other parallel mechanism.
Below are the variables related write-set are :
transaction_write_set_extraction : specifies the algorithm used to hash the writes extracted during a transaction. binlog_format
must be set to ROW
to change the value of this system variable. XXHASH64 is the most recommended method.
Note : The variable transaction_write_set_extraction is not enabled by default in MySQL 5.7.22 and above.
binlog_transaction_dependency_tracking : specifies the source of dependency information that the source records in the binary log to help replicas determine which transactions can be executed in parallel. The possible values are:
COMMIT_ORDER
: Dependency information is generated from the source’s commit timestamps. This is the default.WRITESET
: Dependency information is generated from the source’s write set, and any transactions that write different tuples can be can be made parallel. in other words the data is parallelised the transactions has not touched or modified the same data row.WRITESET_SESSION
: Dependency information is generated from the source’s write set, and any transactions that write different tuples can be made parallel, with the exception that no two updates from the same session can be reordered.
now we will dive into testing environment and look at how the write-set parallelises the replication?.
I have installed latest MySQL 8.0.23 version for testing purpose
In order to enable the write-set parallelism ,Binlog_transacion_dependency_tracking
is to be set from COMMIT_ORDER
(default) to WRITESET
Creating a test database and table.
Let us insert some sample data into the table writeset_test
table.
To observe the behaviour of WRITESET
, let us decode the binlog and see the how the above inserted transactions will be parallelised.
As we can see from the above logs , the transaction from sequence_number 2-5 will be executed parallel in slave, as the all the transactions are independent and non conflicting to each other.
When the same the row is modified or deleted from multiple transaction , those transactions will not be part of WRITESET
In above example, the id 6 is deleted and inserted back , as this rows will conflicted with 2 transactions, it will not replicated in parallel.
WRITESET_SESSION
Now lets look at another mode Binlog_transacion_dependency_tracking
is WRITESET_SESSION
When we want to restrict the two transaction from same session should not be executed on slave when slave-preserve-commit-order option is not enabled , then WRITESET_SESSION
mode can be used, which basically never allows the transactions from the same session to be executed in parallel.
lets look at example to see the behaviour of WRITESET_SESSION
, now we already now that the 2 transactions in same session cannot be parallelised , so the will execute the the queries from 2 different session and see the results by decode the the binlog
Session 1
In session 1 ,even the transactions are on non-conflicting rows, it cannot be parallelised.
Session 2
From session 2 , the transaction 2(from session1) and transaction 3 are taken in write-set which will can be executed in parallel.
Writeset based replication is the fastest replication method in MySQL. It ensures the faster synchronisation of Replica set by utilising the underlying the hardware efficiently.
Limitations of Writeset
- Supports only InnoDB .
- Ensure tables have Primary keys.
- Foreign key tables will not take benefit of
WRITESET
mode. - Slave_parallel_workers should be greater than zero.
- Larger value of binlog_transaction_dependency_history_size will keep more row hashes in memory.
The writset replication needs fine tuning based on the workload to reap a very optimal performance out of it.