Skip to content

Lesson 2 of 9 · 25m

Hashes, Sets, and Sorted Sets Solve Real Workflows

Model sessions, feature entitlements, negative caches, leaderboards, scheduling, and time windows without turning Redis into a relational database.

A new hire once asked why we had three systems in Redis when "everything is just key-value." The answer is that the difference between a hash and a serialized blob is not cosmetic. It determines update atomicity, memory layout, network amplification, and how confident you can be under concurrency.

Hashes are for independently mutable fields

A session is a good fit because different paths update different fields:

HSET session:01JRYC user_id u_934 csrf 4f8e touch 1780000100
HINCRBY session:01JRYC request_count 1
HGET session:01JRYC user_id
EXPIRE session:01JRYC 3600

You can increment a counter without reading and rewriting the whole object. You can expire the whole session with one TTL. What you cannot do is expire one hash field independently. If lifecycle differs by field, use separate keys or a design that stores epoch deadlines inside values.

Sets express membership, not truth by themselves

Sets answer "is this thing in this collection?" quickly:

SADD entitlement:team_77 feature:audit-log feature:sso
SISMEMBER entitlement:team_77 feature:sso
SINTER entitlement:team_77 feature:beta-catalog

They are ideal for coarse entitlements, feature flags, blocked identifiers, and negative-cache sentinel membership. They are not authorization policy by themselves. Membership still has to be maintained correctly when teams change, subscriptions expire, or tenants are deleted.

Sorted sets are time machines with scores

A sorted set is an ordered index. Score can be priority, timestamp, points, or any monotonically comparable number:

ZADD leaderboard:weekly 1840 user:alice 1775 user:maya
ZREVRANGE leaderboard:weekly 0 9 WITHSCORES
ZRANK leaderboard:weekly user:maya

The same primitive supports delayed jobs:

ZADD jobs:delayed 1780000300 job:mail-901
ZRANGEBYSCORE jobs:delayed -inf 1780000000 LIMIT 0 100

Fetch jobs due now, atomically claim them in application logic or Lua, then remove them. If you need retry, ordering, acknowledgment, and multiple consumer groups, use streams instead of contorting a sorted set into a queue.

Do not recreate relational joins

Redis can represent relationships, but every join you invent is application code you now own. If you need ad hoc queries across many dimensions, Redis is the wrong source of truth. Use it for known hot paths where the query shape is fixed.

Field notes

  • Large hashes have encoding tradeoffs; huge field counts stop being small-object optimizations and become memory and CPU concerns.
  • SMEMBERS on a large set copies the whole collection. Use SSCAN when cardinality is uncertain.
  • Ties in sorted sets sort lexicographically; if tie behavior matters, test it explicitly.
  • Store IDs in sets and sorted sets, not full objects, unless the collection is intentionally self-contained.

Was this lesson helpful?

Sign in to rate