Redis TimeSeries

What is a Time Series?

series of data points ordered by time

most commonly sequence taken at successive equally-spaced points in time.

graph

Trend in the last 24 Months

db engines ranking

Time Series in Redis

The traditional way:

  • Sorted Sets & Hashes

  • Streams

What is missing?

  • Enhanced queries: Labeling

    sensor_id:2, cpu:8, area:5

  • Downsampling (compaction)

  • Enhanced queries: Aggregation

    avg, sum, min, max, range, count, first, last

RedisTimeSeries Basics

logo
  • Each key is a time series

  • Each key can have any set of labels

  • Each key can have multiple downsampling rules

    Each downsampling rule will write to a different key

Create a new time series

TS.CREATE key [RETENTION retentionSecs] [LABELS field value]
  • key: Key name for timeseries

  • retentionSecs: Maximum age for samples

  • labels: set of key-value pairs that represent metadata labels

Example
TS.CREATE ts:2 RETENTION 0 LABELS sensor_id 2 type temperature

Append a new value to the series

TS.ADD key timestamp value
  • timestamp: epoch timestamp (in seconds) or * for automatic timestamp (using the system clock)

  • value: Sample numeric data value (double)

Examples
TS.ADD ts:2 1548149181 30
TS.ADD ts:2 * 30

Aggregation, Compaction, Downsampling

TS.CREATERULE sourceKey destKey AGGREGATION aggType bucketSizeSeconds
  • sourceKey: Key name for source time series

  • destKey: Key name for destination time series

  • aggType: Aggregation type: avg, sum, min, max, range, count, first, last

  • bucketSizeSeconds: Time bucket for aggregation in seconds

Delete Rule

TS.DELETERULE sourceKey destKey
Examples
TS.CREATERULE ts:2 ts:avg:2 AGGREGATION avg 60
TS.DELETERULE ts:2 ts:avg:2

Downsampling

Raw data

graph original

Downsampling

Downsampled and aggregated using average

graph downsample