# The Hunger of Resources (and the Speed of the Elephant) Every database is a hungry beast. Before it can perform the magnificent operations we ask of it, Postgres needs to eat. It consumes **Disk IO** to remember things, eats up **Memory** to think fast, burns **CPU** cycles to calculate, and uses the **Network** to shout its answers back to you. But to truly understand why the Lazy Elephant is so obsessed with his Snack Pile ([[Memory]]) and so terrified of the Filing Cabinet ([[Disk IO]]), we have to talk about time. In the digital world, time moves so fast that it’s hard for a large, slow-moving mammal to comprehend. To help the elephant understand why he should be lazy, we’ve translated the standard "Latency Numbers Every Programmer Should Know" into a scale that humans (and elephants) can actually feel. ## The Elephant's Latency Menu If we imagine that **one CPU cycle (0.3 ns) is equal to one second**, then the world of Postgres looks like this: | Action | Digital Time | Elephant Time | Metaphor | | :--- | :--- | :--- | :--- | | **L1 Cache Reference** | 0.5 ns | **1.5 seconds** | A quick blink of the eye. | | **Branch Mispredict** | 5 ns | **15 seconds** | Realizing you walked into the wrong room and turning around. | | **L2 Cache Reference** | 7 ns | **21 seconds** | Checking your pocket for a snack. | | **Main Memory (RAM)** | 100 ns | **5 minutes** | Walking to the kitchen to boil a kettle. | | **SSD Random Read** | 150,000 ns | **1.7 days** | A weekend road trip to another city just to buy a single postage stamp. | | **Network (Same DC)** | 500,000 ns | **5.7 days** | Waiting a full week for a carrier pigeon to return with a "Yes" or "No". | | **HDD Seek (Disk)** | 10,000,000 ns | **4 months** | A grueling, season-long expedition into a frozen basement to find one folder. | | **Packet (CA to NYC)** | 40,000,000 ns | **1.2 years** | Moving to a different state and waiting for your mail to be forwarded. | | **Packet (CA to NL)** | 150,000,000 ns | **4.7 years** | Enrolling in university and earning a full degree before the data arrives. | ## The Lesson of the Desk When you look at this list, the elephant’s "laziness" starts to look like extreme wisdom. If the data is in the **L1 Cache**, it’s like a thought currently in the elephant's head. If it's in **Main Memory**, it’s a snack sitting right there on his desk. He can reach it in the time it takes to brew a cup of tea (this is why we set `work_mem` and `shared_buffers`). But the moment the elephant has to touch the **Disk**, he is committing to a 4-month-long journey. This is why we have [[Index|Indexes]] (cheat sheets), [[WAL|Write-Ahead Logs]] (pocket diaries), and [[Chapter 6 - The Hunger of Resources#The alternative is Disk IO|Shared Buffers]]. Doing the least work possible isn't just a lifestyle choice—it's the only way to survive when every mistake costs you a semester of college. --- ### Resources Deep Dive - [[CPU]] - [[Memory]] - [[Disk IO]] - [[Network]]