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

ItemValue
Enable/disableinnodb_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 sizeinnodb_doublewrite_files, innodb_doublewrite_pages
Torn page detectionPage checksum (innodb_checksum_algorithm, default crc32), comparison of the LSNs at the start and end of the page
CostData 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

InnoDBPostgreSQLSQL ServerOracle
CountermeasureA copy in the doublewrite areaThe full page image goes into the WAL on the first change after a checkpointNo copy normally. Detected by checksum, then restored from backupNo copy normally. Detected by block checksum, then RMAN block recovery
Torn page detectionPage checksumData checksums (initdb option, on by default from 18)PAGE_VERIFY CHECKSUMBlock checksum (DB_BLOCK_CHECKSUM)

In the articles

NOTE File locations and setting values differ by version. In 8.0.20 the area moved out of the system tablespace into separate files.

Related pages