Section 8.2

Durability

Definition

Durability is the guarantee that once a system reports an operation as complete, the effect of the operation survives every failure the system is designed to survive, such as a crash, a reset or a sudden loss of power. The report of completion is called the acknowledgment. Before the acknowledgment the effect may or may not survive a failure; after it, the effect must survive until a later operation replaces it. Durability is the D in ACID (atomicity §8.3, consistency, isolation, durability), the four guarantees of database transactions §8.7.

The idea

A data logger offers a function that stores one measurement. To be fast, the function copies the measurement into a buffer in RAM and returns success at once; a background task writes the buffer to flash memory §6.6 a few milliseconds later. If power fails in between, the measurement is gone, although the caller was told that it was stored. The caller may already have deleted its own copy, or told someone else that the measurement is safe.

Durability is about which report of success the caller can rely on. A write passes through several stages on its way to storage, and each stage can report that it has accepted the data. A letter handed in at a post office counter has been accepted, not delivered; only a delivery receipt says that it arrived. In the same way, only a report sent after the data has reached storage that survives the failure says that the data is durable. The analogy stops at one point: the write can complete and its report can then be lost, as if the letter arrived but the receipt went missing.

How it works

On its way to the storage medium, the data of a write passes through several places, and each of them holds the data in memory that a loss of power erases:

  1. the program’s own buffer;

  2. buffers in a library or in the operating system, such as the page cache, the part of RAM in which an operating system keeps file data before writing it to the disk;

  3. the queue of the device driver §6.38, the software that controls the storage device;

  4. a cache inside the storage device; many disks and solid-state drives keep recent writes in RAM of their own.

Only when the data reaches the medium itself, such as the cells of flash memory or the surface of a disk, is it safe from a loss of power. The part of this path whose content survives a given failure is often called the persistence domain.

To give durability, a system issues the write, waits for a completion signal that is defined to arrive only after the data is in the persistence domain, and only then sends the acknowledgment. On POSIX systems (Portable Operating System Interface, the standard interface of Unix-like operating systems) that signal is the return of the call fsync on the file. According to the Linux manual, fsync transfers the file’s modified data to the storage device, flushing the device’s own cache if it has one, and returns when the device reports that the transfer is complete. On a microcontroller §6.1, a small computer on one chip that writes its flash memory directly, the signal is the flash controller’s report that the program operation has finished.

Everything that recovery needs in order to find the data must be durable too. A record that is safely stored but that no index or commit record §8.21 points to is invisible after a restart. For files, this is the question of whether the directory entry is durable, which the fsync and directory durability §15.22 entry treats. The data must also reach storage before the record that points to it, as the Write ordering §8.20 entry explains.

The acknowledgment can be lost separately from the write. If the device loses power after the effect became durable but before it sent the acknowledgment, the effect is present after the restart and the caller never heard about it. A caller that receives no acknowledgment therefore cannot conclude that nothing happened. A retry may apply the operation twice. That is harmless only for an operation with idempotency §8.31, the property that applying it twice has the same effect as applying it once, or when the request carries an identifier that lets the receiver recognize a repeat, as the Exactly-once effect §8.32 entry explains.

Worked example

A sensor replaces a stored setting, value 7, with value 8. In this hypothetical timeline, times are in milliseconds (ms) after the request arrives:

Look at a loss of power at three moments.

The reply sent at 4 ms is the acknowledgment that carries the durability guarantee, because the sensor sent it after the completion at 3 ms.

Stated precisely

Let pp be the instant at which the effect of an operation, and every record that recovery needs to find it, are completely in the persistence domain. Let aa be the instant at which the acknowledgment is sent, and let tt be the instant of a failure that the failure model §1.30 covers. Durability is the contract that if t≥at \ge a, the state after recovery contains the effect, or the effect of a later operation that replaced it, whether or not that later operation was acknowledged. An implementation keeps the contract only if p≤ap \le a in every execution. For t<pt < p the effect may be present or absent after recovery. For p≤t<ap \le t < a it is present, although it was never acknowledged.

The contract rests on three assumptions.

  1. An honest completion signal. The signal that the acknowledgment waits for arrives only after the data is in the persistence domain. A storage device that reports a flush as complete while the data still sits in its own volatile cache breaks the chain.

  2. Recovery finds the effect. The recovery procedure, the code that rebuilds working state at start-up, locates the stored record and accepts it.

  3. The failure is in the model. Crash, reset and loss of power are the usual failures covered. Destruction of the medium, use beyond its rated retention time or number of erase cycles, and deliberate tampering are usually not.

Limits and common mistakes

Durability is not atomicity. An operation that changes three records can, after a failure, have two of them durable and the third lost. Making a multi-record change all or nothing is the subject of the Atomicity entry.

Surviving a program crash is not surviving a loss of power. Data that a program has handed to the operating system with an ordinary write call sits in the page cache. It survives if the program crashes, because the operating system still writes it out, but it is lost if power fails, or the operating system itself crashes, before the write-out.

A check value shows that a record is intact, not that it is the newest. If the newest acknowledged record was lost, recovery finds an intact older record and accepts it; nothing in the older record reveals the loss.

Durability gives no protection against an attacker. Someone who can write the storage can put back an old, valid copy. Preventing that is anti-rollback §8.26 protection, which the Rollback versus anti-rollback entry treats.

Durability costs time, because waiting for the medium after every operation is slow. Systems often group several operations behind one flush, and they may acknowledge each of those operations only after the shared flush completes.

Questions answered

At which acknowledgment does the durability guarantee begin?

The durability guarantee begins at the first acknowledgment that is sent only after the effect has completely reached storage that survives the failure in question, together with everything the recovery procedure needs to find it, such as the index entry or commit record that points to it. Before that acknowledgment, a failure may or may not preserve the effect. From that acknowledgment on, recovery must preserve the effect unless a later operation replaces it.

Earlier reports of success do not start the guarantee: a function that returns after copying data into a RAM buffer, a write call that hands data to the operating system, or a storage device that accepts data into its own volatile cache. On a Unix-like operating system the boundary is the return of the call fsync, applied to the file and, when a file was created or renamed, to its directory as well. On a microcontroller that writes flash memory directly, it is the flash controller’s report that the program operation finished. If power fails between completion and acknowledgment, the effect survives unacknowledged, so a caller that received no acknowledgment must treat the outcome as unknown, not as failed.

Related entries: Write ordering (§8.20), Exactly-once effect (§8.32), fsync and directory durability (§15.22). Further reading: [140].