A file write is the path data takes from your program to persistent storage: the application issues a write, the kernel copies the bytes into page cache, marks the pages dirty, and a background flusher later sends them to the disk. The detail most people get wrong is that a successful write call usually means the data reached memory, not the platter or flash cells. Durability only arrives with an explicit sync, or when the kernel's dirty-page limits force the data out.
That distinction shapes everything else: what fsync costs, why a power loss can lose recent writes, and why buffered and direct I/O behave so differently.
What Happens Inside a Write Call?
On a typical Unix-like system, the write call is short. The kernel checks that the file descriptor is open for writing, finds the file's inode, and copies the requested bytes from your process's buffer into the page cache. Those pages are tagged dirty. The call returns the number of bytes accepted, which can be fewer than you asked for. A short write is normal and your code should loop until every byte is handed over.
On Windows the equivalent path goes through the file system driver and the system cache, but the shape is the same: user buffer to cache, cache to device later. On any operating system, the write call does not wait for the disk unless the file was opened in a synchronous mode or the cache is under pressure.
Buffered Versus Direct I/O
Buffered I/O is the default. It lets the kernel merge small writes, reorder them, and read them back from memory without touching the device. Direct I/O bypasses the page cache and moves data straight between your buffer and the storage device, usually with alignment requirements on the buffer address, file offset and length, often 512 bytes or 4 KB. Direct I/O is used by databases and backup tools that manage their own caching. It trades the kernel's readahead and write merging for predictable memory use.
When Does a Write Actually Reach the Disk?
Dirty pages sit in memory until one of three things happens. The kernel's periodic flusher wakes up and writes pages older than a threshold. The amount of dirty memory crosses a limit, forcing writeback. Or your program calls fsync or fdatasync, which blocks until the file's data and the metadata needed to retrieve it are on stable storage. A directory fsync is needed after creating, renaming or deleting a file if you need the name change to survive a crash.
Timing is governed by kernel tunables. On Linux, dirty_background_ratio or dirty_background_bytes sets when background writeback starts, and dirty_ratio or dirty_bytes sets the hard limit where writers are throttled. The flusher typically runs every few seconds; the exact interval is set by dirty_writeback_centisecs, in hundredths of a second. These are tunables, not fixed facts, and they differ by system and workload.
| Operation | What it guarantees | Typical cost |
|---|---|---|
| write | Bytes copied to page cache | Microseconds |
| fsync | File data and needed metadata on stable storage | Milliseconds to tens of milliseconds |
| fdatasync | File data, metadata only if needed to read it back | Slightly less than fsync |
| O_SYNC write | Each write durable before return | One device round trip per write |
Why Do Writes Get Lost After a Power Cut?
Because the write call returned before the data left memory. The page cache is volatile. A crash or power loss discards dirty pages, and the file may contain old data, a mix of old and new blocks, or a size that does not match the bytes written. Journaling file systems protect the file system's own consistency, not your application's data. The file system will mount cleanly, but the last few seconds of writes can be missing.
The fix is ordering. Write the data, call fsync on the file, then update any pointer or rename that makes the new data visible, and fsync the directory. That sequence is what makes a crash-safe replace: write to a temporary file, sync it, rename it over the target, sync the directory. Without the syncs, the rename can land before the data, leaving a correctly named file with empty or partial contents.
How Large Is a Write, and How Many Does It Take?
Storage devices work in blocks, commonly 512 bytes or 4 KB logical sectors, and the file system allocates in clusters or extents, often 4 KB. A 1 MB write is about 256 pages of 4 KB. If your program writes 100 bytes at a time, the kernel still dirties whole pages, so 1 MB spread over 10,000 small writes touches the same 256 pages but pays the system call overhead 10,000 times. Buffering in user space and writing in chunks of 64 KB to 1 MB usually reduces overhead without hurting durability, because the sync still happens once at the end.
Alignment matters for direct I/O and for some devices. A buffer that is not aligned to the device's block size can cause the request to be split or rejected. Check the device's logical and physical block sizes before choosing an alignment.
What Slows a Write Down?
The device is often the bottleneck, but not always. A sync write on a spinning disk costs a seek plus rotational latency, typically several milliseconds. On flash storage, a sync write costs a program/erase cycle or a flush of the device's write cache, also milliseconds. Without sync, the cost is memory bandwidth and system call overhead, which is far smaller.
Other limits include the file system's journal, which serialises metadata updates; write barriers, which order writes for crash safety; and the dirty-page throttling that pauses writers when too much memory is dirty. On network file systems, a sync write may involve a round trip to the server, so latency depends on the network, not the local disk.
Checking Whether a Write Is Durable
You cannot tell from the return value of write alone. Use fsync or fdatasync and check the return value; a failed sync means the data may not be on stable storage. Tools such as iostat, vmstat and the file system's own statistics show writeback activity and dirty page counts. For a quick test, write a file, sync it, then drop caches and read it back; if the contents match, the write reached the device.
What About Metadata and File Size?
A write that extends a file also updates the inode's size and block map. That metadata is part of the file's state, so fsync must flush it too; fdatasync may skip metadata that is not needed to retrieve the data, such as access time. If your application relies on the file size after a crash, use fsync rather than fdatasync, or sync the directory as well.
Truncating a file, renaming it, or unlinking it changes directory entries, which live in the directory's own pages. Those pages need their own sync. A common pattern is to sync the file, then sync the directory, then treat the operation as complete.