Available in: Redis Stack
Time complexity: O(M) when M is the amount of compaction rules or O(1) with no compaction
Append a sample to a time series.
TS.ADD key timestamp value [RETENTION retentionPeriod] [ENCODING [COMPRESSED|UNCOMPRESSED]] [CHUNK_SIZE size] [ON_DUPLICATE policy] [LABELS {label value}...]
If the time series does not exist - it will be automatically created.
*
can be used for an automatic timestamp from the server's clock.The following arguments are optional because they can be set by TS.CREATE
:
RETENTION
retentionPeriod - Maximum retention period, compared to maximal existing timestamp (in milliseconds).
Used only if a new time series is created. Ignored When adding samples to an existing time series.
When set to 0, the series is not trimmed. If not specified: set to the global RETENTION_POLICY configuration of the database (which, by default, is 0).
ENCODING
enc - Specify the series samples encoding format. One of the following values:
COMPRESSED
: apply the DoubleDelta compression to the series samples, meaning compression of Delta of Deltas between timestamps and compression of values via XOR encoding.UNCOMPRESSED
: keep the raw samples in memory.Used only if a new time series is created. Ignored When adding samples to an existing time series.
CHUNK_SIZE
size - Memory size, in bytes, allocated for each data chunk. Must be a multiple of 8 in the range [128 .. 1048576].
Used only if a new time series is created. Ignored When adding samples to an existing time series.
If not specified: set to 4096.
ON_DUPLICATE
policy - Overwrite key and database configuration for DUPLICATE_POLICY (policy for handling samples with identical timestamps). One of the following values:
BLOCK
- an error will occur for any out of order sampleFIRST
- ignore any newly reported valueLAST
- override with the newly reported valueMIN
- only override if the value is lower than the existing valueMAX
- only override if the value is higher than the existing valueSUM
- If a previous sample exists, add the new sample to it so that the updated value is equal to (previous + new). If no previous sample exists, set the updated value equal to the new value.LABELS
{label value}... - Set of label-value pairs that represent metadata labels of the time series.
Used only if a new time series is created. Ignored When adding samples to an existing time series.
127.0.0.1:6379>TS.ADD temperature:2:32 1548149180000 26 LABELS sensor_id 2 area_id 32
(integer) 1548149180000
127.0.0.1:6379>TS.ADD temperature:3:11 1548149183000 27 RETENTION 3600
(integer) 1548149183000
127.0.0.1:6379>TS.ADD temperature:3:11 * 30
(integer) 1559718352000
If a compaction rule exits on a time series, TS.ADD
performance might be reduced.
The complexity of TS.ADD
is always O(M) when M is the number of compaction rules or O(1) with no compaction.
RETENTION
, ENCODING
, CHUNK_SIZE
, ON_DUPLICATE
, and LABELS
are optional arguments.RETENTION
and LABELS
introduces additional time complexity.