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.
๐ฉ 1. The Illusion of Infinite RAM (Virtual Memory)
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.
๐ 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.
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.
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.
๐ง 4. The Working Set Model
To mathematically prevent thrashing, OS engineers developed the Working Set Model.
The golden rule of system stability:If Sum(All Process Working Sets) > Total Physical RAM = Thrashing Begins
๐ ๏ธ 5. Preventing Thrashing in Production
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.
limits:
memory: "512Mi"
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.
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.
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
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?
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?
Full Deep-Dive Locked
This production architecture deep-dive, interactive canvas labs, and 5-language code templates require an active Pro Pass ($1/mo or $49 lifetime).