| name | redis-lists-sets |
| description | Master Redis Lists and Sets - queues, stacks, unique collections, set operations, and real-world implementation patterns |
| sasmp_version | 1.3.0 |
| bonded_agent | 02-redis-data-structures |
| bond_type | PRIMARY_BOND |
| version | 2.1.0 |
| last_updated | 2025-01 |
| parameters | {"key":{"type":"string","required":true,"pattern":"^[a-zA-Z0-9:_-]+$"},"operation":{"type":"string","required":true,"enum":["push","pop","range","members","add","remove","set_ops"]},"blocking":{"type":"boolean","required":false,"default":false},"timeout_seconds":{"type":"integer","required":false,"default":30,"max":300}} |
| retry_config | {"max_retries":3,"backoff_strategy":"exponential","backoff_base_ms":100,"retryable_errors":["connection_timeout","BUSY"]} |
| observability | {"metrics":["list_length","set_cardinality","operation_latency_ms","blocking_wait_time"]} |
Redis Lists and Sets Skill
Lists Overview
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