Redis Sets are collections that store unique elements. Why is this uniqueness important in Redis Sets?
Think about how Redis quickly checks if an element exists in a Set.
Redis Sets use a hash table internally. Hash tables store keys uniquely, so duplicates cannot exist. This makes checking membership very fast.
Consider the following Redis commands:
SADD myset apple SADD myset banana SADD myset apple SMEMBERS myset
What will be the output of SMEMBERS myset?
Remember that Sets do not allow duplicates.
The duplicate 'apple' is ignored on the second SADD. The Set contains only unique elements.
Choose the correct Redis command syntax to add the elements 'cat', 'dog', and 'bird' to a Set named 'pets'.
Redis commands list multiple elements separated by spaces without brackets or quotes.
The correct syntax is SADD key element1 element2 ... without brackets or quotes around elements.
Redis uses different internal encodings for Sets depending on their size. How does this help maintain uniqueness efficiently for small Sets?
Think about how integer arrays can be faster than hash tables for small data.
For small Sets, Redis uses an integer array encoding (intset) which allows quick membership checks and keeps elements unique without hashing overhead.
Given the commands:
SADD colors red SADD colors blue SADD colors red SCARD colors
The SCARD colors returns 2. Why?
Think about how Sets handle duplicates when adding elements.
Redis Sets do not allow duplicates. Adding 'red' twice only stores it once, so the count is 2.