skip to content

In a Redis time series, what happens when TS.ADD repeats an existing timestamp, and how do DUPLICATE_POLICY and ON_DUPLICATE control the outcome?

level: middleimportance: should knowfreq 21%

answer

  1. the default refuses
  2. six policies, one per outcome
  3. series setting versus one-command override
  4. the reply is not the stored value
  5. identical timestamps only

basics

~10 s

By default the second sample fails: DUPLICATE_POLICY is BLOCK and TS.ADD replies with an error. A series can instead keep FIRST, LAST, MIN, MAX or SUM, and ON_DUPLICATE overrides that policy for one command.

solid answer

~40 s

A Redis time series keeps one value per timestamp, so a repeated timestamp needs a decision. The default `BLOCK` discards the new value and replies with an error. `FIRST` keeps the old value, `LAST` takes the new one, `MIN` and `MAX` keep the lower or higher, and `SUM` adds them. `DUPLICATE_POLICY` sets this per series at creation or through `TS.ALTER`, while `ON_DUPLICATE` on `TS.ADD` overrides it for that one command without changing the series. The order is `ON_DUPLICATE`, then the key's policy, then `ts-duplicate-policy`, then `BLOCK`. Two traps matter: `TS.ADD` replies with the timestamp even when `FIRST` kept the old value, so the reply does not prove your value was stored, and the policy governs only identical timestamps, so an older timestamp with no sample is an ordinary out-of-order insert even under `BLOCK`.

code

plaintext · 11 lines
plaintext
TS.ADD sensor:7:temp 1000 21.4
(integer) 1000
TS.ADD sensor:7:temp 1000 21.6
(error) TSDB: Error at upsert, update is not supported when DUPLICATE_POLICY is set to BLOCK mode, or either current or new value is NaN and DUPLICATE_POLICY is MAX/MIN/SUM
TS.ADD sensor:7:temp 1000 21.6 ON_DUPLICATE MAX
(integer) 1000
TS.ADD sensor:7:temp 1000 20.0 ON_DUPLICATE FIRST
(integer) 1000
TS.RANGE sensor:7:temp 1000 1000
1) 1) (integer) 1000
   2) 21.6

go deeper

for a junior

Remember that the default is BLOCK, so a second write to the same millisecond returns an error, and that the policies are named FIRST, LAST, MIN, MAX and SUM.

for a middle

Explain the precedence from ON_DUPLICATE down to BLOCK, and be precise that the policy concerns identical timestamps only, while an older unseen timestamp is simply an out-of-order insert.

for a senior

Pick the policy from the failure you expect. Retries call for FIRST or LAST, counters from several reporters call for SUM, and clients must not read the TS.ADD reply as proof of the stored value.

for a principal

Set the fleet's default deliberately through ts-duplicate-policy or at series creation, because this choice decides whether a retry storm shows up as errors, as silent drops, or as corrupted values.

## Why a policy exists A Redis time series holds one value per timestamp on each key. Real ingestion produces repeats: a gateway retries a write whose reply it lost, two sources report for the same device, or a client using `*` writes twice inside one millisecond. Redis has to decide what the second write means, and the **duplicate policy** is that decision. ## The six policies | Policy | Result when the timestamp already holds a sample | Typical use | |---|---|---| | `BLOCK` | New value discarded, error reply | Default; catches accidental rewrites | | `FIRST` | New value discarded, timestamp reply | Duplicate reports are possible and harmless | | `LAST` | New value replaces the old one | Corrections are being reported | | `MIN` | Lower of the two is kept | Lowest value per timestamp | | `MAX` | Higher of the two is kept | Highest value per timestamp | | `SUM` | Old plus new is stored | Counters fed by several reporters | For a temperature fleet with retries, `FIRST` or `LAST` both make a retry harmless, because the resent value equals the original. `SUM` would double the reading on every retry, so it belongs to counters whose reporters each send a different share of the count. `BLOCK` is safe but noisy, because a retry surfaces as an error your client must recognise. ## Where the policy comes from The policy in force for one write is resolved in this order: 1. `ON_DUPLICATE` on that `TS.ADD`, for that single command only. 2. The key's own policy, set by `TS.CREATE` or `TS.ALTER` with `DUPLICATE_POLICY`. 3. The `ts-duplicate-policy` configuration parameter. 4. The hard-coded default, `BLOCK`. The spelling matters. `TS.ADD` accepts `DUPLICATE_POLICY` too, but only to set the policy of a series that this write creates, and it is ignored on an existing key. `ON_DUPLICATE` is the reverse: it has no effect when the write creates the series. `TS.MADD` takes no options at all, so a batch always runs under the key's policy or the configured default. ## What the reply tells you `TS.ADD` replies with the timestamp of the upserted sample. It does **not** tell you which value ended up stored. - Under `FIRST`, the reply is the timestamp although your value was thrown away. - Under `MIN` or `MAX`, the reply is the same whether your value won or lost. - Under `BLOCK`, you get an error and nothing changes. To learn the stored value, read it back with `TS.RANGE key ts ts`. The `TS.MADD` reply is per sample, so a batch retried under `BLOCK` returns an array in which repeated timestamps show as error entries and new ones as timestamps. Treat that as a partial success rather than a failed batch. ## What the policy does not govern The policy applies **only when the timestamp is identical**. A new timestamp older than the newest sample, with nothing stored at that exact millisecond, is an out-of-order insert. `BLOCK` does not stop it; the source inserts it into the right chunk and only consults the policy on an exact match. The check that can reject such a write is retention, which fails it with `TSDB: Timestamp is older than retention`. Compaction destinations do not follow this setting either. When a rule writes a bucket into its destination series, it does so with `LAST` semantics, whatever policy that series declares. ## Near-duplicates and IGNORE A separate mechanism drops readings that are nearly the same as the previous one. `TS.CREATE` takes `IGNORE ignoreMaxTimediff ignoreMaxValDiff`. A new sample is discarded when all of these hold: - the series is not a compaction destination - the policy is `LAST` - the sample is in order, at or after the newest timestamp - its timestamp is within `ignoreMaxTimediff` of the newest - its value is within `ignoreMaxValDiff` of the newest sample's value When that happens, `TS.ADD` replies with the series' largest timestamp rather than the one you sent. Comparing the two is how a client detects the drop, since they match only when your timestamp already was the largest. Both thresholds default to 0, and `TS.MADD` applies the per-key values without accepting them as arguments. ## NaN values Since Redis 8.6 a sample's value can be `NaN`. With `MIN`, `MAX` or `SUM`, a repeated timestamp returns an error when exactly one of the old and new values is `NaN`. `NaN` values are also never treated as duplicates by `IGNORE`.

  • How would you make a Redis TS.ADD retry safe for temperature readings?
    Set `DUPLICATE_POLICY FIRST` or `LAST` on the series, so resending the same timestamp and value changes nothing and replies with the timestamp. Avoid `SUM`, which doubles the reading, and expect `BLOCK` to return an error on every retry. The key requirement is that the client reuses the original timestamp instead of sending `*`, because a fresh server timestamp makes the retry a new sample.
  • How can a client tell that IGNORE dropped its Redis time series sample?
    Compare the reply with the timestamp you sent. When the sample is ignored, `TS.ADD` replies with the largest timestamp in the series, which equals yours only if yours was already the largest. This only applies to a series whose policy is `LAST` with non-zero `IGNORE` thresholds, and it happens without any error reply, so a client that never compares will not notice.

  • A repeated timestamp simply overwrites the old sample by default.
  • BLOCK rejects any sample older than the newest one in the series.
  • A timestamp reply from TS.ADD proves your value is the one stored.
  • ON_DUPLICATE on a TS.ADD permanently changes that series' duplicate policy.
  • IGNORE filters near-duplicate samples whatever the series' duplicate policy is.
  • SUM is a safe choice for temperature readings that might be retried.

Assumes Redis 8.10; NaN values and their MIN, MAX and SUM duplicate errors arrived in 8.6, and the ts-duplicate-policy parameter name in 8.0.