Redis Lists are linked lists of string values, perfect for queues and stacks.
List Commands
# Push operations
LPUSH key value [value ...] # Push to head - O(1) per element
RPUSH key value [value ...] # Push to tail - O(1) per element
# Pop operations
LPOP key [count] # Pop from head - O(N)
RPOP key [count] # Pop from tail - O(N)
BLPOP key [key ...] timeout # Blocking pop - O(1)
BRPOP key [key ...] timeout # Blocking pop from tail
# Range operations
LRANGE key start stop # Get range - O(S+N)
LINDEX key index # Get by index - O(N)
LLEN key # Get length - O(1)
# Manipulation
LMOVE source dest LEFT|RIGHT LEFT|RIGHT
LINSERT key BEFORE|AFTER pivot value
LSET key index value # Set by index
LTRIM key start stop # Trim list
LPOS key element # Find position (Redis 6.0.6+)
Sets Overview
Redis Sets are unordered collections of unique strings.
Set Commands
# Basic operations
SADD key member [member ...] # Add members - O(N)
SREM key member [member ...] # Remove members - O(N)
SMEMBERS key # Get all members - O(N)
SISMEMBER key member # Check membership - O(1)
SMISMEMBER key member [member ...] # Multi-check (Redis 6.2+)
SCARD key # Get cardinality - O(1)
# Random operations
SRANDMEMBER key [count] # Random members
SPOP key [count] # Pop random members
# Set operations
SINTER key [key ...] # Intersection - O(N*M)
SUNION key [key ...] # Union - O(N)
SDIFF key [key ...] # Difference - O(N)
SINTERSTORE dest key [key ...] # Store intersection
SUNIONSTORE dest key [key ...] # Store union
SDIFFSTORE dest key [key ...] # Store difference
# Scanning
SSCAN key cursor [MATCH pattern] [COUNT count]
Production Patterns
Pattern 1: Reliable Message Queue
# Producer
RPUSH queue:tasks '{"id":1,"action":"process","retry":0}'
# Consumer with reliability (move to processing)
LMOVE queue:tasks queue:processing LEFT RIGHT
# After processing complete
LREM queue:processing 1 '{"id":1,"action":"process","retry":0}'
# Dead letter queue for failures
RPUSH queue:dlq '{"id":1,"action":"process","error":"timeout"}'
Pattern 2: Priority Queue
# High priority
LPUSH queue:tasks:high '{"priority":"high"}'
# Normal priority
RPUSH queue:tasks:normal '{"priority":"normal"}'
# Consumer checks high first
BLPOP queue:tasks:high queue:tasks:normal 30
Pattern 3: Unique Visitors Tracking
# Track unique visitors per day
SADD visitors:2024-01-15 "user:123"
SADD visitors:2024-01-15 "user:456"
# Count unique
SCARD visitors:2024-01-15
# Weekly unique visitors (union)
SUNIONSTORE visitors:week:3 visitors:2024-01-15 visitors:2024-01-16 visitors:2024-01-17
SCARD visitors:week:3
# Set TTL for automatic cleanup
EXPIRE visitors:2024-01-15 604800 # 7 days
Pattern 4: Tag System
# Add tags to items
SADD item:123:tags "redis" "database" "cache"
SADD item:456:tags "redis" "nosql"
# Find items with specific tag
SADD tag:redis:items "item:123" "item:456"
# Find items with ALL tags (intersection)
SINTER tag:redis:items tag:database:items
# Find items with ANY tag (union)
SUNION tag:redis:items tag:nosql:items
WRONGTYPE Operation against a key holding the wrong kind of value
Cause: Using list command on set or vice versa
Diagnosis:
TYPE mykey # Check actual type
Prevention: Use consistent naming conventions (e.g., list:*, set:*)
2. Blocking Operation Timeout
# Returns nil after timeout
BLPOP queue:empty 5
Cause: No elements in list within timeout
Fix:
Increase timeout for low-traffic queues
Check if producers are running
Use non-blocking LPOP with retry logic
3. Large Set Operations Blocking
# Can block for seconds on large sets
SMEMBERS huge:set # 1M+ members
Fix:
# Use SSCAN instead
SSCAN huge:set 0 COUNT 1000
4. Memory Issues with Long Lists
Cause: Unbounded list growth
Fix:
# Cap list length
LTRIM queue:logs 0 9999 # Keep last 10000
# Or use RPUSH with LTRIM atomically
MULTI
RPUSH queue:logs "entry"
LTRIM queue:logs -10000 -1
EXEC