Linux Internals
Dirty Pages and Writeback Tuning in Linux
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيWhen a program writes a file on Linux, the data usually does not go to disk right away. It lands in memory first, and the kernel writes it out later. Those modified-but-not-yet-saved memory pages are called dirty pages. This design makes writes fast, but when too much dirty data piles up, applications can suddenly freeze while the kernel flushes it. Knowing how writeback works lets you avoid those stalls.
What a dirty page is
Linux keeps recently used file data in the page cache (see our page cache guide). When you call write(), the kernel copies your data into a page in that cache and marks the page dirty. The call returns immediately, long before the disk is touched.
Later, kernel flusher threads (shown as kworker/u*:*-flush or similar in process lists) perform writeback: they send dirty pages to storage and mark them clean. Data reaches disk when:
- a page has been dirty longer than the expiry time;
- the amount of dirty memory crosses a background threshold;
- an application calls
fsync(),fdatasync()orsync; - memory pressure forces the kernel to reclaim pages.
The risk is simple: dirty data lives only in RAM. A crash or power loss before writeback loses it, which is why databases call fsync() for anything that must survive.
Seeing dirty pages on your server
grep -E 'Dirty|Writeback' /proc/meminfoExample output:
Dirty: 18432 kB
Writeback: 0 kBDirty is data waiting to be written; Writeback is data being written right now. Watch it while copying a large file:
watch -n1 "grep -E 'Dirty|Writeback' /proc/meminfo"You will see Dirty climb, then drain as writeback catches up.
The writeback settings
The main controls live under /proc/sys/vm/ and are managed with sysctl:
| Setting | What it does | Typical default |
|---|---|---|
vm.dirty_background_ratio | % of available memory at which background writeback starts | 10 |
vm.dirty_ratio | % at which processes that write are throttled and must help flush | 20 |
vm.dirty_background_bytes | Same as background ratio, as an absolute size | 0 (unused) |
vm.dirty_bytes | Same as dirty ratio, as an absolute size | 0 (unused) |
vm.dirty_expire_centisecs | Age after which dirty data is eligible for writeback | 3000 (30 s) |
vm.dirty_writeback_centisecs | How often flusher threads wake up | 500 (5 s) |
The _bytes and _ratio versions are alternatives: setting one resets the other to zero. Check your current values:
sysctl vm.dirty_ratio vm.dirty_background_ratio vm.dirty_expire_centisecsNote that "available memory" here means roughly free memory plus reclaimable page cache, not total RAM.
Why defaults can cause stalls
Percentages made sense when servers had little RAM. On a VPS with 16 GB, 20% allows several gigabytes of dirty data. If your disk writes, say, a few hundred megabytes per second, flushing that backlog takes many seconds. When a writer hits dirty_ratio, the kernel throttles it, and anything that needs to write, including logs and databases, can pause.
Symptoms include periodic latency spikes, high wa in vmstat, and messages like task ... blocked for more than 120 seconds in dmesg.
Tuning writeback safely
The goal for most servers is to start writeback earlier and keep the backlog small, so flushing happens steadily instead of in bursts. Absolute byte values are easier to reason about than percentages:
sudo tee /etc/sysctl.d/90-writeback.conf >/dev/null <<'EOF'
vm.dirty_background_bytes = 134217728
vm.dirty_bytes = 536870912
EOF
sudo sysctl --systemThis starts background writeback at 128 MB and throttles writers at 512 MB. These numbers are a starting point, not a rule. Adjust them to your disk speed: the hard limit should be something your storage can flush in a second or two.
Guidelines:
- Measure first. Record
vmstat 1and latency during your normal write load before changing anything. - Change one thing at a time and compare.
- Do not set limits extremely low. Tiny values force constant small writes and can reduce throughput.
- Do not raise limits to "speed up" writes on servers holding important data. Larger dirty backlogs mean longer stalls and more data at risk on a crash.
- Databases manage their own durability. PostgreSQL and MySQL rely on
fsync(); writeback tuning mainly smooths the background noise around them.
Useful commands
vmstat 1 # watch bo (blocks out) and wa
iostat -x 1 # disk utilization and latency (sysstat)
sync # flush all dirty data now
cat /proc/vmstat | grep -E 'nr_dirty|nr_writeback'Running sync before taking a snapshot or rebooting is a good habit, although the system does it during a clean shutdown anyway.
If you are sizing a server for write-heavy work, RAM and fast NVMe storage both matter. See plans on our pricing page.
FAQ
Is it safe to tune vm.dirty settings on a production server?
Yes, if you change them gradually and measure. The settings take effect immediately with sysctl -w, so test with that first, then persist the values in /etc/sysctl.d/ once you are satisfied.
Does lowering dirty limits protect my data?
It reduces how much unsaved data exists at any moment, but it is not a durability guarantee. Applications that must not lose data need to call fsync(), and you still need backups.
Why does copying a big file finish instantly and then the server slows down?
The copy completed into the page cache as dirty pages. The kernel then writes them out in the background, and that heavy writeback competes with other disk I/O. Lower dirty limits make this smoother.