Sets
Sets store unique unordered strings — O(1) SADD and SISMEMBER make them perfect for tags, unique visitors, permission roles, and graph adjacency lists.
Introduction
Sets store unique unordered strings — O(1) SADD and SISMEMBER make them perfect for tags, unique visitors, permission roles, and graph adjacency lists.
Redis uses intset encoding for small integer-only sets and hash table for larger or string members. Set operations (SUNION, SINTER) power recommendation and filtering features.
When order or ranking matters, use sorted sets instead — plain sets cannot sort by score.
Understanding the topic
Key concepts
- SADD key member — add; returns count of new elements.
- SISMEMBER — O(1) existence check.
- SMEMBERS — returns all members; O(N) danger on large sets.
- SUNION/SINTER/SDIFF — combine sets server-side.
- SCARD — cardinality count.
- SRANDMEMBER — random sampling.
Step-by-step explanation
- Small integer sets stored as sorted intset array.
- Grows to hash table when size or type thresholds exceeded.
- Set commands run atomically on single key.
- Multi-key ops (SINTER) block until complete.
- No duplicate members — SADD idempotent.
Syntax reference
Common commands
- SISMEMBER — feature flag / role check.
- Avoid SMEMBERS on huge sets — use SSCAN.
- SINTER — mutual followers, shared tags.
SADD article:42:tags redis cache nosqlSISMEMBER article:42:tags redisSCARD article:42:tagsSINTER tag:redis tag:cache
Informative example
Track unique article viewers per day with set cardinality:
@Servicepublic class UniqueViews {private final StringRedisTemplate redis;public UniqueViews(StringRedisTemplate redis) {this.redis = redis;}public long recordView(String articleId, String userId) {String key = "views:" + articleId + ":" + LocalDate.now();redis.opsForSet().add(key, userId);redis.expire(key, Duration.ofDays(2));return redis.opsForSet().size(key);}}
For approximate counts at scale use HyperLogLog. Sets are exact but memory-heavy for millions of users.
Real-world use
Real-world use cases
- Article tags and filtered browse (SINTER).
- Unique daily active users per page (exact, small scale).
- Mutual connections — SINTER friends:a friends:b.
- Blacklist IP set — SISMEMBER on each request.
- Lottery draw — SRANDMEMBER without replacement pattern.
Best practices
- Use SSCAN not SMEMBERS for large sets.
- Set TTL on daily analytics keys.
- Prefer HLL when ~1% error acceptable for UV counts.
- Namespace: article:{id}:tags.
- Limit set size — split by shard if millions of members.
- SINTER on large sets is expensive — precompute or cache.
Common mistakes
- SMEMBERS on million-member set in production.
- Using set when you need sorted ranking.
- No expiry on daily UV sets — memory accumulates.
- Storing large JSON blobs as set members.
Advanced interview questions
Q1BeginnerWhat is a Redis set?
Q2BeginnerSISMEMBER complexity?
Q3IntermediateSINTER use case?
Q4IntermediateSet vs HyperLogLog for UV?
Q5AdvancedDesign tag system for 10M articles?
Summary
Sets = unique membership, O(1) lookup.