Databases 8 min read

Mastering Redis Sets: From Basics to Advanced Operations

This article introduces Redis Sets as unordered unique string collections, explains their ideal use cases, and provides detailed walkthroughs of creation, query, update, and deletion commands—including SADD, SREM, SISMEMBER, SCARD, SMEMBERS, SSCAN, SINTER, SUNION, and SDIFF—along with underlying encoding mechanisms.

NiuNiu MaTe
NiuNiu MaTe
NiuNiu MaTe
Mastering Redis Sets: From Basics to Advanced Operations

What is Redis Set?

Redis Set is an unordered collection of unique strings.

Use Cases

Ideal for scenarios such as tracking which public accounts a user follows; Sets also support intersection and union operations to easily implement common‑follow features.

Common Operations

We explore the basic CRUD operations for Sets.

Write Operations

SADD

Syntax: SADD key member [member ...] Function: adds elements and returns the number of elements successfully added.

Example – create a set named setniuniu with initial elements aa, bb, cc:

127.0.0.1:6379> SADD setniuniu aa bb cc
(integer) 3

Current elements: "aa", "bb", "cc".

SREM

Syntax: SREM key member [member ...] Function: removes elements and returns the number of elements successfully removed.

127.0.0.1:6379> SREM setniuniu 33
(integer) 1

After deletion, the set contains "aa", "bb", "cc", "11", "22".

Read Operations

SISMEMBER

Syntax: SISMEMBER key member Function: checks whether an element exists in the set.

127.0.0.1:6379> SISMEMBER setniuniu aa
(integer) 1
127.0.0.1:6379> SISMEMBER setniuniu ff
(integer) 0

SCARD

Syntax: SCARD key Function: returns the number of elements in the set.

127.0.0.1:6379> SCARD setniuniu
(integer) 5

SMEMBERS

Syntax: SMEMBERS key Function: lists all elements of the set.

127.0.0.1:6379> SMEMBERS setniuniu
1) "aa"
2) "bb"
3) "11"
4) "cc"
5) "22"

SSCAN

Syntax: SSCAN key cursor [MATCH pattern] [COUNT count] Function: iterates over set elements; default count is 10.

Example – start from cursor 0, default 10 elements:

127.0.0.1:6379> SSCAN setniuniu 0
1) "0"
2) "aa"
3) "bb"
4) "11"
5) "22"
6) "cc"

Example – fuzzy match with pattern 1*:

127.0.0.1:6379> SSCAN setniuniu 0 MATCH 1*
1) "0"
2) "11"

SINTER

Syntax: SINTER key [key ...] Function: returns elements that exist in all specified sets.

127.0.0.1:6379> SINTER setniuniu setmart
1) "bb"
2) "aa"

SUNION

Syntax: SUNION key [key ...] Function: returns the union of all specified sets.

127.0.0.1:6379> SUNION setmart setniuniu
1) "ff"
2) "aa"
3) "bb"
4) "11"
5) "cc"
6) "22"

SDIFF

Syntax: SDIFF key [key ...] Function: returns elements present in the first set but not in the subsequent sets.

127.0.0.1:6379> SDIFF setniuniu setmart
1) "cc"
2) "22"
3) "11"

Underlying Implementation

Encoding Methods

Redis uses two encoding strategies for Sets. If all elements are integers and the set size does not exceed 512, an INTSET encoding is used, which stores elements compactly in memory but requires binary search for lookups.

When the INTSET conditions are not met, Redis falls back to a HASHTABLE encoding, offering O(1) lookup performance for element existence checks.

Summary

Redis Sets provide an easy way to manage unordered collections, support powerful set operations like intersection and union, and are implemented using either INTSET for small integer sets (memory‑efficient) or HASHTABLE for fast element lookup.

databaseRedisData StructurescommandsSet
NiuNiu MaTe
Written by

NiuNiu MaTe

Joined Tencent (nicknamed "Goose Factory") through campus recruitment at a second‑tier university. Career path: Tencent → foreign firm → ByteDance → Tencent. Started as an interviewer at the foreign firm and hopes to help others.

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.