Definition
Write ordering is the order in which writes to persistent storage become durable, meaning certain to survive a crash or a loss of power. Crash-safe updates depend on it: a commit marker §8.21, a small record saying that a change is complete, must not become durable before the data it names. The order in which a program issues writes does not fix this order by itself, because buffers and caches, temporary holding areas between the program and the storage medium, may complete writes in a different order.
The idea
A device saves a 4,096-byte settings block and then a 16-byte commit marker that says “the new settings are complete”. After a restart, recovery code, the code that examines storage before anything else runs, uses the new settings only if it finds the marker. The program writes the settings first and the marker second, but each call only copies the bytes into a cache, a fast temporary memory that passes them to storage later. The cache happens to store the small marker first, and power fails before the settings follow. After the restart, recovery finds the marker and uses a settings block that is partly old and partly new.
The marker is a promise about other data, and a promise is safe only once its content is true. Write ordering is the discipline of letting the marker become durable only after every write it vouches for is durable. A delivery notice is a fair picture: the notice “your parcel is in your mailbox” must not be sent until the parcel is there. The picture stops at partial delivery. A parcel arrives whole or not at all, but a storage write cut off by a loss of power can leave its target partly written, a torn write §8.19. Ordering does not prevent torn writes; recovery must detect them separately.
How it works
Between a program’s write call and the storage medium there can be several layers. Each layer may hold a write in volatile memory, which loses its content without power, and pass writes on in its own order.
The compiler and the processor may reorder memory stores, for example the stores that fill a buffer in RAM and the store that tells a hardware controller to copy that buffer to storage.
Software buffers. A library may collect small writes, and an operating system keeps written file data in its page cache, a region of RAM, and writes it to storage later in an order it chooses.
The driver and the storage device. A driver, the code that operates the device, may queue requests, and a disk, an SSD (solid-state drive) or a memory card may keep written data in its own volatile write cache and store it in a different order.
The medium. Writing flash memory §6.6 takes time, typically tens of microseconds for a word and milliseconds to erase a page, the unit in which flash is cleared before it is rewritten, and an operation cut off by a loss of power leaves the affected cells in an undefined state.
A program therefore controls the order with completion signals, not with the order of its calls. A write is complete, in this sense, when the layer that reports completion guarantees that the bytes are on the medium. Three kinds of completion signal are common.
Waiting for a device operation. The flash controller of a microcontroller §6.1 reports, usually through a status bit that software can read, when a write or erase operation has finished. A driver function that returns only after that report has made the write durable. If the controller performs one operation at a time and the driver waits each time, the order of the calls is also the durable order; that conclusion comes from the documented behavior of the controller and driver, not from the source code.
A flush call to the operating system. On a POSIX (Portable Operating System Interface) system, the function
fsynccalled on a file returns after the file’s modified data has been passed to the storage device. On Linux it also asks the device to empty its write cache. On Apple’s macOSfsyncdoes not, and a program must make a separate request for that.A device flush command, which a storage device acknowledges only after everything in its write cache is on the medium.
With these tools, a change that a commit marker vouches for is made in four steps.
Write the data to a place that the current valid state does not use.
Wait until the data is durable.
Write the commit marker, and wait until the marker is durable.
Only then report the change as done.
The wait in step 2 provides the ordering. The wait in step 3 makes the report true: a change reported as done is not lost.
Detection can replace the wait in step 2. The marker carries a checksum §4.32 or a cryptographic hash §5.1 of the data, a short value computed from its bytes, and recovery ignores a marker whose value does not match the stored data. A marker that reached storage before its data then counts as absent, and the change as not made. The Linux ext4 file system offers this for its journal, the log in which it records changes before making them, as the option journal_async_commit. The wait in step 3 is still needed before the change is reported as done.
Worked example
Take a system whose write cache passes cached writes to storage in its own order. Times are in milliseconds (ms) from the first write. The data is 4,096 bytes and the marker 16 bytes.
Without ordering.
At 0 ms the program writes the data. The call copies it into the cache and returns.
At 1 ms the program writes the marker. The call returns at once.
The cache stores the marker on the medium at 3 ms and finishes storing the data at 5 ms.
Power fails at 4 ms. The marker is durable, since . The data is not, since .
After the restart, recovery finds a marker that names incomplete data. Every loss of power between 3 ms and 5 ms, a span of ms, has this outcome.
With ordering.
At 0 ms the program writes the data, and at 1 ms it calls the flush. The flush returns at 6 ms, when the data is on the medium.
At 6 ms the program writes the marker and calls the flush again. That flush returns at 9 ms.
At 9 ms the program reports the change as done.
A loss of power before 6 ms leaves no marker on the medium. The data may be partly written, but nothing names it, and recovery keeps the old state. A loss of power between 6 ms and 9 ms leaves the marker either absent or present, while the data is durable in both cases, so recovery finds either the complete old state or the complete new state. A loss of power after 9 ms leaves the new state, which is what the report promised.
The cost is time. The ordered program reports success at 9 ms. The unordered program returned after 1 ms, but its early return promised nothing about what would survive a loss of power.
Stated precisely
Let be a commit marker and the set of writes it vouches for. For a write , let be the time at which becomes durable, meaning that from then on its bytes survive a loss of power; if never becomes durable. Let be the time at which the program reports the change as done. Write ordering for this change is the condition and a truthful report is the condition . Together they give recovery what it needs at any crash time : if the marker is durable (), then for every , so every write it vouches for is durable; and if the change was reported (), the marker is durable.
A program establishes by issuing only after a completion signal has covered every . The argument rests on four assumptions.
Truthful completion. When a wait or flush returns, every write it covers is durable. A device that acknowledges a flush while data is still in a volatile cache breaks this assumption, and no ordering in software can repair it. Only tests on the hardware reveal such a device, for example power-loss injection §8.38, which cuts the power at chosen moments and checks what survived.
Stable storage. A durable write stays intact until the program writes that location again.
A detectable marker. A marker cut off by a loss of power is either absent or recognizably damaged, for example because it carries its own checksum, so recovery never reads a torn marker as valid.
Out-of-place data. The writes in do not overwrite anything the old state needs, so recovery still has a complete old state when the marker is absent.
Limits and common mistakes
The order of calls in source code is not the durable order. Writing the data before the marker in the program establishes nothing, unless every layer below documents that it completes writes in the order issued, or the program waits for completion.
A memory barrier §4.37, an instruction that makes the processor complete earlier memory accesses before later ones, orders memory accesses, not storage. It is needed when a device must see a filled buffer before the command that starts a transfer, but it never makes data durable, because RAM is not persistent storage. The volatile qualifier §4.34, a keyword of the C programming language that only stops the compiler from removing or reordering certain accesses, gives even less.
The right tools depend on the platform. On byte-addressable persistent memory, memory that keeps its content without power and is written with ordinary store instructions, the tools are processor instructions, and which ones are needed depends on whether the content of the processor caches survives a loss of power. Where it does not, a store that goes into a cache is lost if power fails before its cache line, the fixed-size block in which the cache holds memory contents, is written back to memory. The processor writes lines back at moments of its own choosing, so the marker’s line can reach memory before the data’s lines do. The program therefore writes the data’s lines back with an instruction, and a memory barrier then orders that write-back before later stores, such as the marker. Some platforms use stored energy to write the caches back when power fails. There a store is durable as soon as other processors can see it, so no write-back instruction is needed, and a memory barrier between the data stores and the marker store is enough to keep the marker from becoming durable first.
Ordering is neither atomicity §8.3 nor security. It does not stop the marker or the data from being torn; it only keeps the marker from becoming durable before the data. And an attacker who can write the storage can write a marker for any data; protection against an attacker needs a cryptographic check, such as a message authentication code §5.7, and a check that the marker is not an old one written back.
Each layer needs its own argument. A new file whose data was flushed can still disappear after a crash if the directory that lists it was not flushed; the fsync and directory durability §15.22 entry treats this case.
Every wait costs time. Systems that commit often place several changes behind one marker, which is called group commit, so that they share the wait.
Questions answered
Can a commit marker become durable before the data it names?
Yes. A commit marker, the small record that declares a change complete, can become durable (certain to survive a crash or a loss of power) before the data it names whenever two things hold together: some layer between the program and the storage medium holds writes in volatile memory and passes them on in its own order, and the program writes the marker without first waiting until the data is durable. Such layers include library buffers, the operating system’s page cache, a driver’s queue of requests and a storage device’s own write cache. Writing the data first in the source code does not prevent it. A crash after the marker reaches the medium and before the data does leaves a marker that names missing or partly written data.
To prevent it, the program waits for a completion signal that guarantees the data is durable, and only then writes the marker: for example a flash driver call that returns only after the flash controller reports that the operation finished, or fsync on a system where it also empties the device’s write cache. Alternatively, the marker carries a checksum or cryptographic hash of the data, and recovery ignores a marker whose data does not match. A memory barrier alone is not enough: it orders memory accesses, not writes to storage. An exception is byte-addressable persistent memory, memory that keeps its content without power and is written with ordinary store instructions, on a platform that writes the processor caches back to it when power fails: there a store is durable as soon as other processors can see it, so a barrier between the data stores and the marker store is enough.
Related entries: Durability (§8.2), Write-ahead logging (§8.12), Commit record and selector (§8.21), Memory barrier (§4.37), fsync and directory durability (§15.22), Torn write (§8.19), Volatile qualifier (§4.34), Power-loss injection (§8.38).