Atomic rename does not make a file update durable
Replacing a pathname atomically protects readers from a partially written file, but crash durability also depends on flushing file contents and the containing directory. The distinction shapes safe configuration and state-file updates.
Atomic visibility and persistence answer different questions
Suppose a program rewrites a small configuration file while another process reads it. Opening the existing file and writing the new bytes in place can expose a prefix of the new version followed by old bytes, or a short file between truncation and completion. A common alternative is to build a replacement file and rename it over the destination. On Linux, rename atomically replaces an existing destination: a concurrent pathname lookup does not observe a moment when that name is missing. Existing open descriptors are unaffected, so a reader that already opened the old file can continue to see its old object. [2]
That is an observation about concurrent processes while the machine is running. It does not answer what will remain after a sudden power loss. A successful rename can make the new name visible immediately without proving that the new bytes and the changed directory entry have both reached stable storage. “Atomic” describes namespace visibility; “durable” describes which state survives a crash. Confusing them can make an update look safe in ordinary tests but lose data after reboot.
A file flush does not necessarily flush its name
Linux fsync() on a file descriptor flushes modified file data and associated metadata, waiting for the storage device to report completion. The manual calls out a separate detail: syncing the file does not necessarily persist the entry in its containing directory. To persist that directory change, the program also needs to sync a descriptor for the directory. [1]
File contents and attributes belong to an inode-like object; a pathname is a directory entry that refers to it. Creating a temporary file and renaming it change directory state. Flushing only the file can protect bytes while leaving uncertainty about whether its name survives; persisting the name cannot complete unwritten contents. A replacement protocol must order both.
Treat replacement as an ordered commit protocol
For a local Linux-style filesystem, a typical durable replacement starts by creating a uniquely named temporary file in the same directory as the destination. Write the complete new representation there, checking every write and close result; then call fsync() on the temporary file. Once its contents and file metadata are synchronized, rename the temporary name over the destination. Finally, open and fsync() the containing directory so the rename is recorded durably. The order matters: publishing the name before the content flush leaves the name able to refer to incomplete data after a crash. [1] [2]
Imagine replacing settings.json after changing a service limit. A reader opening it during the rename sees the previous or new complete file, rather than half a JSON document. Completing both syncs strengthens crash survival when the filesystem and device honor them. Keeping the temporary file in the same directory avoids a cross-filesystem move, which cannot generally use one rename.
A failed flush creates uncertainty, not a clean rollback
The sequence has failure points. If writing or syncing the temporary file fails, do not publish it as though it were complete. For ordinary local Linux semantics, the manual says a failed rename leaves an existing destination in place; it separately warns that an NFS failure response can follow a rename that actually happened. If rename succeeds but directory fsync() fails, the new file may already be visible, yet durability is unestablished. It cannot safely pretend that the old version is still active or blindly retry the whole operation without checking what is now named at the destination. The result is an uncertain commit that recovery code should inspect and resolve. [2] [1]
A successful synchronization is not an unconditional promise against every failure mode. The storage stack must honor flushes; the Linux manual notes historical implementations and devices that failed to flush caches. Filesystems, network mounts, virtual disks and controllers have their own contracts. State the platform assumptions behind a guarantee, and keep recovery data when losing the last committed version is unacceptable.
Multi-step transactions need more than a rename
Atomic replacement works well when one file is the whole unit of state and readers can tolerate seeing either version. It does not atomically update a configuration file plus a separate index, nor does it coordinate concurrent writers by itself. A reader that opens two files on opposite sides of the rename can observe a mixed pair. Those requirements call for a versioned directory, a journal, a database transaction, or another protocol that defines a consistent generation and serializes writers.
SQLite’s atomic-commit documentation makes the difference concrete. In rollback-journal mode, SQLite coordinates locks, journal contents, database writes and flushes; deleting the journal is the commit point, and recovery uses the remaining journal to undo an interrupted transaction. Its WAL mode uses a different mechanism. Even a mature database relies on ordering and documented storage assumptions rather than treating one rename as a universal transaction primitive. [3]
When reviewing a file update, ask three separate questions: what can a concurrent reader observe, what does the program promise after a process crash, and what does it promise after the operating system or storage loses power? Atomic rename helps answer the first. File and directory synchronization address parts of the third. Journaling or a transactional store is needed when the state change spans multiple objects or requires recovery. Naming the guarantee precisely is a small design step that prevents “atomic write” from promising more than the implementation actually provides.