Understanding the Flush, Sync, and Commit Subphases of MySQL 8.0.32 Binlog Transaction Commit
This article explains how MySQL 8.0.32 processes the flush, sync, and commit subphases during a two‑phase commit, detailing the roles of leader and follower threads, log handling, system variables, and the conditions that trigger binlog rotation and persistence.
The article, based on MySQL 8.0.32 source code with the InnoDB storage engine, walks through the three subphases—flush, sync, and commit—of the binlog transaction commit process, using a scenario of 30 concurrent transactions to illustrate thread coordination.
Flush Subphase : Thread 16 becomes the flush leader, acquires LOCK_log , gathers threads 17‑30 as members, moves them out of the flush queue, triggers the OS to flush all redo logs, and then writes the accumulated binlog data to the binlog file in 4096‑byte chunks (constant IO_SIZE ). If the binlog file size reaches or exceeds max_binlog_size (default 1 GB), a rotate = true flag is set for later rotation.
Sync Subphase : Threads 6‑15 are already in the sync queue, with thread 6 as the sync leader. The flush leader releases LOCK_log , allowing the sync leader to acquire LOCK_sync . The sync leader checks the counter sync_counter ; if sync_counter + 1 is greater than or equal to the system variable sync_binlog , the subphase proceeds, otherwise it ends without work. When proceeding, the leader may wait either until the delay specified by binlog_group_commit_sync_delay expires or until the number of threads reaches binlog_group_commit_sync_no_delay_count . After waiting, the sync leader triggers the OS to flush the binlog to disk, resets sync_counter when the threshold is met, and ensures durability against crashes.
Commit Subphase : Threads 1‑5 are in the commit queue, with thread 1 as the commit leader. Thread 6 (the former sync leader) joins as a member and releases LOCK_sync . The commit leader acquires LOCK_commit and, based on the boolean variable binlog_order_commits , either commits each InnoDB transaction sequentially (if true) or commits its own transaction first and lets members commit individually (if false). Finally, if the earlier flush leader set rotate = true , the commit leader handles binlog file rotation and purges expired logs according to binlog_expire_logs_auto_purge , binlog_expire_logs_seconds , and expire_logs_days .
Summary : The flush subphase writes redo logs to disk and appends binlog entries; the sync subphase conditionally flushes the binlog based on sync_counter and related variables; the commit subphase finalizes InnoDB transactions and may rotate binlog files, with behavior controlled by binlog_order_commits and other system settings.
Aikesheng Open Source Community
The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.