DEV Community

Cover image for Page cache fine tunning, dirty contents
Ronaldo Lanhellas
Ronaldo Lanhellas

Posted on

Page cache fine tunning, dirty contents

Hi folks, no matter the language you use, if you running under Linux this post is for you.

TLDR; Use dirty properties to set correct Page Cache usage on linux kernel. They are: vm.dirty_background_ratio and vm.dirty_ratio.

In your app, when you call a file write it does not write right away to disk, instead, it is saved on RAM, and this block is called Dirty content. During this "dirty period" you can lose data, just heads up.

When is this dirty content saved to disk? To simplify this post, it depends on two properties most of the time: vm.dirty_background_ratio and vm.dirty_ratio.

  • vm.dirty_background_ratio: A soft limit representing the percentage from total system RAM to trigger the write back thread process. i.e.: 10% from 10GB RAM, meaning when you reach 1GB in page cache, Linux will flush the content to disk in a background thread.
  • vm.dirty_ratio: A hard limit to avoid using more page cache than allowed. Then if you reach this hard limit percentage from total system RAM the content will be flushed to disk right away and might cause some performance degradation.

The recommendation is to use always vm.dirty_background_ratio less than vm.dirty_ratio. SUSE KB suggests dirty_background_ratio to be 1/4 or 1/2 of the dirty_ratio.

In order to apply these to your Linux distro, you should:

  • Change the /etc/sysctl.conf to apply permanently.
  • Or execute the command bellow to a temporary change:
echo 10 > /proc/sys/vm/dirty_ratio
echo 5 > /proc/sys/vm/dirty_background_ratio 

Enter fullscreen mode Exit fullscreen mode

Top comments (0)