The write succeeded. Will it survive a crash?

write, fsync and what "saved" means

Reading data back proves nothing about durability. Only a successful fsync, or a WAL flush, makes the storage commit to it.

Being able to read data now is not the same as the storage system having promised to keep it after a crash.

Three stops on the way

| Stop | Where the data may be | Not yet guaranteed | |---|---|---| | Library buffer | process memory | that it reached the kernel | | After a successful write | kernel page cache | that it reached stable storage | | After a successful fsync | storage reported sync done | backups, disk loss, replication |

This is the model for ordinary buffered writes, not every I/O mode. Reading the file back can be served from the cache, so it is no test of surviving a power cut. A successful write() can also write fewer bytes than you asked for.

What fsync adds

fsync(fd) asks for the file's data and metadata to be synced to storage and waits for completion. Check its errors: a write failure can show up only at sync time. And for a newly created file, syncing the file alone does not guarantee the directory entry is durable. Sync the directory too.

Why it matters for an API and for PostgreSQL

Imagine an API that confirms an order and immediately calls a warehouse. If the confirmation is sent before the data is durable, a crash leaves an external action with no matching record.

In PostgreSQL a normal synchronous commit waits for the relevant WAL records to be flushed before reporting success. The WAL is a log that lets the database replay changes; table pages do not all have to be written first. With synchronous_commit=off the success can arrive before the WAL is flushed, so already-acknowledged transactions can vanish in a crash. That is a fair trade for logs you can afford to lose and a poor one for an order that triggers something outside. Do not confuse it with fsync=off, which can corrupt the database after a system crash. Neither is advice to change production settings.

The trade-off

Waiting for storage costs latency. You can group several operations into one sync and acknowledge them all after it: the cost is shared and nothing is acknowledged early. Acknowledging earlier changes the durability contract. It is a decision about what the word "saved" means, not only about speed.

Experiment

flush() empties Python's buffer; fsync() syncs with the OS. If the temp directory is tmpfs, this says nothing about disk durability, and it proves nothing about crash survival either way.

Check yourself

A service wrote a file, read it back fine, returned 200, and never synced. Does that prove survival of a system crash? No, the read may come from the page cache. If only the service process crashed, data already in the kernel survives, but data held only in the process buffer may not. Still no promise about a whole-system crash.

Tagged: storage, durability, fsync, postgresql, linux

Area: Systems and Networking

Related notes

עברית