Reduce disk I/O for small reads using memory

Many applications that are used on a standard server perform quite a few of small writes to the disk (like MySQL or Apache). These writes can pile up and limit the performance of your applications. If you have kernel 2.6.9 or later, you can adjust how these small writes are handled to allow for better performance.

There’s two main kernel variables to know:

vm.dirty_ratio – The highest % of your memory that can be used to hold dirty data. If you set this to a low value, the kernel will flush small writes to the disk more often. Higher values allow the small writes to stack up in memory. They’ll go to the disk in bigger chunks.

vm.dirty_background_ratio – The lowest % of your memory where pdflush is told to stop when it is writing dirty data. You’ll want to keep this set as low as possible.

These might confuse you. In short, when your memory begins filling with little pieces of data that needs to be written to the disk, it will keep filling until it reaches the dirty_ratio. At that point, pdflush will start up, and it will write data until it reduces the dirty data to the value set by dirty_background_ratio.

Stock 2.6.9 kernels have a dirty_background_ratio of 10% and a dirty_ratio of 40%. Some distributions tweak these defaults to something different, so you may want to review the settings on your system. On a system with heavy disk I/O, you can increase the dirty_ratio and reduce the dirty_background_ratio. A little experimentation may be necessary to find the perfect setting for your server.

If you want to play with the variables, just use your standard echo:

echo 5 > /proc/sys/vm/dirty_background_ratio
echo 60 > /proc/sys/vm/dirty_ratio

Once you’ve found the right setting, you can set it permanently by adding lines to your /etc/sysctl.conf:

vm.dirty_background_ratio = 5
vm.dirty_ratio = 60

If you have a reliable server with a good RAID card and power supply, you could set the dirty_ratio to 100 and the dirty_background_ratio to 1. This was recommended by a buddy of mine who runs quite a few servers running virtual machines.


  1. says

    Thanks for this useful post. Do you have any experience with kernel 2.6 and Citrix XenServer? Myself and a many more people on the Citrix XenServer forum have problems with Ubuntu 10.04 (2.6.32-23-server #37-Ubuntu SMP) and there is no idea to have a right host (dom0) configuration. If you have an idea please contact me.

  2. says

    Ramon -

    I know that Ubuntu 10.04 uses grub 2, but XenServer 5.5 doesn’t support grub 2. There are a few guides on there about some workarounds to get it working.

  3. says

    iirc the dirty_background_ratio triggers the start of the [flush], not its end.

    I would add that:
    – when you hit dirty_ratio, writes start to be blocking on all processes until dirty_ratio falls under the threshold ;)

    Setting dirty_ratio = 100 could saturate of your write capacity. Instead you *may* want your application to block and notify as soon as possible that something is going bad. The desired behaviour mainly depends on the workload of your app anyway.



Leave a Reply

Your email address will not be published. Required fields are marked *