Set

A Redis set is an unordered collection of unique strings (members). You can use Redis sets to efficiently:

  • Track unique items (e.g., track all unique IP addresses accessing a given blog post).

  • Represent relations (e.g., the set of all users with a given role).

  • Perform common set operations such as intersection, unions, and differences.

A Redis set is an unordered collection of unique strings (members). You can use Redis sets to efficiently:

SADD "user:userID1:followers" followerID1
SADD "user:userID1:followers" followerID2

SADD "user:userID2:followers" followerID1
SADD "user:userID2:followers" followerID3

-- Find the common followers between two users
SINTER "user:userID1:followers" "user:userID2:followers"

-- Get the SET values
SMEMBERS "user:userID1:followers"

-- Gets the size of the set.
SCARD "user:userID1:followers"

-- Find out whether followerID1 is part of this set
SISMEMBER user:userID1:followers followerID1

Use Cases

Real-time Analytics: Track and display unique events or items, like products viewed in a session.

ACLs (Access Control List): Manage user access to certain features or commands.

Last updated