Design a Time Series Database
Understanding the Problem
📈 What is a time series database? A time series database (TSDB) stores streams of timestamped numbers, such as CPU usage every 15 seconds or request counts per endpoint, and answers questions about how they change over time. Data arrives in time order, is almost never updated, is queried mostly for the recent past, and is aggregated across many series at once ("p99 latency by region, last 6 hours"). Prometheus, InfluxDB, TimescaleDB, VictoriaMetrics, M3 and Grafana Mimir are common examples.
When an interviewer says "Prometheus-compatible," they mean three concrete things: the data model (a series is a metric name plus a set of key-value labels), the remote-write protocol that agents use to push samples, and the PromQL query API that dashboards and alerting use. A single Prometheus server stores data on its local disk and only scales so far; the question is how to build the horizontally scalable, long-retention store behind it. This post is about the storage engine and its distribution. The full monitoring product, with scraping, alerting and paging, is in metrics monitoring.
Functional Requirements
Core Requirements
- Agents should be able to push samples for series identified by a metric name and labels, using the Prometheus remote-write protocol.
- Users should be able to query with PromQL, selecting series by label matchers over a time range, with functions and aggregations like
rate()andsum by (region). - Operators should be able to set a retention period per tenant, after which data is deleted.
- Users should be able to query long ranges (months to years) quickly, using downsampled data.
Below the line (out of scope)
- Scraping targets, evaluating alerting rules and sending notifications. They sit on top of this store.
- Logs, traces and events.
- Updating or deleting individual samples, apart from retention and tenant-wide deletes.
Non-Functional Requirements
Core Requirements
- The system should ingest 100 million active series at a 15-second interval, about 6.7M samples/sec, and absorb series churn of around a billion distinct series over the retention period.
- The system should keep raw data for 30 days, 5-minute rollups for 1 year and 1-hour rollups for 5 years.
- Queries over the last few hours should return in under 1 second at p99; queries over a year in under 10 seconds.
- The system should not lose acknowledged samples when a node fails, and ingestion should keep working during failures, even if queries degrade.
- The system should be multi-tenant, with per-tenant limits so one team's mistake doesn't affect others.
Below the line (out of scope)
- Strong consistency for queries. A sample may take a few seconds to become visible, and that's fine for monitoring.
- Multi-region replication of the store.
The math shows where the effort should go.
Raw volume. A sample is an 8-byte timestamp and an 8-byte float, so 6.7M samples a second is about 107 MB/s, or 9.2 TB a day uncompressed. Thirty days is about 280 TB, and with three replicas nearly 850 TB. That would be a large and expensive system.
Compressed. With the compression in deep dive 1, typical monitoring data gets down to roughly 1.3-1.5 bytes per sample. That's about 9 MB/s, or around 800 GB a day, and 24 TB for 30 days. Compression shrinks the raw store by more than 10x, and it's why the whole working set of recent data can sit in memory and on local disk.
The index. 100 million active series, each with ten or so labels, is the other big structure. Finding "all series where job="api" and status=~"5.."" among them needs an inverted index, and its size grows with the number of distinct series, not the number of samples.
The interesting part of a TSDB isn't throughput. 6.7M small appends a second spread over a few dozen nodes is manageable. The interesting parts are compression (which makes storage cheap) and cardinality (which makes the index expensive). Lead with both.