This episode covers three unique-collection structures: Sets for set operations and tags, Sorted Sets for leaderboards and priority queues, and HyperLogLog for counting unique visitors with a constant memory footprint of around 12KB.

After Lists and Hashes in episode 4, now we cover three structures related to unique collections: Sets, Sorted Sets, and HyperLogLog.
Sets give you set operations like intersection and union — perfect for tags and membership detection. Sorted Sets add a score for ranking — the foundation of leaderboards and priority queues. Finally, HyperLogLog is a magic trick for counting millions of unique visitors with almost no growth in memory. Let's dissect them one by one.
A Set holds unique elements without order. The same element cannot be added twice:
redis-cli SADD tags:post:1 "redis" "database" "cache"
redis-cli SREM tags:post:1 "cache"
redis-cli SMEMBERS tags:post:1SADD tags:post:1 "redis" "database" "cache" adds three tags, SREM removes one, SMEMBERS lists all. Membership and count:
redis-cli SISMEMBER tags:post:1 "redis"
redis-cli SCARD tags:post:1SISMEMBER returns 1 if the element exists — an O(1) operation that is extremely cheap for membership checks. SCARD counts the number of elements.
This is the power of Sets that no other data type has:
redis-cli SINTER tags:post:1 tags:post:2
redis-cli SUNION tags:post:1 tags:post:2
redis-cli SDIFF tags:post:1 tags:post:2SINTER gives the intersection (tags that appear in both posts), SUNION the union, SDIFF the difference. Real-world use cases: content recommendations ("users who liked A also liked B"), anomaly detection, and "people you may know" features.
A Sorted Set is like a Set, but every element has a numeric score that determines ordering:
redis-cli ZADD leaderboard 100 "player1"
redis-cli ZADD leaderboard 250 "player2"
redis-cli ZRANGE leaderboard 0 -1 WITHSCORES
redis-cli ZREVRANGE leaderboard 0 -1ZADD leaderboard 100 "player1" adds a player with a score of 100. ZRANGE sorts ascending, ZREVRANGE descending — the standard leaderboard pattern. Equal scores are ordered lexicographically by member.
redis-cli ZSCORE leaderboard "player2"
redis-cli ZREVRANK leaderboard "player2"
redis-cli ZINCRBY leaderboard 50 "player2"ZSCORE shows a member's score, ZREVRANK its rank position (0 = highest), and ZINCRBY increments the score atomically — exactly what's needed to update real-time game scores.
redis-cli ZRANGEBYSCORE leaderboard 100 200ZRANGEBYSCORE leaderboard 100 200 lists all members with a score between 100 and 200. This pattern becomes the basis for the sliding window rate limiter (episode 11) and score-based priority queues.
Counting millions of unique visitors exactly requires a large Set and lots of memory. HyperLogLog uses probabilistic estimation: about 0.81% error, but memory stays at ~12KB no matter how many elements there are. Redis doesn't store the elements themselves; it only exploits hash distribution properties to estimate cardinality.
redis-cli PFADD visits:2026-08-03 "user-1" "user-2" "user-1"
redis-cli PFCOUNT visits:2026-08-03
redis-cli PFADD visits:2026-08-04 "user-2" "user-3"
redis-cli PFMERGE visits:week1 visits:2026-08-03 visits:2026-08-04PFADD visits:2026-08-03 "user-1" "user-2" "user-1" adds visitors (duplicates are ignored), PFCOUNT estimates the unique count. PFMERGE merges several HLLs — for example counting unique weekly visitors from daily data.
Info
The 0.81% error tolerance is almost always sufficient for analytics dashboards and reach estimation. If you need exact counts for financial or audit purposes, use a Set or Sorted Set — at the price of much larger memory.
score = timestamp).| Structure | Properties | Typical Use Case |
|---|---|---|
| Set | Unique, unordered, set operations | Tags, online users |
| Sorted Set | Unique, ordered by score | Leaderboards, rate limiting |
| HyperLogLog | Unique estimation, ~12KB memory | Unique visitor analytics |
Episode 5 equipped you with Sets with set operations, Sorted Sets for score-based ranking, and HyperLogLog for cardinality estimation with constant memory: SADD/SINTER, ZADD/ZREVRANK/ZINCRBY, and PFADD/PFCOUNT/PFMERGE.
Key takeaways:
SISMEMBER is an O(1) membership check.SINTER/SUNION/SDIFF provide set operations for recommendations and filtering.ZINCRBY updates scores atomically; ZRANGEBYSCORE unlocks the rate limiter pattern.In the next episode, episode 6, we cover Streams — a log-based structure for event streaming and message brokering, similar to Apache Kafka but built into Redis. You'll learn XADD, consumer groups, acknowledgments, and pending message management. This is favorite material for many backend engineers!