Redis
Redis cheat sheet covering data structures, caching strategies, pub/sub, transactions, persistence, and cluster management examples.
Sign in to mark items as known and track your progress.
Sign inInstallation & Basics
Getting started with Redis
Installation & Setup
Installing Redis on different platforms
# Ubuntu/Debian
sudo apt update
sudo apt install redis-server
# macOS with Homebrew
brew install redis
# Docker
docker run -d -p 6379:6379 redis
# Start Redis server
redis-server
# Connect with CLI
redis-cliBasic Commands
Essential Redis commands and operations
# Connect to Redis
redis-cli
# Test connection
PING
# Set and get values
SET key "value"
GET key
# Set with expiration
SETEX key 60 "value" # Expires in 60 seconds
TTL key # Check time to live
# Delete key
DEL keyData Structures
Redis data types and their operations
Lists
Ordered collections of strings
# Push elements
LPUSH mylist "first" # Push to left (head)
RPUSH mylist "last" # Push to right (tail)
# Pop elements
LPOP mylist # Pop from left
RPOP mylist # Pop from right
# Get elements
LRANGE mylist 0 -1 # Get all elements
LINDEX mylist 0 # Get element at index
LLEN mylist # Get list lengthSets
Unordered collections of unique strings
# Add members
SADD myset "member1"
SADD myset "member2"
# Check membership
SISMEMBER myset "member1"
# Get all members
SMEMBERS myset
# Set operations
SUNION set1 set2 # Union
SINTER set1 set2 # Intersection
SDIFF set1 set2 # DifferenceSorted Sets
Ordered sets with scores
# Add members with scores
ZADD leaderboard 100 "player1"
ZADD leaderboard 200 "player2"
# Get by rank
ZRANGE leaderboard 0 -1 # All members
ZREVRANGE leaderboard 0 9 # Top 10
# Get by score
ZRANGEBYSCORE leaderboard 100 200Hashes
Field-value pairs (like objects)
# Set fields
HSET user:1 name "John"
HSET user:1 age 30
# Get fields
HGET user:1 name
HGETALL user:1
# Multiple fields
HMSET user:2 name "Jane" age 25 city "NYC"Pub/Sub & Streams
Messaging patterns and stream processing
Pub/Sub Messaging
Publish/Subscribe messaging pattern
# Subscribe to channel
SUBSCRIBE channel1
# Publish message
PUBLISH channel1 "Hello subscribers"
# Pattern subscription
PSUBSCRIBE news:*
# Unsubscribe
UNSUBSCRIBE channel1Streams
Persistent message streams with consumer groups
# Add to stream
XADD mystream * field1 value1 field2 value2
# Read from stream
XREAD COUNT 2 STREAMS mystream 0
# Consumer groups
XGROUP CREATE mystream mygroup 0
XREADGROUP GROUP mygroup consumer1 STREAMS mystream >Persistence & Backup
Data persistence and backup strategies
Persistence Options
RDB snapshots and AOF logging
# RDB (snapshots)
SAVE # Synchronous save
BGSAVE # Background save
LASTSAVE # Last save timestamp
# AOF (Append Only File)
CONFIG SET appendonly yes
BGREWRITEAOF # Rewrite AOF file
# Configure in redis.conf
save 900 1 # Save after 900s if 1 key changed
appendonly yes # Enable AOFPerformance & Optimization
Optimizing Redis for better performance
Memory Optimization
Memory management and optimization techniques
# Memory commands
INFO memory # Memory statistics
MEMORY USAGE key # Memory used by key
MEMORY STATS # Detailed memory stats
MEMORY DOCTOR # Memory issues report
# Eviction policies
CONFIG SET maxmemory 2gb
CONFIG SET maxmemory-policy allkeys-lruPerformance Tuning
Optimizing Redis performance
# Slow log
CONFIG SET slowlog-log-slower-than 10000 # Log queries > 10ms
SLOWLOG GET 10 # Get last 10 slow queries
# Pipeline commands for bulk operations
# Use client library pipelining
# Disable persistence if not needed
CONFIG SET save ""
CONFIG SET appendonly noClustering & Replication
Scaling Redis with replication and clustering
Replication & Clustering
Master-slave replication and Redis Cluster
# Replication setup
REPLICAOF master-host 6379 # Make this instance a replica
REPLICAOF NO ONE # Promote to master
# Cluster commands
CLUSTER NODES # List cluster nodes
CLUSTER INFO # Cluster information
CLUSTER SLOTS # Slot assignments