TL;DR
Prometheus doesn't just write numbers to a hard drive. In this post, we map the exact journey of a metric—from a temporary whiteboard in Django, into Prometheus's RAM (squeezed by Gorilla compression as it arrives), backed up by a Write-Ahead Log, and finally sealed into blocks on disk. If you want to know why Prometheus is so fast, it's all about memory management.
If you followed along in Part 1, you know that Prometheus acts like a health inspector. It walks over to your application every 15 seconds, scrapes the numbers, and walks away.
But what actually happens to that number once Prometheus grabs it?
If you assume Prometheus just opens a file on your server's hard drive and writes the number down like a standard database, you are drastically underestimating the engineering behind this tool. To process thousands (or in large deployments, millions) of data points a second, Prometheus has to play a dangerous game with your server's RAM.
1. The Public Whiteboard vs. The Private Brain
Let's start at your application. If you have a Django app generating metrics using a library like prometheus_client, the numbers are often exposed via an in-memory registry. In production setups with multiple worker processes, teams sometimes write these to a tmpfsA trick used by Linux to treat a chunk of RAM as if it were a normal folder on your hard drive. Extremely fast, but everything in it vanishes when the server restarts. directory—a chunk of RAM that Linux treats as a normal folder.
Think of tmpfs as a public whiteboard that exists entirely in the server's RAM.
Django runs up to the whiteboard, writes "Current CPU: 45%", and walks away. 15 seconds later, Prometheus knocks on the /metrics door. Prometheus never walks into the room itself: the client library inside Django reads the whiteboard and hands the numbers over HTTP, and Prometheus copies them into its own private brain (its Heap MemoryThe portion of a computer's RAM that an application (like Prometheus) explicitly claims for itself to hold active data while it is running.).
Why not just organize everything directly on the hard drive? Because disks (even SSDs) are far slower than RAM, especially for small, scattered writes. Prometheus tracks thousands of separate series, and each scrape adds a new sample to every one of them. Updating thousands of compressed structures on disk, every 15 seconds, would quickly make the disk the bottleneck.
By keeping the active numbers in RAM, Prometheus gets very fast reads and writes. But RAM is volatile. If the server loses power, the brain is wiped clean.
2. The Active Chunk and the WAL (Safety vs. Speed)
To balance this extreme speed with safety, Prometheus uses a two-part system.
Inside its RAM, Prometheus keeps the most recent data in a structure called the Head BlockThe in-memory part of the Prometheus database that holds the most recent samples (roughly the last 2–3 hours) before they are written to disk as a permanent block.. Every series (every unique metric and label combination) gets its own small, compressed ChunkA small compressed container holding up to about 120 samples of a single series. At a 15-second scrape interval, one chunk covers about 30 minutes. inside the Head, which it keeps appending new samples to.
Every time a new number arrives, Prometheus appends it to that series' chunk in RAM, but it also quickly jots down a backup copy on the physical hard drive. This backup is called the Write-Ahead Log (WAL)A sequential, append-only log file on disk (Snappy-compressed by default). It acts as an emergency restore point so in-memory data can be rebuilt if Prometheus crashes..
Wait, didn't we just say disks are too slow? The trick is how the WAL writes. It never goes back to update anything. It simply appends records to the end of a file, one after another, in batches. Sequential appends like this are the one kind of disk write that is cheap.
Think of the WAL as an emergency safety net. Queries never read from it. But if someone trips over the server's power cord and the RAM is wiped out, the WAL saves the day. When the server reboots, Prometheus replays the WAL and rebuilds its in-memory Head almost exactly as it was before the crash.
Every couple of hours, once the Head holds enough data, Prometheus cuts off the oldest 2-hour window and writes it to disk as a permanent, immutable block. It then truncates the part of the WAL that block covered, and the cycle continues. Later, a background compaction step merges these small 2-hour blocks into bigger ones.
3. The Magic of Gorilla Compression
So how does Prometheus fit all of this in RAM in the first place? The samples in those chunks are never stored as plain numbers. As each sample arrives, Prometheus encodes it with a clever scheme from Facebook's Gorilla paper, known as Gorilla CompressionAn algorithm optimized for time-series data. It uses Delta-of-Deltas for timestamps and XOR compression for values, achieving incredible compression ratios..
Gorilla uses two tricks: Delta-of-Deltas for timestamps (instead of storing full Unix timestamps, it stores tiny differences between consecutive timestamps) and XOR compression for values.
Here's why XOR works so well: imagine a metric tracking how many servers you have online. The data points over a minute look like this: [5, 5, 5, 5, 5].
When you XOR a number with itself (5 XOR 5), you get zero. Zeros compress extremely well: a repeated value is stored as a single bit. Because consecutive metrics rarely change dramatically, XOR-ing a value with its predecessor often produces mostly zeros.
Because of this, Prometheus shrinks each data point down to about 1 to 2 bytes, in RAM and on disk alike. (Each sample is a timestamp plus a 64-bit float, so that's 16 bytes uncompressed.)
4. Why Duplicates are Proof of Time
One of the most confusing things for beginners is looking at raw Prometheus data and seeing the same numbers repeated over and over. If my website has had exactly 1,000 visitors for the last hour, why does Prometheus keep scraping and storing 1000 every 15 seconds? Why not just scrape it once?
Because in time-series databases, the duplicates are the math. And thanks to Gorilla, they cost almost nothing: a repeated value takes about one bit. That is the golden rule of Prometheus: storage compresses, queries calculate. Store everything cheaply, and do the thinking at query time.
When a Grafana panel uses a function like rate(), Grafana sends the query to Prometheus, and Prometheus does the math: it compares samples inside a time window to work out how fast the number is changing. That needs at least two samples in every window. A flat line of 1000, 1000, 1000 is how Prometheus proves the rate is zero, instead of guessing.
There's a second reason. If Prometheus gets no fresh sample for a series for 5 minutes, it treats the series as stale, and it disappears from your graphs. Without the duplicates, Prometheus couldn't tell whether your website still has 1,000 visitors right now or whether the app died an hour ago. The constant, repetitive scraping is the heartbeat that proves your application is still alive and reporting.
Counters vs. Gauges
rate() is meant for CountersA metric that only goes up (like total requests served). Use rate() to calculate how fast it's increasing. Resets to zero when the application restarts., metrics that only go up (like total visitors), and it gives you the per-second rate of increase. GaugesA metric that can go up or down (like current CPU usage or temperature). Read it directly without rate(). can go up or down (like current CPU usage) and are read directly, without rate().
Up Next in the Series:
In our next post, we will look at what happens when you ask Prometheus to calculate too much at once, and how a single bad PromQL query can accidentally crash your entire server.
Part 3: The "Query of Death" and PromQL Defenses