{
  "id": "ts.range",
  "title": "TS.RANGE",
  "url": "https://redis.io/docs/latest/commands/ts.range/",
  "summary": "Query a range in forward direction",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-04-09T10:29:34-04:00",
  "page_type": "content",
  "content_hash": "b843f47e595b63957f0873b257082a0676428a134758929d088e8fff34ed921c",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Query a range in forward direction. Starting from Redis 8.6, NaN values are included in raw measurement reports (queries without aggregation).\n\n[Examples](#examples)"
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open>\n<summary><code>key</code></summary> \n\nis the key name for the time series.\n</details>\n\n<details open>\n<summary><code>fromTimestamp</code></summary> \n\nis start timestamp for the range query (integer Unix timestamp in milliseconds) or `-` to denote the timestamp of the earliest sample in the time series.\n</details>\n\n<details open>\n<summary><code>toTimestamp</code></summary> \n\nis end timestamp for the range query (integer Unix timestamp in milliseconds) or `+` to denote the timestamp of the latest sample in the time series.\n\n<note><b>Note:</b>    When the time series is a compaction, the last compacted value may aggregate raw values with timestamp beyond `toTimestamp`. That is because `toTimestamp` only limits the timestamp of the compacted value, which is the start time of the raw bucket that was compacted.</note>\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "<details open>\n<summary><code>LATEST</code> (since RedisTimeSeries 1.8)</summary>\n\nis used when a time series is a compaction. With `LATEST`, TS.RANGE also reports the compacted value of the latest, possibly partial, bucket, given that this bucket's start time falls within `[fromTimestamp, toTimestamp]`. Without `LATEST`, TS.RANGE does not report the latest, possibly partial, bucket. When a time series is not a compaction, `LATEST` is ignored.\n  \nThe data in the latest bucket of a compaction is possibly partial. A bucket is _closed_ and compacted only upon arrival of a new sample that _opens_ a new _latest_ bucket. There are cases, however, when the compacted value of the latest, possibly partial, bucket is also required. In such a case, use `LATEST`.\n</details>\n\n<details open>\n<summary><code>FILTER_BY_TS ts...</code> (since RedisTimeSeries 1.6)</summary> \n\nfilters samples by a list of specific timestamps. A sample passes the filter if its exact timestamp is specified and falls within `[fromTimestamp, toTimestamp]`.\n\nWhen used together with `AGGREGATION`: samples are filtered before being aggregated.\n</details>\n\n<details open>\n<summary><code>FILTER_BY_VALUE min max</code> (since RedisTimeSeries 1.6)</summary> \n\nfilters samples by minimum and maximum values. `min` and `max` cannot be NaN values.\n\nWhen used together with `AGGREGATION`: samples are filtered before being aggregated.\n</details>\n\n<details open>\n<summary><code>COUNT count</code></summary> \n\nWhen used without `AGGREGATION`: limits the number of reported samples.\n\nWhen used together with `AGGREGATION`: limits the number of reported buckets.\n</details>\n\n<details open>\n<summary><code>ALIGN align</code> (since RedisTimeSeries 1.6)</summary> \n\nis a time bucket alignment control for `AGGREGATION`. It controls the time bucket timestamps by changing the reference timestamp on which a bucket is defined.\n\n`align` values include:\n   \n - `start` or `-`: The reference timestamp will be the query start interval time (`fromTimestamp`) which can't be `-`\n - `end` or `+`: The reference timestamp will be the query end interval time (`toTimestamp`) which can't be `+`\n - A specific timestamp: align the reference timestamp to a specific time\n   \n<note><b>Note:</b> When not provided, alignment is set to `0`.</note>\n</details>\n\n<details open>\n<summary><code>AGGREGATION aggregator bucketDuration</code></summary> \n\naggregates samples into time buckets, where:\n\n  - `aggregator` takes one of the following aggregation types:\n\n    | `aggregator` | Description                                                     |\n    | ------------ | --------------------------------------------------------------- |\n    | `avg`        | Arithmetic mean of all non-NaN values                           |\n    | `sum`        | Sum of all non-NaN values                                       |\n    | `min`        | Minimum non-NaN value                                           |\n    | `max`        | Maximum non-NaN value                                           |\n    | `range`      | Difference between the maximum and the minimum non-NaN values   |\n    | `count`      | Number of non-NaN values                                        |\n    | `countNaN`   | Number of NaN values (since Redis 8.6)                          |\n    | `countAll`   | Number of values, including NaN and non-NaN (since Redis 8.6)   |\n    | `first`      | The non-NaN value with the lowest timestamp in the bucket       |\n    | `last`       | The non-NaN value with the highest timestamp in the bucket      |\n    | `std.p`      | Population standard deviation of the non-NaN values             |\n    | `std.s`      | Sample standard deviation of the non-NaN values                 |\n    | `var.p`      | Population variance of the non-NaN values                       |\n    | `var.s`      | Sample variance of the non-NaN values                           |\n    | `twa`        | Time-weighted average over the bucket's timeframe (ignores NaN values) (since RedisTimeSeries 1.8) |\n\n  - `bucketDuration` is duration of each bucket, in milliseconds.\n  \n  Without `ALIGN`, bucket start times are multiples of `bucketDuration`.\n  \n  With `ALIGN align`, bucket start times are multiples of `bucketDuration` with remainder `align % bucketDuration`.\n  \n  The first bucket start time is less than or equal to `fromTimestamp`.\n</details>\n\n<details open>\n<summary><code>[BUCKETTIMESTAMP bt]</code> (since RedisTimeSeries 1.8)</summary> \n\ncontrols how bucket timestamps are reported.\n\n| `bt`             | Timestamp reported for each bucket                            |\n| ---------------- | ------------------------------------------------------------- |\n| `-` or `start`   | the bucket's start time (default)                             |\n| `+` or `end`     | the bucket's end time                                         |\n| `~` or `mid`     | the bucket's mid time (rounded down if not an integer)        |\n</details>\n\n<details open>\n<summary><code>[EMPTY]</code> (since RedisTimeSeries 1.8)</summary> \n\nis a flag, which, when specified, reports aggregations also for empty buckets.\n\n| `aggregator`         | Value reported for each empty bucket |\n| -------------------- | ------------------------------------ |\n| `sum`, `count`       | `0`                                  |\n| `last`               | The value of the last sample before the bucket's start. `NaN` when no such sample. |\n| `twa`                | Average value over the bucket's timeframe based on linear interpolation of the last sample before the bucket's start and the first sample after the bucket's end. `NaN` when no such samples. |\n| `min`, `max`, `range`, `avg`, `first`, `std.p`, `std.s` | `NaN` |\n\nRegardless of the values of `fromTimestamp` and `toTimestamp`, no data is reported for buckets that end before the earliest sample or begin after the latest sample in the time series.\n</details>"
    },
    {
      "id": "complexity",
      "title": "Complexity",
      "role": "performance",
      "text": "TS.RANGE complexity can be improved in the future by using binary search to find the start of the range, which makes this `O(Log(n/m)+k*m)`.\nBut, because `m` is small, you can disregard it and look at the operation as `O(Log(n)+k)`."
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "<details open><summary><b>Filter results by timestamp or sample value</b></summary>\n\nConsider a metric where acceptable values are between -100 and 100, and the value 9999 is used as an indication of bad measurement.\n\n\n127.0.0.1:6379> TS.CREATE temp:TLV LABELS type temp location TLV\nOK\n127.0.0.1:6379> TS.MADD temp:TLV 1000 30 temp:TLV 1010 35 temp:TLV 1020 9999 temp:TLV 1030 40\n1) (integer) 1000\n2) (integer) 1010\n3) (integer) 1020\n4) (integer) 1030\n\n\nNow, retrieve all values except out-of-range values.\n\n\nTS.RANGE temp:TLV - + FILTER_BY_VALUE -100 100\n1) 1) (integer) 1000\n   2) 30\n2) 1) (integer) 1010\n   2) 35\n3) 1) (integer) 1030\n   2) 40\n\n\nNow, retrieve the average value, while ignoring out-of-range values.\n\n\nTS.RANGE temp:TLV - + FILTER_BY_VALUE -100 100 AGGREGATION avg 1000\n1) 1) (integer) 1000\n   2) 35\n\n</details>\n\n<details open><summary><b>Align aggregation buckets</b></summary>\n\nTo demonstrate alignment, let’s create a stock and add prices at nine different timestamps.\n\n\n127.0.0.1:6379> TS.CREATE stock:A LABELS type stock name A\nOK\n127.0.0.1:6379> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120\n1) (integer) 1000\n2) (integer) 1010\n3) (integer) 1020\n127.0.0.1:6379> TS.MADD stock:A 2000 200 stock:A 2010 210 stock:A 2020 220\n1) (integer) 2000\n2) (integer) 2010\n3) (integer) 2020\n127.0.0.1:6379> TS.MADD stock:A 3000 300 stock:A 3010 310 stock:A 3020 320\n1) (integer) 3000\n2) (integer) 3010\n3) (integer) 3020\n\n\nNext, aggregate without using `ALIGN`, defaulting to alignment 0.\n\n\n127.0.0.1:6379> TS.RANGE stock:A - + AGGREGATION min 20\n1) 1) (integer) 1000\n   2) 100\n2) 1) (integer) 1020\n   2) 120\n3) 1) (integer) 2000\n   2) 200\n4) 1) (integer) 2020\n   2) 220\n5) 1) (integer) 3000\n   2) 300\n6) 1) (integer) 3020\n   2) 320\n\n\nAnd now set `ALIGN` to 10 to have a bucket start at time 10, and align all the buckets with a 20 milliseconds duration.\n\n\n127.0.0.1:6379> TS.RANGE stock:A - + ALIGN 10 AGGREGATION min 20\n1) 1) (integer) 990\n   2) 100\n2) 1) (integer) 1010\n   2) 110\n3) 1) (integer) 1990\n   2) 200\n4) 1) (integer) 2010\n   2) 210\n5) 1) (integer) 2990\n   2) 300\n6) 1) (integer) 3010\n   2) 310\n\n\nWhen the start timestamp for the range query is explicitly stated (not `-`), you can set `ALIGN` to that time by setting align to `-` or to `start`.\n\n\n127.0.0.1:6379> TS.RANGE stock:A 5 + ALIGN - AGGREGATION min 20\n1) 1) (integer) 985\n   2) 100\n2) 1) (integer) 1005\n   2) 110\n3) 1) (integer) 1985\n   2) 200\n4) 1) (integer) 2005\n   2) 210\n5) 1) (integer) 2985\n   2) 300\n6) 1) (integer) 3005\n   2) 310\n\n\nSimilarly, when the end timestamp for the range query is explicitly stated, you can set `ALIGN` to that time by setting align to `+` or to `end`.\n</details>"
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Supported</span><br /> | <span title=\"Supported\">&#x2705; Flexible & Annual</span><br /><span title=\"Supported\">&#x2705; Free & Fixed</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Array reply]() of ([Integer reply](), [Simple string reply]()) pairs representing (timestamp, value).\n* [Simple error reply]() in these cases: invalid filter value, wrong key type, key does not exist, etc.\n\n**RESP3:**\n\nOne of the following:\n* [Array reply]() of ([Integer reply](), [Double reply]()) pairs representing (timestamp, value).\n* [Simple error reply]() in these cases: invalid filter value, wrong key type, key does not exist, etc."
    },
    {
      "id": "see-also",
      "title": "See also",
      "role": "related",
      "text": "[`TS.MRANGE`]() | [`TS.REVRANGE`]() | [`TS.MREVRANGE`]()"
    },
    {
      "id": "related-topics",
      "title": "Related topics",
      "role": "related",
      "text": "[RedisTimeSeries]()"
    }
  ],
  "examples": []
}
