Redis Timeseries

Use Case

Redis timeseries - introduction
Ticker Plant

Redis Timeseries is a module in Redis for timeseries storage, accumulation and query.
Redis Timeseries can be used as a ticker plant for financial data.

Tools Used

  • Redis Insight

Video explanation

Redis Introduction

Commands

CUD (Create, Update, Delete a datapoint)

CommandDescription
TS.CREATE sensor1Creates a timeseries
TS.CREATE sensor1 RETENTION 2678400000Creates a timeseries with retention of 1 month (time is in milliseconds)
TS.ADD sensor1 1626434637914 26Adds a data point to the timeseries with timestamp 1626434637914
TS.ADD sensor1 * 26Adds a data point to the timeseries with the latest timestamp
TS.MADD sensor1 1695508396327 27 sensor1 1695508396328 28Adds multiple data points to the timeseries
TS.DEL sensor1 1000 2000Delete the datapoint from 1000 to 2000 timestamps
TS.DEL sensor1 1000 1000Delete the datapoint with 1000 timestamps

Labels to group the timeseries

CommandDescription
TS.CREATE sensor1 LABELS region eastLabels are key-value metadata we attach to data points, allowing us to group and filter. They can be either string or numeric values and are added to a timeseries on creation

Compaction

CommandDescription
TS.CREATE sensor1_compactedCreate the destination timeseries first
TS.CREATERULE sensor1 sensor1_compacted AGGREGATION avg 60000Create the rule - Supported aggregation types: avg, sum, min, max, range, count, first, last, std.p, std.s, var.p, var.s and twa.

Filtering or querying the data

CommandDescription
TS.MRANGE - + FILTER area_id=32Create the destination timeseries first
TS.MGET FILTER area_id=32Get last sample that matches the label
TS.RANGE sensor1 - + FILTER_BY_VALUE 25 30Filter by value
TS.MRANGE - + FILTER_BY_VALUE 20 30 FILTER region=eastFilter by value and a label
TS.MRANGE - + FILTER_BY_VALUE 20 30 FILTER region=eastFilter by value and a label
TS.RANGE sensor1 - + FILTER_BY_TS 1626435230501 1626443276598Filter by timestamp
TS.MRANGE - + FILTER_BY_TS 1626435230501 1626443276598 FILTER region=eastFilter multiple timeseries by timestamp

Aggregation

CommandDescription
TS.RANGE sensor1 - + + AGGREGATION avg 3600000Aggregates (avg) per hour
TS.MRANGE - + AGGREGATION avg 3600000 FILTER area_id=32Aggregates (avg) per hour by label
TS.RANGE sensor1 10 70 + AGGREGATION min 25Aggregate (min) from timestamp 10 to 20 every 25 milliseconds. Start aggregation from timestamp 0 milliseconds
TS.RANGE sensor1 10 70 + AGGREGATION min 25 ALIGN startAggregate (min) from timestamp 10 to 20 every 25 milliseconds. Start aggregation from timestamp 10 milliseconds
TS.MRANGE - + FILTER region=(east,west) GROUPBY region REDUCE minAggregate accros timestamp, group by region and get the min in each region

Data Visualization

References

https://redis.io/docs/data-types/timeseries/
https://redis.io/docs/data-types/timeseries/quickstart/

comments powered by Disqus