July 29th, 2021

On the interaction between the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

2 comments

Discussion is closed. Login to edit/delete existing comments.

  • aidtopia

    I think some of the confusion stems from the conceptual conflation of "buffering" with "caching." Caching is keeping data in memory in anticipation of future reads--speeding up the reads by avoiding going back out to the slower media. Buffering is about collecting data in blocks that are compatible with the "sector size" of the media.

    Disk drives won't let you read or write four bytes of data at an arbitrary offset. You can only...

    Read more
    • switchdesktopwithfade@hotmail.com

      What is the size of atomicity with a hard drive? I always wondered how journaling was possible.