Scrivio Interview library
InternalsFoundations6 min read · updated 13 November 2026

Time-series data is a stream of timestamped values per series, such as CPU per host every 10 seconds. Dedicated time-series stores keep billions of samples in little space because the data has a very regular shape.

How do time-series stores compress samples so well, and how do they organise data on disk by time?

Time-series stores compress well because the data is regular: timestamps arrive at near-fixed intervals, so storing the delta of deltas usually needs a single zero bit, and consecutive values are similar, so XOR with the previous value leaves mostly zero bits. This Gorilla-style encoding brings typical samples from 16 bytes to about 1 to 2. On disk, data is cut into time blocks or chunks so recent data stays in memory and old data is dropped whole, a label index maps series to ids so cardinality drives memory, and downsampled rollups keep long-range queries fast.

Why this question is asked

Monitoring systems store trillions of samples, and a naive layout of a 64-bit timestamp plus a 64-bit float per sample would cost 16 bytes each, far too much to keep in memory or on disk for long. Time-series databases such as Prometheus, VictoriaMetrics, InfluxDB and TimescaleDB get close to one byte per sample instead. Interviewers ask how, because the answer shows real understanding of encodings and of why time-ordered storage makes retention cheap, and because the cardinality point explains many production incidents. In this answer, we will recap what a series is, then walk through timestamp compression, value compression, time blocks and chunks, the label index and cardinality, and downsampling, ending with why compression gets worse for resets and noise.

1A Quick Recap of Time-Series Data

A time series is a sequence of timestamped values for one thing being measured, identified by a metric name and labels, such as cpu_usage with host=web-17 and core=3. A scrape every 10 seconds gives one sample per series per 10 seconds, a pair of a timestamp in milliseconds and a float value. Samples almost always arrive in time order, are almost never updated, and are queried by time range for a set of series. That regular shape is what the storage exploits.

2Compressing Timestamps With Delta of Deltas

Storing each timestamp in full wastes space because they differ by almost the same amount every time. The first timestamp is stored in full, the second as a delta from the first, and every later one as the delta of deltas: how much this interval differs from the previous interval. With a 10-second scrape, the intervals are 10,000 ms, 10,000 ms, 10,001 ms, so the delta of deltas is 0, 0, 1. A zero is encoded as a single 0 bit, and small values use short bit patterns.

YAML
t: 1700000000000 1700000010000 1700000020000 1700000030001
delta: 10000 10000 10001
dod: 0 1
bits: 64 (full) ~14 1 ('0') ~9 (short code for a small value)

3Compressing Values With XOR

Values are floats, and consecutive values of one series are usually close, often identical. Gorilla, the encoding from Facebook's time-series paper that Prometheus also uses, XORs each value with the previous one. Identical values give a XOR of zero, stored as one bit. Similar values share the sign, exponent and leading mantissa bits, so the XOR has many leading zeros and often trailing zeros. The encoder stores only the meaningful bits in the middle, plus their position, and reuses the previous position when it fits.

Together, these encodings bring a typical sample from 16 bytes to roughly 1 to 2 bytes, and a gauge that rarely changes to well under one byte per sample.

Properties
prev = 42.0 -> 0x4045000000000000
cur = 42.0 -> XOR = 0 -> 1 bit
cur = 42.5 -> XOR = 0x0001000000000000 -> few meaningful bits in the middle
// typical result: about 1.3 to 2 bytes per sample instead of 16
Bytes per sample
EncodingTimestampValueTotal
Raw8 bytes8 bytes16 bytes
Delta of deltas plus XOR, regular dataabout 1 bita few bits to 1 byteabout 1 to 2 bytes
Same, steady gaugeabout 1 bitabout 1 bitwell under 1 byte

4Organising Data by Time: Blocks and Chunks

The second idea is that data is cut by time. Prometheus keeps the most recent samples in an in-memory head block, protected by a write-ahead log, and every 2 hours writes them out as an immutable block on disk with its own index. Background compaction merges small blocks into larger ones covering longer ranges. Retention is then trivial: when a block is entirely older than the retention period, its directory is deleted. No row-by-row deletes, no vacuum.

TimescaleDB applies the same idea inside PostgreSQL: a hypertable is split into chunks, child tables each covering a time interval such as one day. Recent chunks are small and hot in memory, queries for a time range touch only the chunks that overlap it, older chunks can be compressed into a columnar form, and retention drops whole chunks.

Step throughPrometheus storage by time
  1. Head block in memorylast 2 hours, WAL for crash safety
  2. 2-hour blocks on diskimmutable, own index
  3. Compacted blocksmerged into longer ranges
  4. Deleted wholewhen older than retention

4 layers, from the top down. Step through them one at a time.

5The Label Index and Why Cardinality Matters

A query asks for series by labels, such as all cpu_usage series with job=api. An inverted index maps each label pair to the list of series ids that have it, like a search engine's postings lists, and intersects them. Samples are then read by series id.

So memory and index size are driven by the number of distinct series, called cardinality, not by the number of samples. One series sampled for a year costs little; a million series each sampled for a minute cost a lot, because each needs index entries and an in-memory head chunk. That is why putting user ids, request ids or full URLs into labels can take down a metrics system: each new value creates a new series.

6Downsampling and Rollups

Raw 10-second samples are needed for recent troubleshooting but not for a chart of the last year. Systems such as Thanos, Mimir, VictoriaMetrics, InfluxDB and TimescaleDB continuous aggregates compute rollups, for example 5-minute or 1-hour aggregates after 30 days, keeping minimum, maximum, sum and count so averages and extremes stay correct. Long-range queries read a few hundred points per series instead of millions, and raw data can be deleted earlier, keeping storage bounded.

7Follow-up questions

Why does compression get worse for counters that reset or values with random noise?

A counter that resets jumps from a large value to near zero, and the XOR of two very different floats has meaningful bits across the whole word, so that sample costs close to its full size. Random noise, such as a value measured with many significant digits that changes in its low bits every time, also leaves many meaningful bits after XOR, so every sample costs several bytes. Irregular scrape intervals hurt the timestamp side in the same way, because the delta of deltas is no longer zero.

Does TimescaleDB use the same encodings?

Its compressed chunks are columnar and use delta-of-delta encoding for timestamps and integers, Gorilla-style XOR for floats, and dictionary or other encodings for text, so the ideas are the same, applied to PostgreSQL chunks. Check your version for the exact names of the compression settings, which have changed recently.

Why are samples appended in order and rarely updated?

Because the encoding depends on the previous sample; inserting a value in the middle would mean re-encoding the chunk. Systems that accept out-of-order samples handle them in a separate buffer and merge later.

8Conclusion

In this answer, we saw that time-series stores compress samples by exploiting regularity: delta of deltas turns most timestamps into a single zero bit, and XOR with the previous value leaves few meaningful bits for similar floats, bringing 16 bytes to about 1 to 2. Data is cut into time blocks or chunks so recent data stays in memory and retention drops whole blocks, a label index maps series to ids so cardinality drives memory, and downsampled rollups keep long-range queries fast and storage bounded.

What to say in the interviewAbout 80 seconds aloud

Open with

Time-series data has a very regular shape, and the stores exploit it. For timestamps, samples arrive at almost fixed intervals, so instead of storing 64 bits each, they store the delta of deltas, the change in the interval, which is usually zero and takes one bit.

Then, if they want more

For values, consecutive floats are similar, so they XOR each value with the previous one; identical values become a single bit, and similar ones leave a short run of meaningful bits in the middle. That is the Gorilla encoding Prometheus uses, and it gets a typical sample from 16 bytes to about one or two.

On disk, data is organised by time: Prometheus keeps a two-hour head block in memory with a WAL, writes immutable two-hour blocks, compacts them into larger ones, and deletes whole blocks when they pass retention; TimescaleDB does the same with chunks of a hypertable.

An inverted index maps labels to series ids, so memory depends on how many distinct series there are, the cardinality, not on the number of samples, which is why high-cardinality labels are dangerous. And downsampling, like five-minute min, max, sum and count after 30 days, keeps long-range queries fast and storage bounded.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do time-series stores compress samples so well, and how do they organise data on...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/09c48d106c/