Get/Set Write-Through/Write-Back Setting for Your HDD

23 Nov 2015

Gregory Smith's awesome PostgreSQL 9.0 High Performance does a good job of describing how many drives are configured in write-back mode rather than write-through mode. Write-back mode allows the drive to tell the operating system that a write has been successful after that write has gotten into the drive's cache, note after the write has been persisted to the actual disk. Write-through, on the other hand, while slower, gives this guarantee.

For most consumer drives, you will find write-back is the default. Here's how to check.

Get a list of your storage devices:

# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 223.6G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0 207.2G  0 part /
└─sda3   8:3    0  15.9G  0 part [SWAP]

See if write-back is enabled (write-caching is turned on):

# hdparm -W /dev/sda

/dev/sda:
 write-caching =  1 (on)

Yup! Here's how to disable it for guaranteed durability of your writes:

# hdparm -W0 /dev/sda

/dev/sda:
 setting drive write-caching to 0 (off)
 write-caching =  0 (off)