Redis Top-K and t-digest keys have no time window, so how do you serve a last-hour trending panel and a last-hour latency percentile from them?
answer
- decay is not a clock
- one key per time bucket
- only one of them merges
- fractions, not percents
basics
~20 sNeither key has a clock: Top-K decay reacts to collisions, not time, and t-digest observations cannot be removed. Use one key per time bucket with EXPIRE. TDIGEST.MERGE combines buckets, but Top-K has no merge command, so stagger its sketches.
solid answer
~40 sNeither structure ages by time. Top-K's `decay` is the chance of lowering a colliding counter, and a t-digest only accumulates until `TDIGEST.RESET`. So both need one key per time bucket, with `EXPIRE` to clean up. For latency, create per-minute digests with `TDIGEST.CREATE`, add with `TDIGEST.ADD`, combine the last 60 with `TDIGEST.MERGE` and read `TDIGEST.QUANTILE key 0.99`, because merging sketches is correct where averaging percentiles is not. Top-K has no merge command, so run two hourly sketches started 30 minutes apart, write to both, and read `TOPK.LIST` from the older one. Size it explicitly: `TOPK.RESERVE key 50` alone gets width 8 and depth 7. Both `TOPK.ADD` and `TDIGEST.ADD` reply with an error on a missing key, so create before writing.
code
plaintext · 17 lines> TOPK.RESERVE bikes:keywords 5 2000 7 0.925
OK
> TOPK.ADD bikes:keywords store seat handlebars handles pedals tires store seat
1) (nil)
2) (nil)
3) (nil)
4) (nil)
5) (nil)
6) "handlebars"
7) (nil)
8) (nil)
> TOPK.LIST bikes:keywords
1) "store"
2) "seat"
3) "pedals"
4) "tires"
5) "handles"go deeper
Recall the commands: TOPK.RESERVE, TOPK.ADD and TOPK.LIST for frequent items, TDIGEST.CREATE, TDIGEST.ADD and TDIGEST.QUANTILE for percentiles, with quantiles written as fractions.
Explain that neither key has a time window, why one key per bucket gives one, and which structure can be merged. Predict the error when writing to a key that has not been created.
Spot the default width of 8 on a large k and a misread decay argument. Merge latency digests across servers instead of averaging, and handle the empty bucket that replies nan.
Decide how stale a panel may be and what a double write is worth, and whether a user-facing trending list needs the exact sorted set at all. Set the bucket size as a shared convention across teams.
## Neither key has a window A product request for the last hour meets two structures with no notion of time. - **Top-K:** the `decay` argument of `TOPK.RESERVE` sounds like ageing, but it is not. It is the probability of decrementing a counter when a *different* item lands in an occupied bucket, computed as `decay ^ counter`. No clock is involved. An item that dominated yesterday stays in the list until newer traffic pushes it out. - **t-digest:** `TDIGEST.ADD` only accumulates. There is no command that removes an observation. `TDIGEST.RESET` clears everything. Both therefore get their window the same way: **one key per time bucket**, with an `EXPIRE` so old buckets disappear. What differs is how you read across buckets. ## The latency percentile report 1. **Create a digest per minute**, with `TDIGEST.CREATE latency:{api}:202611031015 COMPRESSION 100`. `COMPRESSION` trades accuracy against memory: 100 is the default and 1000 is more accurate. 2. **Write observations** with `TDIGEST.ADD key value [value ...]`. It replies `ERR T-Digest: key does not exist` on a missing key, so the create step comes first. 3. **Combine** the last 60 keys with `TDIGEST.MERGE dest 60 key1 ... key60 OVERRIDE`. Without `OVERRIDE`, an existing destination keeps its old values and accumulates the new ones. 4. **Read** with `TDIGEST.QUANTILE dest 0.5 0.9 0.99`. The argument is a fraction: anything outside 0 to 1 replies `ERR T-Digest: quantile should be in [0,1]`. The same step handles several servers. **Averaging per-server p99 values gives a number that is not the p99 of anything**, whereas merging sketches keeps the distribution. In Redis Cluster, `TDIGEST.MERGE` needs all its keys in one hash slot, hence the `{api}` hash tag. The reply is an estimate and can land on either side of the true value. `TDIGEST.QUANTILE` at `0` and `1`, and `TDIGEST.MIN` and `TDIGEST.MAX`, are exact. An empty sketch replies `nan`, so a quiet minute needs handling in the report. The same merged key also serves `TDIGEST.CDF`, the fraction of observations below a value, and `TDIGEST.TRIMMED_MEAN`, a mean that ignores outliers. Each bucket is a separate key, so a day of per-minute digests is 1,440 keys. Choose the coarsest bucket the report can live with. ## The trending panel `TOPK.RESERVE key topk [width depth decay]` takes the last three together or not at all. The defaults, confirmed in the source and on the command page, are **width 8, depth 7, decay 0.9**, and that matters: `TOPK.RESERVE trending 50` gets only 56 buckets to track fifty items. The docs give `width = k*log(k)` and `depth = log(k)` with a minimum of 5 as a starting point, and their example for 50 items is `TOPK.RESERVE topk 50 2000 7 0.925`. Each `TOPK.ADD` replies with one entry per item: `nil`, or the name of an item expelled from the list. That makes it easy to react when the panel changes. `TOPK.LIST` returns up to `k` items sorted by decreasing count estimate, and `WITHCOUNT` adds the counts. `TOPK.QUERY` checks whether an item is on the list. `TOPK.COUNT` is deprecated. There is **no merge command**, so hourly sketches cannot be rolled up into a day, and per-minute sketches cannot make an hour. The practical options are: - **Fixed buckets.** One sketch per hour, reading the last complete one. Simple, but up to an hour stale. - **Staggered sketches.** Two hourly sketches started 30 minutes apart, both written, and read from the older. The panel then reflects between 30 and 60 minutes of traffic, at twice the write cost. - **An exact alternative.** When the catalogue is small, a per-bucket sorted set can be combined with `ZUNIONSTORE`, at far more memory per item. ## Side by side | Concern | Top-K | t-digest | |---|---|---| | First write on a missing key | Error | Error | | Combine buckets | No command | `TDIGEST.MERGE` | | Main read | `TOPK.LIST` | `TDIGEST.QUANTILE` | | Accuracy setting | `width`, `depth`, `decay` | `COMPRESSION` | | How it errs | Items missed or misranked | Value estimated, either side | | Reset | Replace the key | `TDIGEST.RESET` or replace the key | A writer that creates buckets on demand has to tolerate a race: `TDIGEST.CREATE` replies `ERR T-Digest: key already exists` and `TOPK.RESERVE` replies `TopK: key already exists`, and both errors are harmless there.
- Why does a 50-item Top-K created with defaults give a poor panel even when traffic is modest?TOPK.RESERVE with only k gets width 8 and depth 7, so 56 buckets serve every item in the stream. Items keep colliding and decaying each other's counters, so the list becomes unstable and heavy items may fail to hold a place. Pass width, depth and decay explicitly, sized from k.
- How would you answer what share of requests finished within 200 ms from the same digest?Use TDIGEST.CDF on the merged key with 200. It returns the estimated fraction of observations below that value, plus half of those equal to it. That is the inverse of TDIGEST.QUANTILE, which goes from a fraction to a value, and it suits a service-level check against a fixed limit.
- The decay argument of TOPK.RESERVE makes old items fade out after a period of time.
- Averaging each server's p99 gives the p99 across all the servers.
- Hourly Top-K sketches can be merged into a daily one with a Top-K merge command.
- TDIGEST.ADD creates a missing key with the default compression, as BF.ADD does.
- TDIGEST.QUANTILE takes 99 for the 99th percentile.
- TOPK.RESERVE with only k picks a width suited to that k.