content/commands/ts.incrby.md
Increase the value of the sample with the maximum existing timestamp, or create a new sample with a value equal to the value of the sample with the maximum existing timestamp with a given increment
is key name for the time series.
</details> <details open><summary><code>addend</code></summary>is numeric value of the addend (double). An error is returned if the addend is NaN.
</details><note><b>Notes</b>
IGNORE) is defined for this time series - TS.INCRBY operations are affected as well (sample additions/modifications can be filtered).TS.ADD]({{< relref "commands/ts.add/" >}}), [TS.MADD]({{< relref "commands/ts.madd/" >}}), TS.INCRBY, or [TS.DECRBY]({{< relref "commands/ts.decrby/" >}})) may result in inconsistencies between the raw and the compacted data. The compaction process may override such samples.
</note>
is Unix time (integer, in milliseconds) specifying the sample timestamp or * to set the sample timestamp to the Unix time of the server's clock.
Unix time is the number of milliseconds that have elapsed since 00:00:00 UTC on 1 January 1970, the Unix epoch, without adjustments made due to leap seconds.
timestamp must be equal to or higher than the maximum existing timestamp. When equal, the value of the sample with the maximum existing timestamp is increased. If it is higher, a new sample with a timestamp set to timestamp is created, and its value is set to the value of the sample with the maximum existing timestamp plus addend.
If the time series is empty, the value is set to addend.
When not specified, the timestamp is set to the Unix time of the server's clock.
<note><b>NaN Handling (Redis 8.6+):</b> An error is returned if the current value at the maximum existing timestamp is NaN.</note>
</details> <details open><summary><code>RETENTION retentionPeriod</code></summmary>is maximum retention period, compared to the maximum existing timestamp, in milliseconds.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See RETENTION in [TS.CREATE]({{< relref "commands/ts.create/" >}}).
specifies the series sample encoding format.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See ENCODING in [TS.CREATE]({{< relref "commands/ts.create/" >}}).
is memory size, in bytes, allocated for each data chunk.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See CHUNK_SIZE in [TS.CREATE]({{< relref "commands/ts.create/" >}}).
is policy for handling insertion ([TS.ADD]({{< relref "commands/ts.add/" >}}) and [TS.MADD]({{< relref "commands/ts.madd/" >}})) of multiple samples with identical timestamps.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See DUPLICATE_POLICY in [TS.CREATE]({{< relref "commands/ts.create/" >}}).
is the policy for handling duplicate samples. A new sample is considered a duplicate and is ignored if the following conditions are met:
DUPLICATE_POLICY IS LAST;timestamp ≥ max_timestamp);timestamp - max_timestamp) is less than or equal to IGNORE_MAX_TIME_DIFF;abs(value - value_at_max_timestamp) is less than or equal to IGNORE_MAX_VAL_DIFF.where max_timestamp is the timestamp of the sample with the largest timestamp in the time series, and value_at_max_timestamp is the value at max_timestamp.
When not specified: set to the global [IGNORE_MAX_TIME_DIFF]({{< relref "develop/data-types/timeseries/configuration#ignore_max_time_diff-and-ignore_max_val_diff" >}}) and [IGNORE_MAX_VAL_DIFF]({{< relref "develop/data-types/timeseries/configuration#ignore_max_time_diff-and-ignore_max_val_diff" >}}), which are, by default, both set to 0.
These parameters are used when creating a new time series to set the per-key parameters, and are ignored when called with an existing time series (the existing per-key configuration parameters are used).
</details> <details open><summary><code>LABELS [{label value}...]</code></summary>is set of label-value pairs that represent metadata labels of the key and serve as a secondary index.
Use it only if you are creating a new time series. It is ignored if you are adding samples to an existing time series. See LABELS in [TS.CREATE]({{< relref "commands/ts.create/" >}}).
<note><b>Notes</b>
RETENTION, ENCODING, CHUNK_SIZE, DUPLICATE_POLICY, IGNORE, and LABELS are used only when creating a new time series, and ignored when adding or modifying samples in an existing time series.RETENTION and LABELS introduces additional time complexity.
</note>
Suppose you are getting number of orders or total income per minute from several points of sale, and you want to store only the combined value. Call TS.INCRBY for each point-of-sale report.
{{< highlight bash >}} 127.0.0.1:6379> TS.INCRBY a 232 TIMESTAMP 1657811829000 // point-of-sale #1 (integer) 1657811829000 127.0.0.1:6379> TS.INCRBY a 157 TIMESTAMP 1657811829000 // point-of-sale #2 (integer) 1657811829000 127.0.0.1:6379> TS.INCRBY a 432 TIMESTAMP 1657811829000 // point-of-sale #3 (integer) 1657811829000 {{< / highlight >}}
Note that the timestamps must arrive in non-decreasing order.
{{< highlight bash >}} 127.0.0.1:6379> ts.incrby a 100 TIMESTAMP 50 (error) TSDB: timestamp must be equal to or higher than the maximum existing timestamp {{< / highlight >}}
You can achieve similar results without such protection using TS.ADD key timestamp value ON_DUPLICATE sum.
Suppose a sensor ticks whenever a car is passed on a road, and you want to count occurrences. Whenever you get a tick from the sensor you can simply call:
{{< highlight bash >}} 127.0.0.1:6379> TS.INCRBY a 1 (integer) 1658431553109 {{< / highlight >}}
The timestamp is filled automatically.
</details>| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Supported</span> | <span title="Supported">✅ Flexible & Annual</span> <span title="Supported">✅ Free & Fixed</nobr></span> | |
{{< multitabs id="ts-incrby-return-info" tab1="RESP2" tab2="RESP3" >}}
One of the following:
IGNORE in [TS.CREATE]({{< relref "commands/ts.create/" >}})), the reply will be the largest timestamp in the time series.timestamp is not equal to or higher than the maximum existing timestamp.-tab-sep-
One of the following:
IGNORE in [TS.CREATE]({{< relref "commands/ts.create/" >}})), the reply will be the largest timestamp in the time series.timestamp is not equal to or higher than the maximum existing timestamp.{{< /multitabs >}}
[TS.DECRBY]({{< relref "commands/ts.decrby/" >}}) | [TS.CREATE]({{< relref "commands/ts.create/" >}})
[RedisTimeSeries]({{< relref "/develop/data-types/timeseries/" >}})