What is a Redis set, and what guarantee does it provide about the items you store in it?
Short answer
A Redis set stores an unordered collection of unique values, automatically preventing any duplicate from being added twice, which makes it a natural fit for tracking things like unique tags on a post or unique visitors to a page, where you only care whether an item is present…
Advertisement
AModel answer
A Redis set stores an unordered collection of unique values, automatically preventing any duplicate from being added twice, which makes it a natural fit for tracking things like unique tags on a post or unique visitors to a page, where you only care whether an item is present, not how many times it might have been added.
Example
SADD post_tags:1 'redis' 'database' 'caching' SADD post_tags:1 'redis' -- has no effect, already present
Real-world example
A blogging platform stores each post's tags in a set, automatically ensuring a tag can never accidentally be duplicated even if it is added multiple times through different parts of the application.
Likely follow-ups
- What happens if you try to add an item that already exists in a set?
- How do you check how many unique items are currently in a set?
Related topics