innodb_flush_log_at_trx_commit
innodb_flush_log_at_trx_commit decides how reliably the redo log reaches disk when a transaction commits. The default value 1 writes the log and finishes fsync on every commit before replying, so no committed transaction is lost. Lowering it to 0 or 2 reduces the number of fsync calls and raises write throughput, but a failure can lose the commits of about the last second.
Key Specifications
| Value | At commit | Background (about every second) | What can be lost |
|---|---|---|---|
1 (default) | Write the log buffer to the file and fsync | — | Nothing (as long as the storage device honors fsync) |
2 | Write the log buffer to the file (as far as the operating system cache) | fsync | About the last second if the operating system or server loses power. Nothing if only the MySQL process dies |
0 | Nothing | Write to the file and fsync | About the last second even if only the MySQL process dies |
| Item | Value |
|---|---|
| Background interval | innodb_flush_log_at_timeout, default 1 second |
| How to change | SET GLOBAL, no restart needed |
| Related setting | sync_binlog (fsync interval of the binary log, default 1) |
How It Works
Figure. What finishes before the commit reply, by value
flowchart LR
C["COMMIT"] --> B["Log buffer"]
B -- "1, 2: write at commit" --> O["Operating system cache"]
B -- "0: write every second" --> O
O -- "1: fsync at commit" --> D["Redo log on disk"]
O -- "0, 2: fsync every second" --> D
Diagram notes: the redo records of the committed transaction are in the Log buffer. Value 1 writes them to the Operating system cache and runs fsync to the Redo log on disk before the commit reply. Value 2 writes only as far as the operating system cache at commit and runs fsync every second. Value 0 batches even the write to the operating system cache once per second. If a failure happens before the data reaches disk, the commits in between are lost.
Group commit. Even with value 1, transactions that commit at the same time are flushed together with a single fsync. So with many concurrent connections, the throughput gap between 1 and 2 shrinks. The gap is largest when a single session commits short transactions back to back.
With the binary log. The binary log used for replication and point-in-time recovery must also be flushed to disk separately. Use innodb_flush_log_at_trx_commit = 1 together with sync_binlog = 1 so that the contents of InnoDB and the binary log do not diverge after a failure (commonly called "double 1"). If either one is lowered, the data on a replica and on the source can differ.
Where it is used. It is often lowered to 2 temporarily for work where a failure just means starting over, such as building a replica or a bulk load. Production source servers use 1.
Configuration and Checks
SHOW VARIABLES WHERE Variable_name IN ('innodb_flush_log_at_trx_commit', 'sync_binlog', 'innodb_flush_log_at_timeout');
SET GLOBAL innodb_flush_log_at_trx_commit = 2;
-- Number of redo log fsync calls
SHOW GLOBAL STATUS WHERE Variable_name IN ('Innodb_os_log_fsyncs', 'Innodb_os_log_pending_fsyncs');
Comparison with Other DBMSs
| InnoDB | PostgreSQL | Oracle | SQL Server | |
|---|---|---|---|---|
| Setting | innodb_flush_log_at_trx_commit = 0 / 2 | synchronous_commit = off | COMMIT WRITE NOWAIT / BATCH | Delayed Durability |
| Scope of the setting | Whole server | Server, session, transaction | Session, statement | Database, transaction |
| Range that can be lost | About the last second | The last few hundred milliseconds (about 3 times wal_writer_delay) | Commits whose log write has not finished | Commits remaining in the log buffer |
From the Articles
- 6.2 fsync: Confirming Writes to Persistent Media
- 6.3 Group Commit: Many Commits with One fsync
- 6.6 Differences by DBMS and Phenomena in Practice
NOTE The background interval is not exactly 1 second and can vary with version and load.