Implementing Time Series Data

1. Using Sorted Sets for Time Series

SchemaDetail
ScoreUnix timestamp (ms)
MemberUnique value (e.g. ts:payload-id)

2. Storing Timestamped Data

ZADD metric:cpu 1716200000000 "65.4|host1"
ZADD metric:cpu 1716200060000 "67.1|host1"
PatternNote
Member uniquenessInclude ts in member to avoid collisions

3. Querying by Time Range

CommandDescription
ZRANGE key from to BYSCOREInclusive range
ZRANGE key from to BYSCORE LIMIT off cntPaginate

4. Aggregating Time Series Data

AggregationApproach
Sum/avgPull range, compute client-side or Lua
BucketedHINCRBY into metric:hour:<ts>

5. Implementing Downsampling

StrategyDetail
Periodic jobAggregate raw → minute → hour → day
Different keysSeparate retention per resolution

6. Setting Data Retention Policies

MethodDetail
ZREMRANGEBYSCOREDrop entries older than threshold
Per-bucket EXPIREAuto-drop with TTL

7. Using EXPIRE for Auto-Cleanup

PatternDetail
Per-hour keymetric:cpu:2026051912 with TTL 7 days

8. Handling High-Frequency Updates

TechniqueDetail
Pipeline ZADDBatch metric writes
Local buffer + flushReduce server load

9. Using RedisTimeSeries Module

CommandDescription
TS.CREATE key RETENTION ms LABELS k vCreate series
TS.ADD key ts valueInsert sample (use * for now)
TS.RANGE key from to AGGREGATION avg 60000Bucketed query
TS.MRANGE from to FILTER label=valueMulti-series query
TS.CREATERULE src dst AGGREGATION sum bucketMsDownsampling rule

10. Implementing Compaction Strategies

TierResolution / Retention
Hot1s / 1 hour
Warm1min / 7 days
Cold1h / 1 year