CodePark/ questions / Data types & commands
START PRACTICINGPRACTICE
Backend question · Redis & caching › Data types & commands

You need the approximate number of unique visitors per page per day, for millions of pages, using as little memory as possible. Which structure fits best?

A HyperLogLog per page per day (PFADD / PFCOUNT)

A HyperLogLog fits best. It uses at most about 12 KB per key, no matter how many items you add. Its count has a standard error of about 0.81%, which is fine for an approximate count.

"A set of visitor IDs per page" gives an exact count, but it grows with every unique visitor. Across millions of pages, that costs a lot of memory. "A sorted set scored by visit time" and "A list of visitor IDs per page" also store every visitor, so they have the same memory problem.