Time-Series Database Design

How databases for metrics, sensor readings and other timestamped data take huge write rates, compress them to a byte or two per point, keep old data cheap, and answer range queries fast.

Overview

A time series is a stream of measurements of one thing over time: the CPU use of one server every 15 seconds, the temperature from one sensor every minute, the price of one stock on every trade. A time-series database (TSDB) is built to store millions of these streams and answer questions such as 'the average request rate per service over the last six hours'.

The workload is unusual. Writes arrive constantly and are nearly all for the current moment; old points are almost never changed. Reads ask for ranges of time, usually recent ones, and nearly always aggregate them (averages, rates, percentiles) instead of reading single points. And the volume is large. When Facebook described its Gorilla database in 2015, its monitoring systems were taking in about 12 million data points a second.

A general-purpose database can store this data, but it pays for features the workload does not use (in-place updates, per-row indexes) and misses optimizations it could use. TSDBs split data by time, store each series' points together in compressed blocks, and delete old data by dropping whole blocks.

What you will be able to do

  • Describe the data model (series, labels, samples) and why the number of series matters more than the number of points.
  • Explain the write path: an in-memory head, a write-ahead log, immutable time blocks and compaction.
  • Explain how delta-of-delta and XOR encoding shrink a point from 16 bytes to about 1.4.
  • Design retention and downsampling so that year-long charts stay fast.
  • Choose between Prometheus-style storage, a long-term store, and PostgreSQL with time partitions.

Key concepts

  • Series and samples
  • Append-only writes
  • Time-partitioned blocks
  • Delta-of-delta timestamps
  • XOR value compression
  • Cardinality
  • Downsampling
  • Retention
  • Label index

Interview questions

  • Design a metrics system that ingests 10 million samples per second.
  • Why is time-series data so compressible, and how does Gorilla compress it?
  • What is cardinality, and how do you stop it from taking down a TSDB?
  • Explain how data is written and later deleted in a TSDB like Prometheus.
  • How do you keep dashboards over a year of data fast?
  • When would you use PostgreSQL instead of a dedicated TSDB?
  • How would you estimate storage for a metrics system?
  • What happens when samples arrive late or out of order?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio