Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Design a 10M-Metric/Sec Time-Series
Database & Downsampling Engine.

Shard ingestion by series hash onto Gorilla-compressed 2h in-memory Head blocks backed by WAL, flush immutable blocks to S3, and compact 10s raw points into 5m/1h downsampled rollups.
Start the journey5 stages· Last modified Nov 26, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When designing a 10M-metric/sec TSDB, pair 2-hour Gorilla Head blocks + inverted label indexes with Distributor cardinality quotas and associative S3 rollups.

TL;DR30-second simplified summary
▾

Distributors enforce per-tenant cardinality caps, Ingesters hold ~101 GB of 2h Gorilla chunks in RAM, and Compactors build 5m/1h {min, max, sum, count} rollups for 360x faster 90-day queries.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Gorilla Time-Series Compression
Know how 2-hour Gorilla chunks compress 16-byte (ts, float64) samples to ~1.37 bytes in RAM.
Inverted Indexes & Cardinality
Understand how label=value posting lists map metric selectors to Series IDs—and why unbounded labels like user_id explode memory.
Full walkthrough · 5 stages
01
L4 · FUNDAMENTALS

R & E · Requirements, Ingestion & Retention Math.

10M samples/sec x 16 bytes uncompressed = 160 MB/sec = 13.8 TB/day = 5.04 PB/year if stored raw.

Illustrative Sizing: With Gorilla compression (~1.4 B/pt), 10M samples/sec = 14 MB/sec = 1.21 TB/day. A 2-hour hot Head window across 50M active series needs 14 MB/s * 7,200 s ~ 101 GB of compressed chunk RAM (plus ~100 GB for the in-memory inverted label index -> easily served by 16–20 ingester nodes with 32 GB RAM each at RF=3).

Storage & Query Scale

Raw Row DB (5 PB/yr) vs. Gorilla Head + Rollup Tiering

Before · Raw Uncompressed TSDB
  • 13.8 TB/day raw writes
  • Random B-Tree disk thrashing
  • 90d query scans 777K pts/series
After · Gorilla + Object Store + Rollups
  • 1.21 TB/day (~11x smaller)
  • 101 GB hot 2h Gorilla RAM
  • 360x faster 90d rollup scans
02
L4 · FUNDAMENTALS

S · Storage: Inverted Label Index + 2h Gorilla Chunks.

Users query metrics by label selectors like http_requests_total{env='prod', region='us-east'}. Scanning 50M series names sequentially would take seconds.

Each block maintains an Inverted Index: Postings[(label_name, label_val)] -> sorted uint32[] SeriesIDs. Querying env='prod' AND region='us-east' intersects two sorted posting lists in sub-millisecond time, then reads only the matching SeriesIDs' Gorilla chunks.

Block Layout

Inverted Label Postings -> Series ID Table -> Gorilla Chunks

03
L5 · APPLIED DESIGN

H & A · Write Path, Replication & Fan-Out Query Engine.

Distributors receive Prometheus remote_write batches, enforce tenant cardinality/rate limits, and hash (tenant_id, metric_labels) onto a consistent-hash ring of Ingesters with RF=3.

Each Ingester appends incoming samples to a local sequential WAL (for crash recovery) and into the series' 2-hour Gorilla chunk. Every 2 hours, Ingesters compact the sealed Head chunk into an immutable block and upload it to S3/GCS. Queriers fan out reads in parallel: t > now - 2h goes to Ingesters; t <= now - 2h goes to Store-Gateways reading S3 blocks.

Write & Read Paths

Distributor -> Ingester (WAL + Gorilla) -> S3 Block -> Querier Merge

DistributorIngester (RF=3)S3 / CompactorQuery Engine1. Hash(series) -> W=2 Gorillaappend2. Flush 2h block + inverted index3. Fetch hot <2h chunks4. Fetch cold >2h rollups & merge
04
L5 · APPLIED DESIGN

D · Detailed Design: Downsampling & Cardinality Protection.

When a buggy microservice accidentally puts a trace_id into a Prometheus label, active series count jumps from 50K to 50M in minutes—crashing Ingesters with OOM.

Protect Ingesters at the Distributor using a per-tenant HyperLogLog / bounded series-cache limiter: reject new series with HTTP 429 once a tenant exceeds its quota. Meanwhile, asynchronous Compactors read raw 2h S3 blocks and emit 5m and 1h rollup blocks storing {min, max, sum, count} per bucket.

Data Tiering & Rollups

Hot 2h Gorilla (10s) -> Cold S3 (10s) -> 5m Rollup -> 1h Rollup

flush every 2hcompact > 24hcompact > 30dHot Head (10s,RAM)S3 Raw Block (10s)5m Rollup (30d)1h Rollup (1yr)
Engineering Decision Framework
Problem
How to keep both 2-hour alerting (<50 ms) and 90-day dashboards (<500 ms) fast at 10M metrics/sec.
Naive Solution
Store all 10s raw points in one database and scan raw rows for 90-day graphs.
Why It Fails
90-day queries scan 777,600 points per series, saturating disk/S3 bandwidth and timing out.
Alternatives
Raw-only scans vs. Pre-computed avg rollups vs. Associative {min, max, sum, count} rollups.
Engineering Decision
Keep 2h Gorilla chunks in Ingester RAM, flush 2h blocks to S3, and compact into 5m/1h {min, max, sum, count} rollups.
Trade-Offs
Adds ~15–20% extra S3 storage for rollup tiers in exchange for 360x faster multi-month dashboard queries.
Decision Exercise · Make the Call

A team pre-computes 1-hour rollups storing only avg_value per hour. When a user queries avg_over_time across 7 days with uneven scrape counts (some hours had 10 samples, others had 360), the dashboard shows wrong numbers. Why?

05
L6 · SCALE & TRADE-OFFS

E & D · Evaluation, Assumptions, Limitations & Glossary.

Sizing assumes a 10s regular scrape interval across 50M active series (~10M/sec) and ~1.4 bytes/point average Gorilla compression.

Assumptions & Limitations: If clients send heavily out-of-order samples (>2 hours late), they cannot append to the active Head chunk and must write to an Out-of-Order (OOO) head block that compacts separately on flush. Inverted index RAM scales with unique label-value pairs, requiring strict per-tenant cardinality quotas.

End-to-End TSDB

Distributor -> Ingester (WAL + Gorilla) -> Compactor -> S3 -> Querier

Distributor (Quota)ingressIngester (2hGorilla)computeS3 Blocks + RollupsstorageQuerier (Hot +Cold)controlRF=3 W=22h block flush<2h hot>2h rollups
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

At 10M samples/sec, why does a TSDB keep 2 hours of Gorilla chunks in Ingester RAM instead of writing directly to S3 on every scrape?

Recommended Learning Flow