Try
↑ ↓ move↵ openesc close

Interview prep / B-trees

Senior · B-trees · System design interview question

A crash happens while a page is being written, and only part of it reaches disk. How does a B-tree engine cope?

Sketch your own answer first, then compare it with the one below.

A strong answer

A torn page cannot be repaired by replaying a log record that says "change bytes 40 to 80", because the base page is no longer a valid page.

There are two common defences. One is to write a full image of a page into the log the first time it changes after a checkpoint, so recovery can restore the whole page and then apply later records. The other is to write pages first to a separate area and then copy them to their place, so a good copy always exists. Page checksums let the engine detect the damage in the first place.

The trade-off is more bytes written. This is one more reason write amplification in a B-tree is higher than the size of the record suggests.

Sources

Learn it by running it

The b-trees and page writes lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms