MySQL Logging Mechanism
MySQL Logging Mechanism
1. Binlog Writing Mechanism
During transaction execution, logs are first written into the binlog cache.
Upon transaction commit, the contents of the binlog cache are written to the binlog file.
Each thread has its own binlog cache, but all share a single binlog file.
Parameters:
binlog_cache_size: Controls the size of the binlog cache for each thread. Exceeding this size will temporarily store the data on the disk.sync_binlog: Controls the write and fsync timing.- sync_binlog=0: Every transaction commit does a write but no fsync.
- sync_binlog=1: Every transaction commit executes fsync.
- sync_binlog=N (N>1): Every transaction commit does a write, but only does fsync after every N commits.
2. Redo Log Writing Mechanism
During transaction execution, the generated redo logs are first written into redo log buffer.
Redo log potential states:
- Resides in the redo log buffer (physically in the MySQL process memory).
- Written to disk (write) but not persisted (fsync); Physically in the file system’s page cache.
- Persisted to disk (hard disk).
Parameters:
innodb_flush_log_at_trx_commit: Controls the write strategy for the redo log.- =0: Redo logs remain in the buffer upon transaction commit.
- =1: Redo logs are directly persisted to disk upon transaction commit.
- =2: Redo logs are written to the page cache upon transaction commit.
A background thread in InnoDB writes logs from redo log buffer to the file system’s page cache every second and then persists it to the disk.
Uncommitted transaction’s redo logs can also be persisted to the disk.
3. Group Commit Mechanism
- The more members in a group commit, the more I/O operations are saved.
Performance Enhancement Strategies in Case of I/O Bottlenecks:
- Set
binlog_group_commit_sync_delayandbinlog_group_commit_sync_no_delay_countparameters to reduce binlog disk write frequency. - Set
sync_binlogto a value greater than 1 (typically between 100-1000). This might result in binlog loss during power failures. - Set
innodb_flush_log_at_trx_committo 2. This might result in data loss during power failures.
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.