Redis sets

Introduction to Redis sets

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.

Basic commands

  • 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.

See the complete list of set commands.

Examples

  • Store the sets of bikes racing in France and the USA. Note that if you add a member that already exists, it will be ignored.

  • Check whether bike:1 or bike:2 are racing in the US.

  • Which bikes are competing in both races?

  • How many bikes are racing in France?

Tutorial

The SADD command adds new elements to a set. It's also possible to do a number of other operations against sets like testing if a given element already exists, performing the intersection, union or difference between multiple sets, and so forth.