SystemDesign.website
Track 1 ยท Module 1.8 ยท OS Fundamentals

Thrashing & Memory Bottlenecks

Why does a server suddenly grind to a halt even when CPU usage drops to 5%? Understand virtual memory, page faults, the working set model, and how to prevent catastrophic "Death Spirals" in memory allocation.

โฑ๏ธ Reading Time: ~20 min
๐ŸŽฏ Core Skill: Memory Troubleshooting
๐ŸŽฌ Format: Interactive Diagrams

๐ŸŽฉ 1. The Illusion of Infinite RAM (Virtual Memory)

๐Ÿ“š The Library Analogy

Imagine a library with only 100 physical shelves (RAM). However, the library catalog tells readers there are 10,000 books available (Virtual Memory). Most of these books are actually stored in a massive, slow warehouse down the street (Hard Drive / SSD).

When a reader asks for a book, the librarian silently drives to the warehouse, fetches the book, and puts it on one of the 100 fast shelves so the reader can read it instantly. The reader never knows the warehouse exists.

In computers, this is called Paging. Memory is divided into fixed chunks called Pages (usually 4KB). The OS maintains an illusion for every process that it has massive, contiguous memory, while behind the scenes, the OS is furiously swapping pages between physical RAM chips and the disk.

Process Virtual MemoryPage 1 (Code)Page 2 (Heap)Page 3 (Data)Page 4 (Stack)OS Page TableMapping DirectoryPhysical RAM (Fast)Frame (Page 1)Frame (Page 4)Disk Swap (SLOW)Page 2Page 3

๐Ÿ›‘ 2. Page Faults (The Speed Bump)

A Page Fault occurs when the CPU tries to read a virtual memory address, checks the Page Table, and realizes the required Page is not mapped in physical RAM. The hardware immediately pauses the thread and triggers an interrupt to the OS.

๐ŸŸข Minor Page Fault (Fast)

The page actually exists in RAM, but the OS hasn't wired up the Page Table for this specific process yet (e.g., shared libraries like libc.so).

Latency: ~1 to 5 microseconds. Barely noticeable.

๐Ÿ”ด Major Page Fault (Catastrophic)

The page was evicted (swapped) to Disk. The OS must halt your thread, issue a disk read command, wait for the SSD/HDD to spin up, copy the 4KB block into RAM, update the table, and resume.

Latency: ~10,000 microseconds (10ms). 10,000x slower!

๐Ÿ’€ 3. Thrashing: The Death Spiral

In system design, Thrashing is the phenomenon where a computer spends more time paging (moving data to and from disk) than executing actual application code.

๐Ÿ”ฅ The Thrashing Anatomy
1. Program A needs Page 1. OS evicts Page 2 to disk to make room.
2. One millisecond later, Program B needs Page 2.
3. OS halts Program B, evicts Page 1 to disk to make room for Page 2.
4. One millisecond later, Program A needs Page 1 again.
Result: The CPU is sitting at 5% utilization, but the system is frozen because the disk is at 100% I/O utilization swapping memory back and forth infinitely.
CPU Utilization vs Concurrent LoadNumber of Concurrent Processes / Memory DemandCPU UtilizationThrashing Threshold (WSS > RAM)Healthy ScalingCPU doing real workSystem CollapseCPU idle, waiting on Disk

๐Ÿง  4. The Working Set Model

To mathematically prevent thrashing, OS engineers developed the Working Set Model.

Definition:The Working Set (WSS) is the subset of memory pages a program is actively touching in a recent time window. Even if a Java app allocates 4GB of RAM, if it only loops over a 50MB array for 10 minutes, its Working Set is 50MB.

The golden rule of system stability:
If Sum(All Process Working Sets) > Total Physical RAM = Thrashing Begins

๐Ÿ› ๏ธ 5. Preventing Thrashing in Production

1. cgroup Memory Limits (Docker/K8s)

Never let a process scale unbounded. In Kubernetes, you set strict RAM limits. If a pod exceeds it, the kernel fires the OOMKiller (Out-Of-Memory Killer) to violently terminate the pod before it causes node-level thrashing.

resources:
  limits:
    memory: "512Mi"
2. vm.swappiness (Linux Kernel)

By default, Linux eagerly swaps idle memory to disk. For databases (like ElasticSearch, MongoDB), you turn off swap entirely or set vm.swappiness=1. It is better for a database query to fail outright than for the database to invisibly slow down 10,000x due to paging.

3. Load Shedding & Backpressure

If your web server queue grows too large, the working set of "in-flight requests" will exceed RAM. An API Gateway should Drop Requests (HTTP 503) early. It is better to serve 80% of users in 100ms than to accept 100% of users and serve them all in 30 seconds due to thrashing.

4. Connection Pooling

As seen in Module 1.7, traditional architectures allocate a 1MB thread stack per connection. Capping maximum concurrent DB connections (e.g. HikariCP max-pool-size=50) strictly bounds the maximum memory required for stacks.

๐ŸŽฏ 6. Test Your Understanding

Scenario 1: The Idle CPU Mystery

Your PostgreSQL database server has 32GB RAM. After deploying a feature that adds 50 massive new reporting tables, query latency spikes from 5ms to 2 seconds. You check the server metrics and see CPU usage has plummeted to 2%, but Disk I/O Wait is at 98%. What is the diagnosis?

Scenario 2: The Redis Cache Churn

Your Redis cache holds 5GB of data. During a viral marketing event, traffic spikes 10x and users browse 10x more unique product pages. Suddenly, your cache hit rate crashes from 95% to 10%, causing your primary DB to overload. Redis memory usage is pinned at exactly 5GB. What happened?

โ—‹

Completed this module?

Mark as finished to update your curriculum progress tracker.