Prompt
How do I store millions of metric points per second in a time-series database?
Latest observation
Storing millions of metric points per second in a time-series database is mostly an architecture and ingestion problem, not just a “pick a DB” problem. The key is to design for write throughput, compression, partitioning, and retention from the start.
1) Use an ingestion pipeline, not direct app writes
At that scale, don’t have every producer write straight into the database.
Typical flow:
producers → buffer/queue → ingest workers → TSDB
Use something like:
- Kafka / Pulsar / Kinesis for buffering
- stateless ingest services to batch and write
- optional stream processing for aggregation/filtering/dedup
This helps with:
- smoothing bursts
- retrying safely
- horizontal scaling
- avoiding DB overload
2) Batch aggressively
Write points in large batches, not one by one.
Good batching reduces:
- network overhead
- per-write transaction cost
- index churn
Practical approach:
- batch by time window or message count
- keep batches reasonably sized, e.g. thousands to tens of thousands of points per write, depending on the DB
3) Keep the schema narrow and disciplined
A TSDB performs best when the data model is simple.
Common pattern:
- timestamp
- metric name
- tags/dimensions (host, service, region, etc.)
- numeric value (or a small fixed set of fields)
Important:
- Avoid unbounded tag cardinality
- Don’t put user IDs, request IDs, or other highly unique values into tags unless you really know the cost
- Prefer numeric fields over lots of text
High-cardinality tags are one of the fastest ways to destroy performance and storage efficiency.
4) Control cardinality
This is critical.
Cardinality = number of distinct time series.
Example: cpu_usage{host=..., region=..., service=...}
Too many unique combinations cause:
- memory blowups
- slow indexes
- expensive queries
- poor compression
Rules of thumb:
- Keep tag sets small and stable
- Put frequently changing identifiers into logs/traces, not metrics
- Aggregate at the edge when possible
5) Partition/shard by time and key
Time-series databases rely heavily on partitioning.
Usually partition by:
- time first
- then by tenant / metric / hash shard
This allows:
- fast writes into current partitions
- efficient pruning for time-range queries
- easier retention deletion
A common approach is:
- daily or hourly partitions for hot/warm data
- shard within each partition across nodes
6) Use compression-friendly formats
Metrics compress extremely well if stored properly.
Good practices:
- store timestamps as deltas
- use columnar or block-based storage
- use codecs like Gorilla-style compression, delta-of-delta, run-length encoding, dictionary encoding
If your DB supports it, enable:
- compressed blocks
- columnar storage
- time-partitioned files
- downsampling/rollups
7) Separate hot ingestion from long-term storage
For very high throughput, many systems use a tiered model:
- hot tier: fast writes, recent data
- warm tier: queryable recent history
- cold tier: cheaper object storage / archive
You often keep:
- raw high-resolution data for a short retention window
- downsampled aggregates for longer-term analysis
Example:
- 10s resolution for 7 days
- 1m resolution for 90 days
- 1h resolution for 1 year
8) Pre-aggregate where possible
If you don’t need every raw point forever, aggregate before storage.
Examples:
- min / max / avg / p95
- per-minute rollups
- per-service aggregates instead of per-instance when acceptable
This dramatically cuts:
- write volume
- storage cost
- query cost
9) Plan for backpressure and loss policy
At million-point/sec scale, components will fail or slow down.
Define what happens when the system is overloaded:
- buffer in queue
- drop low-priority metrics
- sample
- shed load
- apply backpressure upstream
You need explicit policies for:
- late data
- duplicate data
- retries
- partial outages
10) Choose a database built for this workload
Good options depend on your access pattern:
If you need raw ingestion + operational metrics
- VictoriaMetrics
- Mimir / Cortex (Prometheus-style scale)
- InfluxDB (depending on version/workload)
- TimescaleDB (great SQL, but very high ingest may require careful tuning/sharding)
- ClickHouse for analytics-heavy time-series workloads
If you need extreme scale and long retention
- ClickHouse
- Apache Druid
- Pinot
- custom ingestion into object storage + query layer
There is no universal winner.
Pick based on:
- query patterns
- cardinality
- retention
- multi-tenancy
- exact ingest rate
- operational complexity
11) Tune the storage engine
Important knobs vary by system, but generally:
- increase write concurrency carefully
- tune memtables / WAL / flush intervals
- size shards/partitions appropriately
- make sure disks are fast enough for sequential writes
- use NVMe if possible
- separate WAL and data if the DB benefits from it
- monitor compaction pressure
At this scale, compaction can become the bottleneck.
12) Monitor the monitors
You need observability on:
- ingest lag
- queue depth
- write latency
- dropped points
- compaction backlog
- cardinality growth
- disk I/O
- memory use
- query latency
Without this, the system fails silently or degrades gradually.
Practical reference architecture
A common high-scale design:
- Agents/exporters send metrics
- Data lands in Kafka/Pulsar
- Consumer group batches points
- Ingest layer validates, samples, and routes
- TSDB writes into partitioned shards
- Compaction/rollup jobs compress and downsample
- Older data is moved to cold storage or deleted by retention policy
Biggest mistakes to avoid
- Writing each point individually
- Too many high-cardinality tags
- No batching
- No queue/buffer layer
- No retention/downsampling strategy
- Choosing a DB without testing your exact workload
- Ignoring compaction and disk IO limits
Rule of thumb
If you truly mean millions of points per second sustained, the answer is usually:
- buffer with Kafka/Pulsar
- batch writes
- control cardinality
- partition by time
- compress aggressively
- downsample old data
- scale horizontally
- benchmark with real data
If you want, I can help you design a concrete architecture for:
- Prometheus-style metrics,
- custom application metrics, or
- analytics-heavy time-series at massive scale.