Database Deret Waktu: InfluxDB, TimescaleDB, dan Lainnya

What Is a Time Series Database?

A time series database (TSDB) is optimized for storing and querying data that is indexed primarily by time. Unlike relational databases designed for arbitrary queries with normalized schemas, TSDBs assume that most queries will be range-based on a timestamp and use data compression techniques that exploit the regular nature of time-ordered data.

Key Characteristics

  • Write-heavy workloads: Millions of data points per second (sensor data, metrics)
  • Time-range queries: "Average CPU over the last 5 minutes" not "all users named John"
  • Automatic retention policies: Downsample and delete old data automatically
  • Compression: Time-ordered data compresses extremely well (delta encoding)
  • Immutable past: You rarely update historical data, only append new points

InfluxDB

The most popular purpose-built TSDB. Uses its own query language (Flux) and line protocol for ingestion:

# InfluxDB line protocol
# measurement,tag_key=tag_value field_key=field_value timestamp
cpu_usage,host=server01,region=us-east value=78.5 1700000000000000000

# Flux query — average CPU over last 5 minutes
from(bucket: "metrics")
  |> range(start: -5m)
  |> filter(fn: (r) => r._measurement == "cpu_usage")
  |> mean()

TimescaleDB

PostgreSQL extension that adds time series superpowers to standard SQL. Best for teams already using PostgreSQL:

-- Create a hypertable (auto-partitioned by time)
SELECT create_hypertable('sensor_readings', 'time');

-- Regular SQL works, but fast on time ranges
SELECT time_bucket('5 minutes', time) AS bucket,
       avg(temperature) AS avg_temp
FROM sensor_readings
WHERE time > NOW() - INTERVAL '1 hour'
GROUP BY bucket
ORDER BY bucket;

Prometheus

Pull-based metrics system with its own TSDB, the de facto standard for Kubernetes monitoring:

# PromQL query — 5-minute rate of HTTP requests
rate(http_requests_total{status="200"}[5m])

# Alert rule
- alert: HighErrorRate
  expr: rate(http_errors_total[5m]) > 0.1
  for: 2m

Other Notable TSDBs

  • Apache Cassandra — distributed, used by Netflix for time-windowed data
  • QuestDB — high-performance, PostgreSQL-compatible SQL
  • ClickHouse — columnar OLAP, excellent for analytics on time-ordered data
  • Amazon Timestream — managed TSDB on AWS

When to Use a TSDB vs PostgreSQL

ScenarioChoice
Server metrics, IoT, monitoringInfluxDB / Prometheus
Time-ordered events in existing Postgres appTimescaleDB
Financial tick data, high-frequency tradingKDB+, QuestDB
Standard app with some time queriesPostgreSQL TIMESTAMPTZ