What Is a Redis Set?
A Redis set is an unordered collection of unique strings. Like the set data structure found in many programming languages, a Redis set never stores duplicate members: adding a value that is already present simply has no effect. Because Redis uses binary-safe strings, the members of a set aren't limited to text — any data serialized into binary form can be stored as a set member.
A Redis set is one of the five basic Redis data types, along with strings, hashes, lists, and sorted sets. It's worth distinguishing the Redis set data type described here from the Redis SET command, which writes a single string value to a key — the two share a name but are unrelated. (The command that adds a member to a set is SADD.) The defining properties of a set are that its members are unique and unordered, and that membership can be tested in constant time, which makes sets a natural fit for problems involving uniqueness, tagging, and relationships between groups of items.
How Does a Redis Set Work?
Unlike a Redis list, a set does not preserve insertion order, and unlike a hash, it stores bare members rather than field-value pairs. What a set guarantees is uniqueness and very fast membership checks — testing whether a value belongs to a set is an O(1) operation regardless of how large the set grows. A single set can hold more than four billion members.
Redis provides a dedicated family of commands for working with sets. These include:
- SADD adds one or more members to a set, ignoring any that are already present.
- SREM removes one or more members from a set.
- SISMEMBER tests whether a given value is a member of a set, returning 1 or 0.
- SMEMBERS returns every member of a set.
- SCARD returns the cardinality — the number of members in a set.
- SPOP removes and returns one or more random members.
- SRANDMEMBER returns one or more random members without removing them.
- SSCAN incrementally iterates over the members of a large set without blocking the server.
Sets also support "set algebra" across multiple keys, which is where much of their power lies:
- SINTER returns the intersection — the members common to all the given sets.
- SUNION returns the union — every member across the given sets.
- SDIFF returns the difference — members of the first set that aren't in the others.
- SINTERSTORE, SUNIONSTORE, and SDIFFSTORE perform the same operations but store the result in a new set instead of returning it.
Redis Set Use Cases
The uniqueness guarantee and constant-time membership checks make Redis sets a good fit for a range of problems:
Tracking unique items. Because a set automatically discards duplicates, it's an efficient way to count or store distinct values — unique visitors to a page, unique IP addresses seen in a request stream, or the distinct tags applied to an article. Adding the same value twice with SADD never creates a duplicate.
Membership and access control. With SISMEMBER, an application can check in constant time whether a user belongs to a group, whether an item has already been processed, or whether an address appears on an allow-list or block-list — without scanning a larger collection.
Tags and labels. Sets are a clean way to model many-to-many relationships. A set can hold all the tags attached to a piece of content, and a separate set can hold all the content IDs attached to a given tag.
Relationships and recommendations. Set algebra shines here. SINTER can find the mutual followers of two users or the products two customers both purchased, SUNION can combine audiences, and SDIFF can find "users in segment A but not segment B." These operations run on the server, avoiding the cost of pulling large collections back to the client.
Random selection. SRANDMEMBER and SPOP make it easy to pick random members for raffles, sampling, or handing out work items, either leaving the set intact or removing the chosen member.
How Java Developers Can Use Redis Sets With Redisson
Java developers already know sets through the standard java.util.Set interface. Redisson makes Redis sets feel just as familiar by providing the RSet interface, which implements java.util.Set and is backed by a Redis set behind the scenes. Because Redisson handles serialization through its codecs, an RSet can hold rich Java objects rather than only strings, while methods like add(element), contains(element), remove(element), and size() behave exactly as a Java developer expects — except that the collection now lives in Redis and is shared across every instance of a distributed application.
RSet<SomeObject> set = redisson.getSet("anySet");
set.add(new SomeObject());
set.contains(new SomeObject());
set.remove(new SomeObject());
RSet also exposes the set-algebra operations directly, so intersections, unions, and differences across several Redis sets can be computed with straightforward Java method calls (readIntersection, readUnion, readDiff, and their storing counterparts) instead of raw commands.
Beyond the basic RSet, Redisson offers set variants for more demanding scenarios. RSetCache adds a feature that native Redis sets don't have — a time-to-live (TTL) on individual members — so each element can be given its own expiration, which is useful for caching:
RSetCache<SomeObject> set = redisson.getSetCache("anySet");
// this element will expire and be evicted after 10 minutes
set.add(new SomeObject(), 10, TimeUnit.MINUTES);
For very large sets, Redisson PRO provides RClusteredSet, which spreads a single logical set's data across multiple master nodes in a Redis cluster rather than confining it to one node. With these implementations, Java developers get distributed, thread-safe sets — along with features like listeners and automatic expiration — without having to learn the underlying Redis set commands.
To learn more about Redisson, RSet, and the advanced features of Redisson PRO, try Redisson for free today.