#07Build a Prometheus-style time-series database
The simplest database that can absorb a 1M-points-per-second firehose and still answer `sum(rate(http_requests_total{status="500"}[5m]))` in milliseconds — built bit by bit, literally.

You are designing the simplest database that can stomach a metrics firehose: 10,000 hosts each emitting 50 numbers every 15 seconds, forever, without losing precision and without needing a rack of disks. Clients push points in (scrape), or run PromQL-shaped queries pulling them out (sum(rate(metric{labels}[range]))); the engine packs every sample into a few bits on disk and answers a 5-minute alert in single-digit milliseconds.

Prometheus and Gorilla are the canonical worked examples. Once you build the core, the design space of every metrics store collapses to a handful of named trades — the bits-per-point you accept, the cardinality budget you defend, the depth-of-retention you afford, and where you put the line between "single-node" and "somebody else's problem."

Resist the urge to "describe Prometheus." Make decisions yourself, defend them, and let the workload push back. The point is to feel why each price is paid and which workload pays it.

Builds on: Raft consensus — 2-min primers appear where needed.
Reading: Pelkonen et al. — Gorilla: A Fast, Scalable, In-Memory Time Series Database (Facebook, VLDB 2015) · Fabian Reinartz — Writing a Time Series Database from Scratch (fabxc.org/tsdb, 2017) · Ganesh Vernekar — Prometheus TSDB (Part 1): The Head Block · Brian Brazil — Cardinality Is Key (robustperception.io) · Prometheus storage and naming docs (prometheus.io) · InfluxData — New Storage Engine: Time-Structured Merge Tree (TSM)
the time-series point shape: (metric, label_set, timestamp, value)
delta-of-delta encoding for fixed-cadence timestamps
XOR encoding for adjacent IEEE-754 floats
chunks: 120 samples or 2 hours of compressed points
head chunk in RAM + write-ahead log + sealed mmapped chunks
inverted index: postings lists per (label, value), intersection at query time
cardinality as the master operational variable
downsampling and the retention pyramid
single-node TSDB + HA via parallel scrapers + remote-write