TS.CREATERULE sourceKey destKey AGGREGATION AVG | FIRST | LAST | MIN | MAX | SUM | RANGE | COUNT | STD.P | STD.S | VAR.P | VAR.S | TWA bucketDuration [alignTimestamp]

Available in: Redis Stack

Time complexity: O(1)


Create a compaction rule.

TS.CREATERULE sourceKey destKey AGGREGATION aggregator bucketDuration [alignTimestamp]
  • sourceKey - Key name for source time series

  • destKey - Key name for destination (compacted) time series

  • AGGREGATION aggregator bucketDuration

    Aggregate results into time buckets.

    • aggregator - Aggregation type: One of the following:

      avgarithmetic mean of all values
      sumsum of all values
      minminimum value
      maxmaximum value
      rangedifference between the highest and the lowest value
      countnumber of values
      firstthe value with the lowest timestamp in the bucket
      lastthe value with the highest timestamp in the bucket
      std.ppopulation standard deviation of the values
      std.ssample standard deviation of the values
      var.ppopulation variance of the values
      var.ssample variance of the values
      twatime-weighted average of all values (since RedisTimeSeries v1.8)
    • bucketDuration - Duration of each bucket, in milliseconds

    • alignTimestamp (since RedisTimeSeries v1.8)

      Assure that there is a bucket that starts at exactly alignTimestamp and align all other buckets accordingly. Units: milliseconds. Default value: 0 (aligned with the epoch). Example: if bucketDuration is 24 hours (24 * 3600 * 1000), setting alignTimestamp to 6 hours after the epoch (6 * 3600 * 1000) will ensure that each bucket’s timeframe is [06:00 .. 06:00).

destKey should be of a timeseries type, and should be created before TS.CREATERULE is called.


  • Calling TS.CREATERULE with a nonempty destKey can result in an undefined behavior
  • Samples should not be explicitly added to destKey
  • Only new samples that are added into the source series after the creation of the rule will be aggregated