How to Disable Disk Write Caching in Ubuntu To Prevent Data Loss

Last updated: January 23, 2014

Ubuntu Prevent Data Loss
 
This simple tutorial is going to show you how to disable disk write caching in Ubuntu to prevent data loss when you may experience power failure.

Enable write caching improves disk performance, but a power outage or equipment failure might result in data loss or corruption. It’s recommended only for disks with a backup power supply.

Some third-party programs require disk write caching to be enabled or disabled. If your disk are used for Event Store databases, it’s highly recommended to disable disk caching to help ensure that data is durable when the machine might experience a power, device or system failure.

In Ubuntu, it’s easy to check out whether disk caching is enabled on your disk or not by running below command:

sudo hdparm -i /dev/sda

Replace /dev/sda with your device and you’ll see below similar outputs:

Model=WDC WD3200BPVT-22JJ5T0, FwRev=01.01A01, SerialNo=WD-WX61EC1KZK99
Config={ HardSect NotMFM HdSw>15uSec SpinMotCtl Fixed DTR>5Mbs FmtGapReq }
RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=50
BuffType=unknown, BuffSize=8192kB, MaxMultSect=16, MultSect=off
CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=625142448
IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio3 pio4
DMA modes: mdma0 mdma1 mdma2
UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6
AdvancedPM=yes: unknown setting WriteCache=enabled
Drive conforms to: Unspecified: ATA/ATAPI-1,2,3,4,5,6,7

* signifies the current active mode

The words in red, WriteCache=enabled, means caching is enabled! To disable it, edit the “/etc/hdparm.conf” with your favorite editor, here I use vi as example:

sudo vi /etc/hdparm.conf

Uncomment the line “#write_cache = off” (without quotes) by removing the # at its beginning. So it looks like:

# -W Disable/enable the IDE drive’s write-caching feature
write_cache = off

After that, restart your computer and check out the write caching status again to make sure it’s disabled.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via ubuntuhandbook1@gmail.com Buy me a coffee: Buymeacoffee | https://ko-fi.com/ubuntuhandbook1 |

6 responses to How to Disable Disk Write Caching in Ubuntu To Prevent Data Loss

  1. Hi Ji m,
    Interesting article ! I’m right now on one my Linux distros “LMDE 201303” and I just checked on my partition LMDE thanks to : sudo hdparm -i /dev/sda2 and I found this line : # -W Disable/enable the IDE drive’s write-caching feature #write_cache = off.
    Looks like the job’s already done on that distro. I’ll check that on my Ubuntu 12.04. Thanks a lot anyway !

  2. If you do screen recordings disabling write cache will cause stuttering and recording games will look like crap because most screen recorders cache while they are recording, if it’s disabled then it saves the data directly to the hard disk which causes stuttering.

    I’ve had numerous power outages thanks to a neighbour of mine, it happens when I least expect it and my system has even been under full load when its happened and i’ve yet to see any data loss with write cache enabled. Disabling it seems silly as you throw away performance gains on the off chance.

    • You’re right @steve. For home use machine, that’s not a big deal. So I said “If your disk are used for Event Store databases, it’s highly recommended to disable …”

      Many databases include postgres will give you a warning about the possible dire consequences of having write caching switched on when you may experience power failure.

  3. Every well written application does call fsync() when a critical write has been written. Furthermore state-of-the-art filesystems like ZFS do use a log-device for application tripped fsync() calls on a fast device like a ssd, now the atomic writes are written to disk, every atomic change to the disk is ensured by a fsync call, which is known by “write barrier”.

    In a short way: the write-barrier ensures that all data is written in a linear manner, which ensures that every 30 seconds (at default) the filesystem is in a clean state were all data and metadata is flushed from the drive’s write cache to disk. If the system crashed the last written data might be lost. The zfs is recovering the last written data from the last write barrier and everything which looks well written is used, everything else is discarded.