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.
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
cachestatand
cachetopreport 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
sysctladjusts kernel parameters at runtime, e.g.,
sudo sysctl -w vm.swappiness=10to reduce swapping.
free -hand
vmstatdisplay memory and I/O usage.
cachestatand
cachetop(from the bcc package) show cache hit/miss data.
4. Practical Tuning Steps
4.1 Check current cache status
Use
free -hand
vmstat 1to observe
buff/cache,
bi,
bovalues.
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_cachesto 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 1wfor CSS/JS,
expires 1Mfor images).
5. Case Study
5.1 Problem
Website slowdown with high
bi/
boand large
buff/cacheusage.
5.2 Diagnosis
Low cache hit rate revealed by
cachestat; frequent temporary file creation caused cache churn; default
vm.swappiness=60increased swapping.
5.3 Solution
Refactor code to reduce temporary files, lower
vm.swappinessto 10, and clear caches with
echo 3 > /proc/sys/vm/drop_cachesduring 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
}Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.