/ THE IDEA
A typical write can copy bytes, small units of information, into a temporary memory area and return before the device transfer finishes. The operating system is the software managing that area and the computer’s other resources. That batching improves efficiency: several small changes can be handled together. But ordinary working memory loses its contents when power disappears. On Linux, fsync asks for the file’s outstanding changes to be transferred and waits until the device reports completion. The programme must check the result for failure. It must also flush any separate application-level buffer first; the operating system cannot synchronise bytes it has not yet received.
THE FORMAL IDEA
application buffer → system write → successful fsync → acknowledge
| The arrows specify an ordering for a simplified durability promise. They do not assign a fixed duration to any step. | | “Acknowledge” means tell the caller that this record is committed under the stated storage guarantees. An error result is not equivalent to a successful completion. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
An existing file receives record A, 100 bytes. Its write returns while the bytes remain in volatile memory, storage that loses contents without power. Power fails before synchronisation: A may be absent after restart. Change only failure timing: power now fails after successful fsync, with no later file changes. Under the assumed honest storage guarantees, A survives because the required transfer completed before the failure.
|
Waiting moves the confirmation after a persistence boundary. It does not prevent the power failure. If synchronising takes an invented 4 milliseconds, acknowledging before it finishes hides that wait by accepting a different risk, not by performing the same guarantee faster.
/ SO WHAT?
Batching ten records behind one flush can share the waiting cost, but callers must understand which batch has actually completed. Creating or renaming a file adds another subtlety: on Linux, making the file itself durable does not necessarily persist its containing directory entry. The relevant directory may need synchronisation too.
ONE CAVEAT |
| Durability depends on filesystem, device, hardware and correctly reported completion. fsync is not a backup, and it does not make several arbitrary file writes one atomic application transaction. This is a conceptual sequence, not a complete crash-safe saving recipe. |
KEEP THIS
A durability promise should follow successful persistence confirmation, not merely the moment a programme handed bytes to a temporary buffer.
|
NEXT: Why spinning twice as fast is more than twice the push
|