Operations 24 min read

Master Linux Cache Tuning: Boost Performance with Proven Tools & Techniques

This comprehensive guide explains Linux cache fundamentals, performance metrics, essential tuning tools, step‑by‑step optimization procedures, and a real‑world case study, helping you diagnose low hit rates, adjust kernel parameters, clean caches, and improve overall system responsiveness.

Deepin Linux
Deepin Linux
Deepin Linux
Master Linux Cache Tuning: Boost Performance with Proven Tools & Techniques

1. Understanding Linux Cache

Cache is a high‑speed storage acting as a temporary “transit station” for frequently accessed data, allowing programs to read from memory instead of slower disk, greatly improving access speed.

In Linux, cache (page cache and inode cache) accelerates server response by bridging slow disk I/O and fast CPU.

2. Cache Performance Metrics

Key metrics include cache hit rate, cache size, and update frequency. High hit rate (>90%) indicates efficient caching; low hit rate (<30%) signals problems. Tools such as

cachestat

and

cachetop

report hit statistics.

Cache size comprises file‑system cache and page cache; too large can exhaust memory, too small leads to frequent disk I/O. Update frequency balances data freshness against overhead.

3. Cache Tuning Tools

sysctl

adjusts kernel parameters at runtime, e.g.,

sudo sysctl -w vm.swappiness=10

to reduce swapping.

free -h

and

vmstat

display memory and I/O usage.

cachestat

and

cachetop

(from the bcc package) show cache hit/miss data.

4. Practical Tuning Steps

4.1 Check current cache status

Use

free -h

and

vmstat 1

to observe

buff/cache

,

bi

,

bo

values.

4.2 Adjust kernel parameters

Set

vm.swappiness

(0‑100) lower (10‑30) to keep data in memory; tune

vm.dirty_ratio

(5‑10) and

vm.dirty_background_ratio

(1‑5) to control dirty page flushing.

4.3 Clean caches

Echo 1, 2, or 3 to

/proc/sys/vm/drop_caches

to drop page cache, dentries/inodes, or all caches after running

sync

.

4.4 Optimize applications

Read files in batches instead of line‑by‑line, cache computed results, and set appropriate HTTP cache headers (e.g.,

expires 1w

for CSS/JS,

expires 1M

for images).

5. Case Study

5.1 Problem

Website slowdown with high

bi

/

bo

and large

buff/cache

usage.

5.2 Diagnosis

Low cache hit rate revealed by

cachestat

; frequent temporary file creation caused cache churn; default

vm.swappiness=60

increased swapping.

5.3 Solution

Refactor code to reduce temporary files, lower

vm.swappiness

to 10, and clear caches with

echo 3 > /proc/sys/vm/drop_caches

during low‑traffic periods, resulting in higher hit rates and reduced I/O.

# 逐行读取
with open('large_file.txt', 'r') as f:
    while True:
        line = f.readline()
        if not line:
            break
        # 处理每一行数据

# 批量读取
with open('large_file.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        # 处理每一行数据
# 缓存字典
fib_cache = {}
def fib(n):
    if n in fib_cache:
        return fib_cache[n]
    if n <= 1:
        result = n
    else:
        result = fib(n - 1) + fib(n - 2)
    fib_cache[n] = result
    return result
# Nginx cache for CSS/JS
location ~ \.(css|js)$ {
    expires 1w;
    # other config
}
# Nginx cache for images
location ~ \.(jpg|jpeg|png|gif)$ {
    expires 1M;
    # other config
}
PerformanceOptimizationcachelinuxMemorySysctl
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.