Metrics and events over time
High write rate of timestamped points, queried by time range and aggregated.
Say it like this
The writes are millions of timestamped points queried by time range and aggregated, so I use a time-series store that partitions by time, compresses columns, and downsamples old data instead of keeping every point forever.
Basic solution
Use a time-series database with compression and downsampling.
Append to the newest chunk, read a range, roll up the rest
Points land in the current time chunk for their series. A query touches only the chunks in its range; a background job rolls old chunks into hourly aggregates and drops the raw files.
How it works
- Every point is (series, timestamp, value) where the series is a metric name plus tags (host, region, video_id). All points with the same name and tags belong to the same series.
- Writes are appended to the newest time partition (called a chunk) for that series; chunks cover a fixed window such as one hour or one day, so an insert only ever touches the current chunk and old chunks are never modified.
- Within a chunk, values are stored column-wise: all timestamps together and all values together. Timestamps are compressed by storing the change in the gap between consecutive points (delta-of-delta, usually zero for regular samples) and values by XOR with the previous value (Gorilla encoding, which leaves mostly zero bits for slowly changing numbers). The result is typically 10–20× smaller than rows.
- A query names a time range and a set of tags. The engine lists the chunks whose window overlaps the range, finds the series matching the tags in each, reads only those columns, and aggregates (avg, p99, rate) into one value per output bucket.
- Retention policies drop old chunks by deleting whole files, which is instant; downsampling jobs read raw chunks and write 1-minute and 1-hour aggregates into their own series that are kept for longer.
- Cardinality is the hidden cost: each distinct tag combination is its own series with its own index entry and in-memory state, so a tag with a million values multiplies everything by a million.
Why it is its own engine
- Append-only by time means sequential writes and cheap deletes by chunk.
- Range plus aggregate is the only query, so the layout optimises for exactly that.
Worked example
View events for the video platform
- 100M DAU, each ~5 views/day: 500M views/day ≈ 5.8K/s average, 30K/s at peak. Each raw event ~100 B with tags video_id, region, device.
- Raw points: 500M × 100 B = 50 GB/day uncompressed; with column compression at ~12× ≈ 4 GB/day. Keep 30 days raw ≈ 120 GB, which is easy on one node pair.
- Series cardinality: 10M active videos × 20 regions × 3 devices = 600M possible series. That is the trap: 600M index entries would not fit in memory. Store per-video counts without region and device (10M series), and keep region/device as a separate rollup keyed by region and device only (60 series).
- Query "views for video X, last 7 days, per hour": one series, 7 daily chunks, 168 buckets, milliseconds, because only that series' columns in those 7 chunks are read.
- Downsample: after 30 days keep hourly totals per video: 10M × 24 × 8 B ≈ 2 GB/day → 1 year ≈ 700 GB. After a year keep daily, which is 24× smaller again.
- Dashboards: Prometheus-style scrape of service metrics is the same engine shape at a tiny fraction of the volume.
Variants and when each wins
Prometheus (pull, local storage)
- It scrapes each service's metrics endpoint on a schedule, stores a couple of weeks on local disk, and includes alerting. It wins as the default for service metrics; for business events or long retention add Thanos or Mimir, which ship its blocks to object storage.
TimescaleDB (Postgres extension)
- Hypertables partition an ordinary Postgres table by time, so you keep SQL, joins and the rest of Postgres, with compression and continuous aggregates added. It wins when metrics live beside relational data and the team already runs Postgres.
InfluxDB
- A purpose-built engine with its own query language, strong on ingestion rate and retention policies. It wins for a single team that wants something simple to run and has no need to join metrics with other data.
ClickHouse (column store used as a TSDB)
- A general column store that handles high-cardinality event streams (per-user, per-video) and ad-hoc analytics in the same system. It wins when cardinality is high and queries are not known in advance, at the cost of more operational work.
Pick Prometheus for infrastructure metrics, TimescaleDB when you already run Postgres and need SQL over time, ClickHouse when cardinality is high and queries are ad hoc.
Failure modes
Cardinality explosion
- You notice memory climbing, ingestion slowing and the series index no longer fitting in RAM. It happens because a tag with unbounded values (user_id, request_id) was multiplied by the other tags, creating millions of series. Never tag by unbounded ids in the metrics store; pre-aggregate those dimensions, or send per-entity series to a column store built for high cardinality.
Writes to old time ranges
- You notice backfilled or late points being slow or rejected. It happens because the engine assumes appends go to the newest chunk, and writing into a sealed, compressed chunk means decompressing and rewriting it. Buffer late data and write it in batches, or widen the engine's out-of-order window and accept worse compression.
Query over too many chunks
- You notice a year-long dashboard timing out. It happens because the query is scanning raw points across 365 chunks instead of reading a rollup. Build continuous aggregates per hour and day, and have each dashboard read the rollup whose resolution matches what it displays.
Retention deletes the wrong thing
- You notice a long-term trend disappearing after 30 days. It happens because the raw retention policy dropped chunks before the downsampling job had rolled them up. Order the jobs explicitly: downsample first, confirm the rollup exists, then drop the raw chunk.
Single ingestion node
- You notice a burst of events backing up and dashboards going stale. It happens because one writer process receives every point and cannot keep up. Put a queue in front, run several ingesters each owning a hash range of series, or use a store that shards natively.
Interviewer follow-ups
Answer out loud before you open each one.
Why not just use a normal table with a timestamp index?
It works to a few hundred million rows, then inserts spread across the index, compression is poor and deleting old data is a slow DELETE instead of dropping a chunk. Time partitioning, columnar compression and rollups are what make tens of billions of points cheap.
How do you handle a metric per user?
I do not put user ids in a metrics TSDB; that is unbounded cardinality. Per-user counters go in a key-value store or a column store built for high cardinality, and the TSDB keeps aggregates like active users per region.
What is downsampling and what do you lose?
Rolling raw points into coarser buckets, say 1-minute averages after a week and hourly after a month. You lose the ability to see short spikes in old data, so keep max and count alongside the average, not just the average.
How do you compute p99 latency over time?
Store histograms per bucket, not raw latencies, then compute the percentile from the histogram at query time. Averaging percentiles across hosts is wrong; merging histograms is right.
Connections
The same parts on other clouds
The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.
| AWS | Used here as | Google Cloud | Microsoft Azure | Open source and others |
|---|---|---|---|---|
| Amazon EC2 Auto Scaling | Ingesters | Compute Engine managed instance groups | Virtual Machine Scale Sets | Kubernetes Deployment with a HorizontalPodAutoscaler, Nomad |
| Amazon EMR | Rollup job | Dataproc | Azure Databricks, HDInsight | Apache Spark, Databricks |