← Lessons

quiz vs the machine

Platinum1780

Databases

The Double Write Buffer

The double write buffer guards against torn page writes by saving a full copy of each page before writing it to its final spot.

5 min read · advanced · beat Platinum to climb

The Torn Page Problem

A database page is larger than the atomic write unit of the underlying storage. If a crash strikes mid write, only part of a page may reach disk, producing a torn page that is half old and half new. Such a page is corrupt, and the write ahead log cannot fix it because log based recovery assumes a known good page to start from.

What the Double Write Buffer Does

The double write buffer, used by InnoDB, writes each dirty page twice:

  • First, the page is written sequentially into a dedicated double write area.
  • Then it is written to its real location in the data file.

Because the first write completes before the second begins, there is always at least one intact copy of the page somewhere on disk.

Recovery After a Crash

On restart, the engine scans pages. If it finds a torn page in the data file, it restores the good copy from the double write area, then replays the log on top. This makes the log safe to apply.

The Cost

Every page is written twice, doubling that portion of write traffic. On hardware with atomic page writes the feature can be disabled, since torn pages cannot occur there.

Key idea

The double write buffer copies each page to a safe area before its real write, so a torn page from a crash can always be recovered from the intact copy.

Check yourself

Answer to earn rating on the learn ladder.

1. What problem does the double write buffer solve?

2. Why can the write ahead log alone not fix a torn page?

3. What is the main cost of the double write buffer?