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 adds a new member to a set.

  • SREM removes the specified member from the set.

  • SISMEMBER tests a string for set membership.

  • SINTER returns the set of members that two or more sets have in common (i.e., the intersection).

  • SCARD returns the size (a.k.a. cardinality) of a set.

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