Sets and Sorted Sets

[!NOTE] This module explores the core principles of Sets and Sorted Sets, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.

1. Sets: Unordered & Unique

Redis Sets store unique strings. They are perfect for filtering duplicates and performing set theory operations.

  • Complexity: O(1) for Add/Remove/Check.
  • Internals: Hashtable (or IntSet if small integers).

Use Case: Common Friends (Intersection)

User A follows [1, 2, 3]. User B follows [2, 3, 4]. Common: [2, 3].

jedis.sadd("user:A:follows", "1", "2", "3");
jedis.sadd("user:B:follows", "2", "3", "4");

// SINTER = Set Intersection
Set<String> common = jedis.sinter("user:A:follows", "user:B:follows");
// Returns ["2", "3"]