Dump Thread Enhancement On MySQL-5.7.2
On
the new milestone release MySQL 5.7.2, we did some optimizations on
binlog lock and dump thread. Major dump thread cod...
On
the new milestone release MySQL 5.7.2, we did some optimizations on
binlog lock and dump thread. Major dump thread code was re-written: Some
code never reached was removed; Complex
logic was simplified; Code became more readable. But I don't want to
introduce the refactoring here. Today, I would like to introduce you the
optimization on binlog lock which improved master throughput.
Background
binlog lock(LOCK_log) is a mutex which is used to protect binlog read
and write operations. With this mutex, binlog read and write operations
are safe. But it has limited concurrency. Not only can dump threads and
user sessions not read and write binlog simultaneously but even the dump
threads themselves cannot read the binlog simultaneously.

All other sessions have to wait whenever one session is reading or writing a binlog file. So sequential
read and write could be a bottleneck especially when read and write operations are slow.
Better Concurrency in MySQL 5.7.2
To improve binlog read and write concurrency, we made the following optimization:
- Removed binlog lock from dump thread
- Introduced safe read boundary to guarantee binlog read safe.
Binlog looks like a log file which is append only, it is safe to read
the binary events which are appended without a lock. And so the binlog
lock was removed from the dump thread. Instead of using the binlog lock,
the safe read boundary(max position) is maintained for the active
binlog.
Dump threads never read positions that exceed the safe read boundary. It
will wait for the boundary to be updated when it reaches the boundary.
User sessions are responsible for updating the safe read boundary after its binlog events have been appended.
From above diagram, we can know that:
- Dump threads are not blocked by each other while reading binlog events.
- Dump threads are not blocked by the user session which is writing binlog events.
- Users sessions are not blocked by dump threads which is reading binlog events.
So both dump threads and users sessions get better throughput and the
improvement is remarkable especially when there are many slaves.