On the interaction between the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags

Raymond Chen

The Create­File function has two related flags: FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH. These flags can be set independently, but they sort of work together.

First, let’s look at FILE_FLAG_NO_BUFFERING. Setting this flag prevents operations on the handle from going through the disk cache. Reads come directly from the disk, and writes go directly to the disk.

The FILE_FLAG_WRITE_THROUGH flag increases the urgency with which write requests are sent to the disk. Setting this flag forces writes to go to the disk immediately, and combining this flag with FILE_FLAG_NO_BUFFERING adds the additional urgency of telling the disk controller to flush the data out of its internal cache.

So let’s fill out a table.

  NO_BUFFERING
Clear Set
WRITE_THROUGH Clear Writes go into cache
Lazily written to disk
No hardware flush
Writes bypass cache
Immediately written to disk
No hardware flush
Set Writes go into cache
Immediately written to disk
Hardware flush
Writes bypass cache
Immediately written to disk
Hardware flush

Bonus reading: We’re currently using FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH, but we would like our WriteFile to go even faster.