Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

A content site tracks unique visitors per article per day with one HyperLogLog per article per day, across about 2 million articles. After four months Redis memory has reached 90 GB, and the weekly unique-visitor figures in reports look too high.

Explain both problems and fix them.

Memory grew because every popular article's daily HyperLogLog turns dense at about 12 KB once it has a few thousand visitors, and with no TTLs four months of daily keys are still there. The weekly figures are too high because they add seven daily counts, which counts a returning visitor once per day. We fix the first with TTLs, rollups by PFMERGE and a traffic threshold, and the second by computing weeks with PFCOUNT over the seven daily keys or a PFMERGE of them.

Why this question is asked

This is two bugs that look like one, and they are typical of HyperLogLog in production. One is about how the structure is stored, the other about how it is used, and both come from treating a HyperLogLog like a simple counter. Interviewers like it because it checks whether we understand the sparse and dense encodings and the difference between a union and a sum. In this answer, we will measure where the 90 GB goes, explain the encodings that make popular articles expensive, bound the memory with TTLs and rollups, fix the weekly numbers, and set expectations about the error so nobody chases noise.

1Measuring Where the Memory Goes

Before changing anything, we sample. We SCAN for the per-article keys (never KEYS, which blocks the server on millions of keys), run MEMORY USAGE on a few thousand of them, and group the results by size and age. Two populations appear: most keys are a few hundred bytes to about 3 KB, and a smaller group sits at about 12 KB.

Text
SCAN 0 MATCH uv:article:* COUNT 1000 # returns a cursor and a batch of keys
MEMORY USAGE uv:article:88121:2026-11-11 # (integer) 12360: dense, a popular article
MEMORY USAGE uv:article:403377:2026-11-11 # (integer) 712: sparse, a quiet article
OBJECT ENCODING uv:article:88121:2026-11-11 # "raw": every HyperLogLog is a string
TTL uv:article:88121:2026-07-01 # (integer) -1: no expiry, four months old
CONFIG GET hll-sparse-max-bytes # 1) "hll-sparse-max-bytes" 2) "3000"

The TTL of -1 is the first finding: no key ever expires. The arithmetic is then simple. 90 GB over about 120 days is roughly 750 MB of new keys a day. If, say, 30,000 articles a day cross into dense form at 12 KB, that is 360 MB, and a few hundred thousand quieter articles at around 1 KB each add the rest. Every day adds the same again, forever.

2Why Popular Articles Cost 12 KB a Day

A HyperLogLog has 16,384 registers of 6 bits. Redis starts each key in a sparse encoding that stores long runs of empty registers in a byte or two, so a key with a few hundred visitors takes a few hundred bytes. As more visitors arrive, more registers are touched and the sparse form grows. When it would exceed hll-sparse-max-bytes, 3000 bytes by default, Redis converts the key to the dense encoding, a flat 12 KB, and it never goes back. That point is reached somewhere in the low thousands of distinct visitors.

So the cost is per key, not per visitor. A popular article costs 12 KB every day whether it had 5,000 or 5 million visitors, and one key per article per day multiplies that by thousands of articles and every day kept. Someone may also have raised hll-sparse-max-bytes, which keeps mid-sized keys smaller at the cost of CPU, because sparse updates are slower; the CONFIG GET above shows the default, so that is not the cause here.

3Bounding the Memory: TTLs, Rollups and a Threshold

First, every daily key gets a TTL when it is created, for example eight days, so daily detail exists for the last week only. We set it in the same pipeline as the PFADD, with EXPIRE key 691200 NX (the NX option, from Redis 7, sets a TTL only if there is none, so we do not keep pushing it forward).

Second, we roll up. A nightly job merges the seven daily keys of each article into a weekly key with PFMERGE, and weekly keys into monthly keys, each level with its own TTL. A merged HyperLogLog is exactly what we would have built by adding every visitor directly, so rollups lose no accuracy.

Third, we ask whether the long tail needs per-article uniques at all. If reports only use the top articles, we keep per-article keys only for articles above a traffic threshold, and count the rest in one shared key. Finally, the existing four months of keys need a cleanup job that SCANs and either sets TTLs or deletes with UNLINK, which frees memory in the background.

Text
# on each visit, in one pipeline
PFADD uv:article:88121:2026-11-12 visitor:7781
EXPIRE uv:article:88121:2026-11-12 691200 NX # 8 days, set once
# nightly rollup
PFMERGE uv:article:88121:2026-W46 uv:article:88121:2026-11-09 ... uv:article:88121:2026-11-15
EXPIRE uv:article:88121:2026-W46 3024000 NX # 5 weeks
Step throughRollup tiers for one article
  1. Daily keysPFADD on each visit, TTL 8 days
  2. Weekly keysPFMERGE of 7 daily keys, TTL 5 weeks
  3. Monthly keysPFMERGE of the month's days, TTL 13 months
  4. Archiveolder months exported as raw bytes to object storage

4 layers, from the top down. Step through them one at a time.

4Why the Weekly Figures Are Too High

The weekly report took PFCOUNT of each day and added the seven numbers. A reader who visits an article every day is one weekly unique visitor but appears in all seven daily counts. So the sum is really 'visitor-days', and it overstates weekly uniques by however often people return, which for loyal audiences can be two or three times.

The correct weekly figure is the count of the union. PFCOUNT accepts several keys and estimates their union without storing it, and PFMERGE stores the union for repeated use. Both give the same answer because merging takes the maximum of each register.

Text
PFCOUNT uv:article:88121:2026-11-09 # (integer) 41022 each day...
# sum of the seven daily counts # 287,154 visitor-days (wrong as uniques)
PFCOUNT uv:article:88121:2026-11-09 uv:article:88121:2026-11-10 ... uv:article:88121:2026-11-15
# (integer) 118930 # weekly unique visitors (correct)

Old reports were wrong, so we recompute the affected weeks from the daily keys that still exist and add a note to the earlier ones.

5Setting Expectations About Error

Every HyperLogLog estimate has a standard error of 0.81 percent. A weekly figure of 118,930 is probably within about 1,000 of the true value, and two methods (for example PFCOUNT over days versus a stored weekly merge) can differ by a little due to rounding in the estimator. We document this in the report itself, so readers do not open tickets about a 0.5 percent difference between two dashboards.

6Follow-up questions

How much memory would the rollup design use over a year?

It reaches a plateau instead of growing. With the assumptions above, eight days of daily keys at about 660 MB a day is roughly 5 GB; five weeks of weekly keys, where more articles turn dense, perhaps 1.5 GB each, about 7.5 GB; and 13 monthly keys at perhaps 2.5 GB each, about 32 GB. That is around 45 GB and flat, against well over 250 GB for a year of daily keys. Exporting months older than a quarter to object storage, using GET to read the raw bytes, brings it down further.

Should we just lower the number of registers to save memory?

Redis does not allow it; the register count and the 0.81 percent error are fixed. Saving memory comes from fewer keys, shorter retention and thresholds, not from smaller HyperLogLogs.

Is UNLINK better than DEL for the cleanup?

Yes, for large or many keys. UNLINK removes the key from the keyspace immediately and frees its memory in a background thread, so a cleanup of millions of keys does not block other clients. For 12 KB keys the difference per key is small, but batching millions of DELs still adds up.

7Conclusion

In this answer, we found two separate problems. Memory grew because HyperLogLogs turn dense at about 12 KB once the sparse form passes hll-sparse-max-bytes, and one key per article per day with no TTL piles up; TTLs, rollups by PFMERGE and a traffic threshold make memory plateau. The weekly figures were too high because they summed daily counts, counting returning visitors once per day; PFCOUNT over the seven keys or their PFMERGE gives the real union. We checked encodings with MEMORY USAGE and documented the 0.81 percent error for readers.

What to say in the interviewAbout 80 seconds aloud

Open with

There are two separate problems. For memory, each HyperLogLog starts sparse, a few hundred bytes, but once it passes hll-sparse-max-bytes, 3000 by default, which happens at a few thousand distinct visitors, it converts to the dense form at about 12 KB and stays there.

Then, if they want more

So every popular article costs 12 KB per day, and since the daily keys have no TTL, four months of them are still sitting there. I'd confirm it by sampling with SCAN and MEMORY USAGE and checking TTL and the config value.

The fix is TTLs on daily keys, set with EXPIRE NX in the same pipeline as PFADD, nightly rollups with PFMERGE into weekly and monthly keys with their own TTLs, which lose no accuracy, and per-article keys only above a traffic threshold if the long tail isn't needed. Then a SCAN and UNLINK job clears the backlog.

For the reports, the weekly number was computed by adding seven daily counts, which counts someone who returns every day seven times. The correct weekly figure is PFCOUNT over the seven daily keys, or PFCOUNT of their PFMERGE, which is the union. I'd recompute the affected weeks and document the 0.81 percent standard error so readers don't chase small differences.

About this answer

Last updated
13 November 2026
Versions it names
Redis 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain both problems and fix them.": https://scrivio-interview-library.netlify.app/databases/redis-patterns/a4ebe1f6cf/