Databases 11 min read

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.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Mastering Redis Set Operations: Intersection, Union, Difference, and More

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:20003

After 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:20201101

The 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:20201101

The 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:202011

To 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:uv

For 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendHyperLogLogredisdata modelingBitmapSet Operations
Code Ape Tech Column
Written by

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

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.