AlgoViz

Page cache, writeback and reclaim

medium 7 min

A buffered write initially modifies a cached page.

Video lesson · Chapter 1 of 8

Fast writes followed by a long stall

0:00 / 5:48

Theory

Read or listen first. About 2 min.

What it is

A buffered write initially modifies a cached page. The page stays dirty until the kernel writes it to backing storage. A fast producer can therefore appear faster than the device for a while. As dirty data accumulates the kernel starts writeback and may throttle writers. A successful buffered write alone does not establish persistence after a crash.

Why it exists

Import jobs run normally for the first minute, then every few seconds a write blocks. Compare dirty and writeback bytes, device throughput and the time workers spend stalled. Clean file cache can often be reclaimed cheaply. Dirty pages need writeback, and anonymous pages have different reclaim options. Clearing caches can discard precisely the hot data the workload needs.

A flat dirty backlog with isolated long sync calls can indicate device flush latency rather than excessive buffered production.

Fast writes followed by a long stall

Writer → Dirty page cache (buffer changes). Dirty page cache → Backing device (write back)
Writer → Dirty page cache (buffer changes). Dirty page cache → Backing device (write back)

Inside this chapter

The Linux Page Cache architecture
The page cache holds file-backed pages in RAM. A second read may avoid device I/O entirely. A cold benchmark and a warmed benchmark measure different paths.
Dirty Pages and Writeback mechanics
A dirty page contains changes that have not reached backing storage. Background writeback drains these pages. Sustained dirtying faster than draining eventually feeds back into writer latency.
Swappiness configuration
Linux swappiness expresses a relative reclaim cost preference for swapping versus filesystem paging. The supported range extends to 200. Interpret it together with workload and swap-device behavior.
Major vs. Minor Page Faults
A minor fault can be resolved without fetching data from backing storage. A major fault requires I/O. Count them per process and compare with observed storage latency.
Memory mapped files (mmap)
A file mapping establishes an address range. Accessing an unmapped-in page can fault later. Shared writable mappings also need a defined persistence and synchronization contract.
Readahead and drop_caches
Readahead speculatively fetches subsequent file pages. Clearing caches on a shared production host distorts workload behavior. Use an isolated cold-start experiment when cache effects matter.
Memory Reclaim and kswapd
Background reclaim attempts to maintain available memory. Under stronger pressure an allocating task can reclaim synchronously and lose execution time on the request path.

How it works

Step through the idea, or press Play.

Step 1 of 4

Modify a page. A buffered write changes a cached file page and marks it dirty.

Understand it

Worked incident

Fast writes followed by a long stall

ObservationDirty data (MiB)Interpretation
Baseline32The storage device keeps pace with dirtying.
Incident480The importer generates dirty pages faster than storage drains them.
After intervention56Pace the importer and reserve I/O capacity. Confirm that stalls and dirty backlog fall together.
Illustrative measurements from the same workload at three checkpoints.

Choose the next investigation

A flat dirty backlog with isolated long sync calls can indicate device flush latency rather than excessive buffered production.

Summary

The whole topic in one minute.

RAM temporarily absorbed the excess. Sustained completion needs the device to drain pending pages at the rate the writer creates them.

A flat dirty backlog with isolated long sync calls can indicate device flush latency rather than excessive buffered production.

Sources