Redis

Data Analytics with Redis Time Series Data Type

June 9, 2026
7 min read

Metrics, sensor readings, financial ticks — time-series data is everywhere. The RedisTimeSeries module turns Redis into a purpose-built time-series store with built-in downsampling, aggregation, and multi-series querying, keeping your real-time analytics infrastructure simple.

What is RedisTimeSeries?

RedisTimeSeries (part of Redis Stack) adds a native time-series data type. Unlike storing timestamps in sorted sets or hashes, it provides:

  • Compressed storage — timestamp-value pairs stored efficiently
  • Automatic retention — old data deleted after a configurable period
  • Downsampling rules — automatically aggregate raw data into lower-resolution series
  • Labels — key-value metadata for filtering across multiple series

Creating a Time Series

# Create a series for CPU usage with 7-day retention
TS.CREATE metrics:cpu:server1
  RETENTION 604800000
  LABELS host server1 metric cpu region us-east

# Create a series with no retention (keep forever)
TS.CREATE metrics:requests:api LABELS service api env prod

Ingesting Data Points

# TS.ADD key timestamp value
# Use * as timestamp for current time
TS.ADD metrics:cpu:server1 * 72.5
TS.ADD metrics:cpu:server1 * 68.1
TS.ADD metrics:cpu:server1 * 85.3

# Add a specific timestamp (Unix milliseconds)
TS.ADD metrics:cpu:server1 1717934400000 70.2

# Batch add to multiple series
TS.MADD
  metrics:cpu:server1 * 71.0
  metrics:cpu:server2 * 45.2
  metrics:requests:api * 1250

Querying Data

# TS.RANGE: get data points in a time window
TS.RANGE metrics:cpu:server1 -inf +inf         # all data
TS.RANGE metrics:cpu:server1 1717920000000 +inf  # last hour

# With aggregation (average over 5-minute buckets)
TS.RANGE metrics:cpu:server1 -inf +inf
  AGGREGATION avg 300000

# TS.GET: latest value
TS.GET metrics:cpu:server1

# TS.MRANGE: query multiple series by label filter
TS.MRANGE - + FILTER metric=cpu WITHLABELS
  AGGREGATION avg 60000

Automatic Downsampling with Compaction Rules

Raw data at 1-second resolution is expensive to store long-term. Compaction rules automatically aggregate raw data into coarser time buckets.

# Raw series: 1-second resolution, keep 1 day
TS.CREATE metrics:cpu:raw RETENTION 86400000 LABELS host server1 res raw

# 1-minute averages, keep 1 week
TS.CREATE metrics:cpu:1min RETENTION 604800000 LABELS host server1 res 1min

# 1-hour averages, keep 1 year
TS.CREATE metrics:cpu:1hour RETENTION 31536000000 LABELS host server1 res 1hour

# Create compaction rules (source -> destination)
TS.CREATERULE metrics:cpu:raw metrics:cpu:1min AGGREGATION avg 60000
TS.CREATERULE metrics:cpu:raw metrics:cpu:1hour AGGREGATION avg 3600000

# Now every TS.ADD to metrics:cpu:raw automatically
# updates the 1-minute and 1-hour aggregates

Python Integration

import redis
import time

r = redis.Redis(host='localhost', port=6379, decode_responses=True)

def record_metric(series, value, labels=None):
    ts_ms = int(time.time() * 1000)
    r.execute_command('TS.ADD', series, ts_ms, value)

def get_last_hour_avg(series, bucket_seconds=60):
    now_ms = int(time.time() * 1000)
    one_hour_ago = now_ms - 3600000
    result = r.execute_command(
        'TS.RANGE', series,
        one_hour_ago, now_ms,
        'AGGREGATION', 'avg', bucket_seconds * 1000
    )
    return [(ts, float(val)) for ts, val in result]

record_metric("metrics:cpu:server1", 72.5)
data = get_last_hour_avg("metrics:cpu:server1", bucket_seconds=300)

Use Cases

  • Application metrics — request latency, error rates, throughput over time
  • Infrastructure monitoring — CPU, memory, disk I/O per host
  • IoT sensor data — temperature, humidity, energy consumption streams
  • Financial data — OHLC (Open/High/Low/Close) price series per instrument
  • Business KPIs — revenue, conversions, active users over time

Key Takeaways

  • TS.CREATE defines a series with retention and labels for filtering
  • TS.ADD appends a timestamped value — use * for the current time
  • TS.RANGE queries a window with optional aggregation (avg, min, max, sum, count)
  • Compaction rules automatically downsample raw data — keep full resolution short-term, aggregates long-term
  • Labels enable multi-series queries like "average CPU across all servers in us-east"