Time-series data
Metrics, IoT readings, stock ticks and app telemetry share a shape: (series identity, timestamp) → value, written once, appended in time order, queried by time range, and rarely updated.
| Property | Consequence for storage |
|---|---|
| Append-only, time-ordered writes | Write to in-memory buffers and flush sorted, immutable chunks (LSM-like) |
| Queries by time range and series | Partition by time; index by series labels |
| Recent data is hot | Keep recent chunks in memory/SSD; push older ones to object storage |
| Old data matters less in detail | Downsample: keep 1-hour averages, not every 10-second point |
| Deletes are by age | Drop whole time partitions instead of deleting rows |
Specialised stores (Prometheus, VictoriaMetrics, TimescaleDB, InfluxDB, ClickHouse) exploit this:
- Delta-of-delta timestamps and XOR-compressed floats shrink 16-byte points to ~1–2 bytes.
- Time-partitioned chunks make range scans sequential and make deleting old data a cheap "drop chunk".
- Downsampling: keep 10-second raw data for 7 days, 1-minute rollups for 90 days, 1-hour rollups for 2 years.
When you downsample, keep min, max, sum and count per bucket, not just the average. You can't rebuild a max from averages, and "the CPU peaked at 100% for 20 seconds" disappears entirely in a 1-hour average.
The series model and cardinality
A series is identified by a metric name plus labels: temperature{device="d-4412", site="pune-3"}. Each unique label combination is a new series with its own index entry and in-memory buffer.
Cardinality explosion. Adding user_id or request_id as a label creates millions of series and can bring down the database. Keep series labels bounded. If you need per-user detail, use logs, traces or an analytics store designed for high-cardinality data.
Two storage designs
| Design | How | Examples | Fits |
|---|---|---|---|
| Series-oriented TSDB | Each series is its own compressed stream of points, with an inverted index on labels | Prometheus, VictoriaMetrics, M3 | Infrastructure metrics, alerting |
| Columnar table with time partitions | Rows of (time, dimensions…, values) stored by column, sorted by time | ClickHouse, TimescaleDB, Druid | IoT, events, flexible SQL over high-cardinality data |
Case Study C25 of the book designs a full metrics platform on the first model; Case Study C20 of the book uses the second.
Analytics: OLTP vs OLAP
| OLTP (transactions) | OLAP (analytics) | |
|---|---|---|
| Queries | Short, by key: "order 881" | Long scans: "revenue by city by week" |
| Rows touched | Few | Millions to billions |
| Concurrency | Thousands of small queries/s | Tens of heavy queries |
| Storage layout | Row-oriented | Column-oriented |
| Examples | PostgreSQL, MySQL | ClickHouse, BigQuery, Snowflake, Redshift, Druid, Pinot |
Running heavy analytics on the production OLTP database is a classic mistake: one finance query scanning a year of orders can starve checkout of I/O. Copy the data out.
Why columnar is fast
Three effects multiply:
- Read less: a query summing
amountreads only that column, not entire rows. - Compress better: similar values stored together (all cities, all amounts) compress 5–10× with dictionary, run-length and delta encodings.
- Process faster: vectorised execution applies each operation to thousands of values at a time, using modern CPU instructions.
Add partitioning (by date) and sorting (by a common filter such as city or customer_id), and the engine can skip whole blocks using min/max statistics without reading them.