Lists and Sets
LPUSH, RPOP, SADD, SINTER, and queue patterns
Skills taught: redis.lists-sets
Lists as Queues and Stacks
Redis Lists support O(1) push and pop from both ends. This makes them ideal for FIFO queues (RPUSH + BLPOP) and stacks (LPUSH + LPOP). Blocking variants wait for data rather than polling.
# FIFO Queue: add to right, consume from left
RPUSH jobs:email "send-welcome" "send-receipt" "send-promo"
LPOP jobs:email # "send-welcome"
# Blocking consumer (worker waits up to 30s for a job)
BLPOP jobs:email 30
# Returns: ["jobs:email", "send-receipt"]
# Stack: add and remove from same end
LPUSH history:user1 "visited page A"
LPUSH history:user1 "clicked buy"
LPOP history:user1 # "clicked buy" (most recent first)
# View without consuming
LRANGE jobs:email 0 -1 # all items
LLEN jobs:email # count
# Trim to last 100 items (capped log)
LTRIM activity:feed 0 99
Reliable Queue with LMOVE
Plain BLPOP has a risk: if the worker crashes after popping but before finishing, the job is lost. LMOVE solves this by atomically moving the job to a 'processing' list.
async function processNextJob() {
// Atomically move job from 'pending' to 'processing'
const job = await redis.lMove('jobs:pending', 'jobs:processing', 'LEFT', 'RIGHT');
if (!job) return;
try {
await doWork(job);
// On success, remove from processing
await redis.lRem('jobs:processing', 1, job);
} catch (err) {
// On failure, put back in pending
await redis.lMove('jobs:processing', 'jobs:pending', 'LEFT', 'LEFT');
throw err;
}
}
// Recover lost jobs (e.g. on startup)
const stuck = await redis.lRange('jobs:processing', 0, -1);
for (const job of stuck) {
await redis.lMove('jobs:processing', 'jobs:pending', 'LEFT', 'LEFT');
}
Sets for Unique Membership
Sets store unique strings with O(1) add, remove, and membership check. They also support powerful set operations: union, intersection, and difference.
# Online presence
SADD online:users "alice" "bob" "carol"
SISMEMBER online:users "alice" # 1 (is online)
SCARD online:users # 3 (count)
SMEMBERS online:users # all members
# Social graph
SADD user:1:following "alice" "bob"
SADD user:2:following "bob" "carol"
# Set operations
SINTER user:1:following user:2:following # common: ["bob"]
SUNION user:1:following user:2:following # all: ["alice","bob","carol"]
SDIFF user:1:following user:2:following # only user:1 follows: ["alice"]
# Move a member between sets
SMOVE online:users away:users "alice"
# Random member (useful for sampling)
SRANDMEMBER online:users 2 # 2 random members
SPOP online:users # remove and return one random member
Real-World: Mutual Friends
// Follow a user
async function follow(userId, targetId) {
await redis.sAdd(`user:${userId}:following`, targetId);
await redis.sAdd(`user:${targetId}:followers`, userId);
}
// Get mutual friends
async function getMutualFriends(userId1, userId2) {
return redis.sInter(
`user:${userId1}:following`,
`user:${userId2}:following`
);
}
// Who to suggest following (friends of friends minus already following)
async function getSuggestions(userId) {
const following = await redis.sMembers(`user:${userId}:following`);
const friendsOfFriends = `user:${userId}:suggestions:temp`;
for (const friendId of following) {
await redis.sUnionStore(friendsOfFriends, friendsOfFriends, `user:${friendId}:following`);
}
const suggestions = await redis.sDiff(friendsOfFriends, `user:${userId}:following`);
await redis.del(friendsOfFriends);
return suggestions.filter(id => id !== userId);
}
List vs Set
- List: Ordered sequence, allows duplicates
- Set: Unordered unique strings
Tip: Use BLPOP jobs:email jobs:sms 30 with multiple keys to have one worker drain multiple queues. Redis returns the key name along with the value so you know which queue fired.
Frequently Asked Questions
How do I implement a task queue with Redis?
RPUSH to enqueue jobs, BLPOP to consume them. For reliability, use LMOVE to atomically move jobs to a 'processing' list before working on them, so crashes don't lose jobs.
What is the maximum number of members in a Redis Set?
Up to 2^32 - 1 (over 4 billion) members. In practice, large sets use significant memory — monitor SCARD and size accordingly.
Can I expire individual set members?
No. TTL applies to the whole key. If you need per-member expiry, use a Sorted Set with timestamps as scores and periodically ZREMRANGEBYSCORE to remove old members.