skip to content

Devices in a Redis time series fleet upload buffered readings after hours offline; ingest slows, some writes fail and hourly rollups look wrong. How do you diagnose and fix it?

level: seniorimportance: should knowfreq 17%

answer

  1. behind the series' own newest sample
  2. a compressed chunk rewritten per insert
  3. each rule rescans one bucket
  4. partly trimmed buckets get overwritten
  5. one bad clock moves the window

basics

~20 s

Late samples take the upsert path: each rewrites a compressed chunk and each rule recomputes its bucket. Samples beyond retention are rejected, and partly trimmed buckets get overwritten. Replay oldest first and keep raw retention above lateness plus bucket.

solid answer

~50 s

A sample is out of order only when it is behind that series' own newest timestamp. Redis then decodes and rewrites the whole compressed chunk, and every compaction rule recomputes its bucket from raw samples, so ingest slows. Anything further behind than `RETENTION` fails with `TSDB: Timestamp is older than retention`, so that data reaches neither raw nor rollups. Rollups go wrong at the edge: when a late sample lands in a bucket whose raw samples are partly trimmed, Redis recalculates from what survives and overwrites the good value. I would start with `TS.INFO` for `lastTimestamp`, since one bad device clock in the future makes every honest write fail. The fixes are replaying each device oldest first, before live samples resume, keeping raw retention at least the maximum lateness plus the largest bucket, using a smaller `CHUNK_SIZE` for series that take heavy backfill, and rejecting future timestamps at the gateway.

code

plaintext · 21 lines
plaintext
# RETENTION is 24 hours and the newest sample is at 1760000000000

# 30 hours behind the newest sample
TS.ADD temp:{dev7} 1759892000000 20.9
(error) TSDB: Timestamp is older than retention

# 6 hours behind: accepted as an out-of-order insert
TS.ADD temp:{dev7} 1759978400000 21.0
(integer) 1759978400000

# one bad device clock, far in the future
TS.ADD temp:{dev7} 2076000000000 21.4
(integer) 2076000000000
TS.ADD temp:{dev7} 1760000060000 21.5
(error) TSDB: Timestamp is older than retention

# remove the bad sample and the series accepts writes again
TS.DEL temp:{dev7} 2076000000000 2076000000000
(integer) 1
TS.ADD temp:{dev7} 1760000060000 21.5
(integer) 1760000060000

go deeper

for a junior

Recall that samples can be inserted behind the newest one, that this costs more than appending, and that a sample older than the retention window is refused with an error.

for a middle

Explain the mechanism: a compressed chunk is decoded and rewritten for each out-of-order sample, and the compaction bucket it falls in is recalculated from raw samples.

for a senior

Diagnose by symptom: read lastTimestamp first, split errors by message, compare rollups to raw, then fix replay order and size retention as maximum lateness plus the largest bucket.

for a principal

Decide who owns lateness. Write a maximum into the ingestion contract, enforce it at the gateway, and derive retention from it, so backfill stops being an incident each time a site reconnects.

## What counts as out of order Every Redis time series tracks its own **highest timestamp**. A write beyond it is an append. Anything at or behind it takes a separate **upsert** path. This is judged per series, not against the clock, and that has a useful consequence: a device that was offline for six hours and replays its buffer **oldest first** is still appending, because its own newest sample is six hours old. The expensive case appears when live samples resume first and the backlog follows, since then every replayed sample is behind the series' newest. ## Why ingest slows Two costs stack up on each out-of-order sample. **The chunk is rewritten.** With `COMPRESSED` encoding, the source decodes the whole chunk and re-encodes it with the new sample inserted. A chunk that has grown past 1.2 times `CHUNK_SIZE` is also split. The docs' benchmark, run on RedisTimeSeries 1.4, gives the shape of the damage: | Out-of-order share | Compressed chunks | Uncompressed chunks | |---|---|---| | 1% | About 10% lower ingest at 4096 bytes | Low or unmeasurable | | 5% to 25% | 35% to 75% fewer ops/sec | 5% to 19% fewer ops/sec | | 99% | Up to 95% drop | Up to 45% drop | The docs' advice for compressed series is to reduce `CHUNK_SIZE`, which they say can cut the impact in half at the extreme, at the price of a lower compression ratio. Chunk size does not change the uncompressed result. **Each rule recomputes a bucket.** On that same path, the source recalculates the affected bucket for every compaction rule by scanning its raw samples. A one-hour rule at one sample a second rescans up to 3,600 samples per late write, and every other rule adds its own bucket. The benchmark page does not mention rules, so expect worse than its numbers if the series has several. ## Why some writes fail The client should separate two different errors. - `TSDB: Timestamp is older than retention` means the sample is further behind the newest timestamp than `RETENTION` allows. It is refused before any write, so neither raw nor rollups change. - `TSDB: Error at upsert...` means the timestamp already holds a sample under `BLOCK`, or a `NaN` conflict under `MIN`, `MAX` or `SUM`. The first usually indicates a retried batch rather than lateness. Both appear per sample inside a `TS.MADD` reply, so a batch can partly succeed. ## Why rollups look wrong Compaction buckets are not sealed by time. The docs define three cases for a sample landing in an older bucket: 1. **All raw samples for the bucket survive:** the compacted value is recalculated correctly. 2. **Only part survives:** it is recalculated from the new sample plus what remains, and that replaces the old value. 3. **None survive:** the bucket is not updated. Case 2 is the one that hurts. The earlier value was computed from the whole bucket, and it is now overwritten by a value computed from a fragment. A daily rollup with one day of raw retention is exposed whenever a late sample lands in yesterday's bucket, because part of yesterday has already passed out of retention by then. The arithmetic that prevents it is simple. If samples can arrive up to L late and the largest rollup bucket is D, then keeping raw retention of at least L + D means a late sample's bucket is still whole. With a 6-hour outage limit, 24 hours of raw retention safely serves one-hour rollups but not daily ones, which would need 30 hours. ## The poisoned clock One sample with a far-future timestamp is a different failure. It is a legal append, so it moves the series' highest timestamp. From then on, reads start at that value minus retention, so older data disappears from `TS.RANGE`, and every honest write fails as older than retention. `TS.DEL` on exactly that timestamp moves the maximum back and the old window becomes readable again, unless a further far-future append has opened a new chunk, which is when old chunks are actually freed. Prevention belongs at the gateway: refuse timestamps ahead of its own clock. ## A diagnosis sequence 1. Run `TS.INFO` on an affected series. Check `lastTimestamp` for a future value, then `retentionTime`, `chunkType`, `chunkSize` and `rules`. 2. Group client errors by message, since `INFO errorstats` alone will not say which device. 3. Compare a rollup with `TS.RANGE` on raw using the same aggregator and bucket, over a window still inside raw retention. 4. Measure at the gateway what share of samples are behind each device's newest. Then fix in order of leverage: replay order first, retention second, chunk settings third. `ENCODING` cannot be changed later through `TS.ALTER`, and a new `CHUNK_SIZE` affects only new chunks.

  • Why does replaying a Redis time series backlog oldest first help so much?
    Out-of-order means behind that series' own newest sample. If the device's newest stored sample is from before the outage, an oldest-first replay is a run of plain appends with no chunk rewrite and no bucket recomputation. To keep that, hold live samples back until the backlog has drained, because a single live sample first would push all the replayed data behind the newest.
  • Can you repair a Redis rollup bucket that a partly trimmed recalculation overwrote?
    Not from raw, because those raw samples are gone. If another source of the data exists, you can recompute the value and write it to the destination with `TS.ADD`, but the docs warn that direct writes to a compaction may be inconsistent and may be overridden. The practical answer is preventing it by keeping retention at least lateness plus the largest bucket.

  • BLOCK stops any sample that is older than the newest one in the series.
  • A sample older than RETENTION is dropped silently with a normal reply.
  • Once a later bucket starts, earlier compaction buckets can no longer change.
  • A late sample always leaves the rollup more accurate than it was.
  • Larger chunks speed up out-of-order inserts because more samples fit per chunk.
  • A far-future timestamp is harmless because retention follows the server's clock.