InnoDB doublewrite buffer
An InnoDB page (16KB) is larger than the unit the operating system and storage device write atomically in one operation (usually 4KB). If power fails while a page is being written, a torn page can remain, with new values in the first part and old values in the rest. The redo log works by reapplying changes to an intact page, so it cannot repair a torn page. Therefore InnoDB first writes dirty pages sequentially to the doublewrite area, and writes them in place in the data file only after that write has reached disk.
Key specs
| Item | Value |
|---|---|
| Enable/disable | innodb_doublewrite, default ON. From 8.0.30 it also accepts DETECT_AND_RECOVER, DETECT_ONLY, and OFF |
| Location (from 8.0.20) | Separate files such as #ib_16384_0.dblwr (innodb_doublewrite_dir) |
| Location (before 8.0.20) | A fixed area inside the system tablespace (ibdata1) (128 pages, 2MB) |
| File count and batch size | innodb_doublewrite_files, innodb_doublewrite_pages |
| Torn page detection | Page checksum (innodb_checksum_algorithm, default crc32), comparison of the LSNs at the start and end of the page |
| Cost | Data pages are written twice. The first write is sequential, so the actual overhead is less than double the write volume |
How it works
Figure. Double write and recovery
sequenceDiagram
participant BP as Buffer pool
participant DW as Doublewrite file
participant DF as Data file
BP->>DW: ① Write a batch of dirty pages sequentially
DW->>DW: ② fsync
BP->>DF: ③ Write each page in place
Note over DF: Power loss here → torn page
DW->>DF: ④ Restart: replace pages with bad checksums by the copy
Note over DF: ⑤ Then apply the redo log
Diagram explanation: The page cleaner gathers dirty pages from the Buffer pool, ① writes them sequentially to the Doublewrite file in one pass, and ② flushes them to disk. Then ③ it writes each page to its original location in the Data file. If power fails during ③, at restart InnoDB checks the checksums of the data file pages and ④ replaces the pages with bad checksums by the doublewrite copy. If the write is interrupted during ①, the data file still holds the previous intact page. Either way, one intact page exists, so ⑤ the redo log can be applied.
When to disable it. On storage devices or file systems that guarantee 16KB atomic writes (for example ZFS, or some devices that support atomic writes), some deployments disable it to reduce write overhead. Disabling it in an environment without that guarantee can leave pages that cannot be recovered after a failure. Disabling it only temporarily during a bulk load carries the same risk.
Settings and checks
SHOW VARIABLES LIKE 'innodb_doublewrite%';
SHOW VARIABLES LIKE 'innodb_checksum_algorithm';
-- Number of double writes and pages written
SHOW GLOBAL STATUS LIKE 'Innodb_dblwr%';
Dividing Innodb_dblwr_pages_written by Innodb_dblwr_writes gives the number of pages written per batch.
Comparison with other DBMSs
| InnoDB | PostgreSQL | SQL Server | Oracle | |
|---|---|---|---|---|
| Countermeasure | A copy in the doublewrite area | The full page image goes into the WAL on the first change after a checkpoint | No copy normally. Detected by checksum, then restored from backup | No copy normally. Detected by block checksum, then RMAN block recovery |
| Torn page detection | Page checksum | Data checksums (initdb option, on by default from 18) | PAGE_VERIFY CHECKSUM | Block checksum (DB_BLOCK_CHECKSUM) |
In the articles
- 5.1 Power loss during a page write
- 5.3 The rule of writing the log before the data page
- 5.6 Differences between DBMSs and a one-line summary
NOTE File locations and setting values differ by version. In 8.0.20 the area moved out of the system tablespace into separate files.