Time-Series Databases

Store and query timestamped measurements efficiently with TimescaleDB or InfluxDB.

Advanced · about 3 hours · difficulty 3/5

Learn first: NoSQL Families and When to Use Them, Writing SQL Queries

Metrics, sensor readings, stock ticks and application events share a shape: an ever-growing stream of timestamped values that are written once, rarely updated, and queried by time range with aggregation ("average CPU per 5 minutes over the last day").

Why a specialized store

A plain table works at first, but at millions of rows per day you want:

  • Time-based partitioning (chunks per day or hour) so queries only touch relevant data.
  • Compression tuned for sequential numbers, often 10x or more.
  • Retention policies that drop old raw data automatically.
  • Downsampling or continuous aggregates that keep hourly rollups after raw data expires.

Common options

  • TimescaleDB: a PostgreSQL extension. You keep SQL, joins and your existing tooling, and gain hypertables, compression and continuous aggregates. The easiest step for teams already on Postgres.
  • InfluxDB: a purpose-built time-series database with its own ingestion protocol, popular for IoT and monitoring.
  • Prometheus: specialized for application metrics scraping, covered in observability.
  • ClickHouse: a columnar analytics database often used for large event and log analytics.

Modeling tips

Separate tags/labels (low-cardinality dimensions like region or device type) from fields (the measured numbers). High-cardinality labels such as user ids can explode index size in some engines.

You are ready to move on when you can ingest a simulated sensor stream, query 5-minute averages over a day, and configure a retention policy.

Key concepts

  • Time-based partitioning
  • Retention and downsampling
  • TimescaleDB hypertables
  • InfluxDB
  • Tags vs fields and cardinality
  • Columnar analytics (ClickHouse)

Mini project

Build a small IoT ingest API that writes readings into TimescaleDB, exposes a time-bucketed chart endpoint and drops raw data older than 7 days.

Check yourself

Why is using a user id as a tag a potential problem in a time-series database?

Free resources