CSE Roadmap

Common tier · about 4 hours · 8 parts

Part 3 of 8 · 2 min read

Data model

Names, labels and the right database.

A series is identified by a metric name and optional labels, which are key and value pairs. A data point is the series, a time and a value. The common text format, called a line protocol, looks like this:

cpu.load  host=web01,region=us-west  1613707265  50
cpu.load  host=web02,region=us-west  1613707265  43

Queries are like: "what was the average CPU load across web servers in us-west for the last 10 minutes?" That needs a search by labels and a calculation over time.

Why not a general database? A relational database could store it, but it needs expert tuning at this scale, does not cope well with constant heavy writes, and has no natural tools for time questions. A moving average in SQL is long and hard to read. Labels also need one index each. A NoSQL store such as Cassandra can hold time series, but its schema needs deep knowledge to scale. The book's advice is to use a database built for time series, such as InfluxDB or Prometheus. These keep recent data in memory, store older data on disk, index by labels, and offer query languages made for time. In an interview, say what the data is and name such a database. Do not explain its internals.

Label cardinality. An index on labels works when each label has few possible values. A label such as region is fine. A label holding a user id or a request id creates millions of series and breaks the index. This is the main modelling rule.

Read pattern. The book quotes a study at Facebook: at least 85 per cent of queries were for data from the last 26 hours. A good time-series database exploits this by keeping recent data hot.

A worked example (made up). If a label user_id has 5 million values, and each existing series is split by it, then 10,000 series become 50 billion. No database copes. The label belongs in logs, not metrics.

Cardinality

Cardinality is the number of distinct values a field can take.

Example: region has 8 values, so low cardinality. request_id has billions, so very high.

Check yourself: 1. What identifies a time series? 2. Why is a user id a bad label? 3. Why is a time-series database better than MySQL here?