Set Math: Intersection/Union

Difficulty: Beginner

Question

How do you find the elements common to two Redis Sets, all unique elements across them, or the elements in one but not the other?

Answer

Use SINTER set1 set2 for the intersection (elements in both), SUNION set1 set2 for the union (every unique element), and SDIFF set1 set2 for the difference (elements in set1 that are not in set2). Each has a STORE variant (SINTERSTORE, SUNIONSTORE, SDIFFSTORE) that saves the result into a new key instead of returning it, and Redis 7.0 added SINTERCARD to count the intersection without transferring it.

The commands at a glance

CommandReturnsTime complexity
SINTER a b ...Members present in all setsO(N*M) worst case, N = smallest set size, M = number of sets
SUNION a b ...Members present in any setO(N), N = total members across sets
SDIFF a b ...Members of a not in any later setO(N), N = total members across sets
SINTERCARD n a b ... [LIMIT k]Size of the intersection (7.0+)Like SINTER, can stop early at LIMIT
S*STORE dest ...Writes result to dest, returns its sizeSame as the read version

SDIFF is the only order-sensitive one: SDIFF a b and SDIFF b a differ. A missing key is treated as an empty set, so SINTER with any missing key returns an empty result.

Practical examples

  • Mutual friends or followers. SINTER followers:asha followers:ravi.
  • Product filters. Keep sets like tag:5g, brand:samsung, price:under-20k holding product IDs; SINTER gives the products matching all filters.
  • Notification targeting. SDIFF users:active users:opted_out gives the users you are allowed to message.
  • Unique visitors across days. SUNION visitors:2026-10-10 visitors:2026-10-11 (or HyperLogLog if you only need an approximate count).
  • Quick check. SINTERCARD 2 cart:42 blocked:skus LIMIT 1 returns 1 as soon as one blocked item is found.

Gotchas

  • Big sets block. SUNION across sets with millions of members returns a huge reply and blocks the server while building it. Use STORE with an expiry and paginate with SSCAN, or precompute.
  • Redis Cluster. Multi-key commands require all keys to hash to the same slot, otherwise you get a CROSSSLOT error. Use hash tags: followers:{asha} and following:{asha} land on the same slot because only the part inside braces is hashed.
  • Results are unordered. Sets have no order. If you need ranking, use sorted sets and ZINTER / ZUNION (6.2+) or their STORE versions with weights.
  • STORE results have no TTL. SINTERSTORE creates a normal key; add EXPIRE if it is a cache.
Set operations on two follower lists. followers:asha = {ravi, meera, kabir} and followers:neha = {meera, kabir, dev}.
CommandResult
IntersectionSINTER asha nehameera, kabir
UnionSUNION asha neharavi, meera, kabir, dev
DifferenceSDIFF asha neharavi
Count onlySINTERCARD 2 asha neha2

Code examples

Set math from redis-cli

SADD followers:asha ravi meera kabir
SADD followers:neha meera kabir dev

SINTER followers:asha followers:neha
SUNION followers:asha followers:neha
SDIFF  followers:asha followers:neha
SINTERCARD 2 followers:asha followers:neha

# Save the mutual followers for 10 minutes
SINTERSTORE mutual:asha followers:asha followers:neha
EXPIRE mutual:asha 600

Member order in set replies is not guaranteed. In Redis Cluster these keys would need a shared hash tag, such as followers:{social}:asha, to avoid a CROSSSLOT error.

Key points

  • SINTER = in all sets, SUNION = in any set, SDIFF = in the first but not the others
  • STORE variants save the result into a key and return its size
  • SINTERCARD (7.0+) counts the intersection, with LIMIT for early exit
  • Multi-key set commands need same-slot keys in Redis Cluster; use hash tags
  • Large unions and intersections block the server; precompute or store with TTL

Common follow-up questions

  • How would you implement a product filter with many attributes using sets?
  • What is the complexity of SINTER and how does Redis optimise it?
  • How do hash tags work in Redis Cluster?
  • When would you use HyperLogLog instead of SUNION?

Interview tips

  • Name all three operations and the STORE variants, not just SINTER
  • Mention the CROSSSLOT issue in Cluster; it is a frequent follow-up
  • Give a concrete use case like mutual friends or filters

Concepts covered

SINTER, SUNION, SDIFF, SINTERCARD, Hash tags in Cluster

More Redis & Caching questions