Mastering Redis Set Operations: Intersection, Union, Difference, and More
This article explains how to use Redis sets, sorted sets, bitmaps, and HyperLogLog for various aggregation, sorting, binary state, and cardinality statistics, providing concrete command examples, design patterns, and performance considerations for large‑scale applications.
Preface
Redis is often used merely as a cache, but its set data structures are powerful for large‑scale applications such as sign‑in systems, e‑commerce comment lists, and social‑network friend lists, where a single key maps to a collection of items that need statistical analysis.
Aggregated Statistics
Intersection
To find common friends between two users, store each user's friend IDs in a Set and compute the intersection with SINTERSTORE. The resulting key contains only the IDs present in both sets.
SINTERSTORE userid:new userid:20002 userid:20003After execution, userid:new holds the intersection of userid:20002 and userid:20003.
Difference
To identify newly added friends on a given day, compute the difference between the current day's set and the previous day's set using SDIFFSTORE.
SDIFFSTORE user:new userid:20201102 userid:20201101The resulting user:new set contains only the friends added on 2020‑11‑02.
Union
To count total new friends over two days, merge the daily sets with SUNIONSTORE.
SUNIONSTORE userid:new userid:20201102 userid:20201101The userid:new set now represents the union of both days' new friends.
Sorting Statistics
Ordered data can be stored using List (insertion order) or Sorted Set (score‑based order). Lists support LRANGE for pagination, while Sorted Sets use ZRANGEBYSCORE and allow custom weighting, making them more flexible for scenarios beyond simple chronological ordering.
Binary State Statistics
For true/false states such as sign‑in (1) or not (0), Redis Bitmap (implemented on top of String) provides a memory‑efficient bit array. Commands SETBIT, GETBIT, and BITCOUNT manipulate and count bits.
SETBIT userid:10001:202011 1 1 GETBIT userid:10001:202011 1 BITCOUNT userid:10001:202011To find users who signed in for 20 consecutive days, store each day’s bitmap under a separate key, then apply a bitwise AND across the 20 keys and finally BITCOUNT the result.
Cardinality Statistics
When counting unique elements (e.g., page UV), Set guarantees exact deduplication but can be memory‑intensive. Redis HyperLogLog offers approximate counting with ~0.81% error using only ~12 KB for billions of elements.
PFADD p1:uv 10001 10002 10003 10004 PFCOUNT p1:uvFor scenarios requiring precise counts, fall back to Set; otherwise, HyperLogLog provides a space‑efficient alternative.
Conclusion
Redis Set and Sorted Set support intersection and union operations, while Sorted Set does not support difference. Bitmap can perform bitwise AND/OR/XOR across multiple keys and is ideal for binary‑state tracking. List and Sorted Set both enable ordered retrieval, but Sorted Sets offer weight‑based flexibility. Choose the data type that matches the statistical requirement, considering operation complexity and memory consumption.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
