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
    • bucketDuration - duration of each bucket, in milliseconds
    • alignTimestamp - alignment of the compacted buckets start times

    The alignment of time buckets is 0.

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