Which Redis data structure would you pick for a leaderboard, a session store, a unique-visitor counter and a work queue?
basicMatch the structure to the access pattern: sorted set for the leaderboard, hash for sessions, HyperLogLog (or set) for unique counts, and a list or stream for queues.
- Sorted set:
ZADD board 1500 user:7,ZREVRANGE board 0 9 WITHSCORES, O(log N) updates and rank queries. - Hash:
HSET session:abc uid 7 role adminlets you update single fields andEXPIREthe whole key. - HyperLogLog:
PFADD uv:2026-10-04 user:7, about 12 KB per key with ~0.8% error; a set is exact but memory grows linearly. - Streams with consumer groups give acknowledgements and replay; lists (
LPUSH/BRPOP) lose a message if the consumer crashes after popping.
- Is HyperLogLog exact? No, it is an approximation; it cannot list members.
- Can you expire a single hash field? Not before Redis 7.4 (
HEXPIRE); before that only the whole key has a TTL.