Deep Dive into Redis VII: Commands
๐ Document Overviewโ
Organized by usage frequency and modern patterns
๐ Top 10 Most Used Redis Commandsโ
1. SET - Store Key-Value Pairโ
Purpose: Store string value with optional expiration
Syntax:
SET key value [EX seconds|PX milliseconds] [NX|XX]
Real-life Scenarios:
- User session storage
- Cache with TTL (Time-To-Live)
- Feature flag storage
- Temporary data storage
Best Practices:
# Cache with 60-second expiration
SET user:123:profile "{name: 'John', email: 'john@email.com'}" EX 60
# Set only if key doesn't exist (for locks)
SET lock:order:456 "processing" EX 30 NX
# Set only if key exists (for updates)
SET user:123:status "active" XX
Diagram:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SET Command Flow โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Client Redis Server โ
โ โ โ โ
โ โ SET key value EX 30 โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ>โ โ
โ โ โ โ
โ โ "OK" โ โ
โ โ<โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โ โ (30 seconds later) โ โ
โ โ GET key โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ>โ โ
โ โ (nil) โ โ
โ โ<โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2. GET - Retrieve Key Valueโ
Purpose: Get value of a key
Syntax: GET key
Real-life Scenarios:
- Retrieve cached data
- Read configuration
- Check feature flags
Examples:
# Basic get
GET user:456:profile
# Use with EXISTS check first
EXISTS user:456:profile
GET user:456:profile
# Pattern for cache miss handling
GET product:789
# If nil, fetch from database and SET with EX
3. DEL - Delete Keysโ
Purpose: Remove one or more keys
Syntax: DEL key [key ...]
Best Practices:
# Delete single key
DEL user:123:session
# Delete multiple keys
DEL user:123:profile user:123:preferences user:123:cart
# Safer deletion with EXISTS check
EXISTS important:config
DEL important:config
# Use UNLINK for large keys in production (non-blocking)
UNLINK large:dataset:456
4. INCR/DECR - Atomic Countersโ
Purpose: Increment/decrement integer values atomically
Real-life Scenarios:
- Page view counters
- Rate limiting
- Inventory management
- Unique ID generation
Examples:
# Basic counter
SET page:views 0
INCR page:views # Returns 1
INCR page:views # Returns 2
# Rate limiting pattern
INCR rate:limit:192.168.1.1
EXPIRE rate:limit:192.168.1.1 60
# Decrement inventory
SET product:789:stock 100
DECR product:789:stock # Returns 99
# Increment by specific amount
INCRBY user:123:score 50
DECRBY user:123:score 20
Rate Limiting Diagram:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Rate Limiting Pattern โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Minute 1 โ Minute 2 โ Minute 3 โ
โ โโโโโโโโโโโ โ โโโโโโโโโโโ โ โโโโโโโโโโโ โ
โ โRequest 1โ โ โRequest 1โ โ โRequest 1โ โ
โ โINCR key โ โ โINCR key โ โ โINCR key โ โ
โ โEXPIRE โ โ โEXPIRE โ โ โEXPIRE โ โ
โ โโโโโโโโโโโ โ โโโโโโโโโโโ โ โโโโโโโโโโโ โ
โ โRequest 2โ โ โRequest 2โ โ โ
โ โINCRโ2 โ โ โINCRโ2 โ โ โ
โ โโโโโโโโโโโ โ โโโโโโโโโโโ โ โ
โ โRequest 3โ โ โRequest 3โ โ โ
โ โINCRโ3 โ โ โINCRโ3 โ โ โ
โ โโโโโโโโโโโ โ โโโโโโโโโโโ โ โ
โ โRequest 4โ โ โRequest 4โ โ โ
โ โINCRโ4 โ โ โGETโ4 โ โ โ
โ โIF >3 โ โ โIF >3 โ โ โ
โ โReject โ โ โReject โ โ โ
โ โโโโโโโโโโโ โ โโโโโโโโโโโ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
5. EXPIRE - Set Key Expirationโ
Purpose: Set timeout on key
Syntax: EXPIRE key seconds
Modern Alternatives:
# Set with expiration in one command (preferred)
SET session:abc123 "user_data" EX 3600
# Update existing key's TTL
EXPIRE cache:key 300
EXPIREAT cache:key 1672531200 # Unix timestamp
# Check remaining TTL
TTL session:abc123
# Returns: -2 (key doesn't exist)
# Returns: -1 (key exists, no expire)
# Returns: >0 (seconds remaining)
# Remove expiration
PERSIST important:config
6. HSET/HGET - Hash Operationsโ
Purpose: Store field-value pairs within a key
Real-life Scenarios:
- User profiles
- Product information
- Object storage
- Partial updates
Examples:
# Store user profile
HSET user:123 name "John Doe" email "john@email.com" age 30
# Get specific fields
HGET user:123 name # "John Doe"
HGET user:123 age # "30"
# Get all fields
HGETALL user:123
# Update single field
HSET user:123 age 31
# Check if field exists
HEXISTS user:123 email
# Increment numeric field
HINCRBY user:123 visits 1
# Get multiple fields
HMGET user:123 name email phone
Hash vs String Storage:
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ String Storage โ โ Hash Storage โ
โโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโค
โ โ โ โ
โ user:123 โ โ user:123 โ
โ โโโโโโโโโโโโ โ โ โโโโโโโโโโโโ โ
โ { โ โ name: "John" โ
โ "name": โ โ email: "john@" โ
โ "John", โ โ age: "30" โ
โ "email": โ โ โ
โ "john@...", โ โ โ
โ "age": 30 โ โ โ
โ } โ โ โ
โ โ โ โ
โ Memory: 100B โ โ Memory: 80B โ
โ โ โ (more efficientโ
โ โ โ for many flds)โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
7. LPUSH/RPUSH - List Operationsโ
Purpose: Add elements to lists
Real-life Scenarios:
- Message queues
- Activity feeds
- Recent items
- Job queues
Examples:
# Message queue pattern
LPUSH messages:queue "{job: 'process', data: {...}}"
LPUSH messages:queue "{job: 'email', data: {...}}"
# Worker processing
RPOP messages:queue # Gets oldest item
# Recent activities
LPUSH user:123:activity "Logged in"
LPUSH user:123:activity "Viewed product:789"
LPUSH user:123:activity "Purchased item"
# Get range (last 10 activities)
LRANGE user:123:activity 0 9
# Fixed-length list (news feed)
LPUSH feed:global "News item 1"
LTRIM feed:global 0 99 # Keep only 100 items
Message Queue Pattern:
โโโโโโโโโโโโโโโ LPUSH โโโโโโโโโโโโโโโ RPOP/BLPOP โโโโโโโโโโโโโโโ
โ Producer โโโโโโโโโโโโ>โ Redis โ<โโโโโโโโโโโโโโโโ Consumer โ
โ โ โ List โ โ โ
โ โ โ โโโโโโโโโ โ โ โ
โ โ โ โ Msg 3 โ โ โ โ
โ โ โ โ Msg 2 โ โ โ โ
โ โ โ โ Msg 1 โ โ โ โ
โ โ โ โโโโโโโโโ โ โ โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ โ โ
โ โ โ
โ (FIFO Queue) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
8. SADD/SMEMBERS - Set Operationsโ
Purpose: Store unique unordered elements
Real-life Scenarios:
- Tags/categories
- Unique visitors
- Friends/followers
- Vote tracking
Examples:
# Store tags
SADD article:456:tags "redis" "database" "nosql" "cache"
# Check membership
SISMEMBER article:456:tags "redis" # Returns 1 (true)
# Get all members
SMEMBERS article:456:tags
# Set operations (powerful!)
SADD users:online "user1" "user2" "user3"
SADD users:active "user2" "user3" "user4"
# Intersection (users both online AND active)
SINTER users:online users:active # ["user2", "user3"]
# Union (all unique users)
SUNION users:online users:active # ["user1","user2","user3","user4"]
# Difference (online but not active)
SDIFF users:online users:active # ["user1"]
# Random element (lottery, sampling)
SRANDMEMBER users:online
SPOP users:online # Removes and returns random
9. ZADD/ZRANGE - Sorted Set Operationsโ
Purpose: Store sorted unique elements with scores
Real-life Scenarios:
- Leaderboards
- Time-series data
- Priority queues
- Range queries
Examples:
# Leaderboard
ZADD leaderboard 1000 "player1"
ZADD leaderboard 850 "player2"
ZADD leaderboard 920 "player3"
# Get top 3 players
ZREVRANGE leaderboard 0 2 WITHSCORES
# Get player rank (0-indexed from top)
ZREVRANK leaderboard "player2"
# Increment score
ZINCRBY leaderboard 50 "player1"
# Range queries (scores 800-900)
ZRANGEBYSCORE leaderboard 800 900
# Time-series pattern
ZADD temperature:nyc 1672444800 "38.5" # timestamp, value
ZADD temperature:nyc 1672448400 "39.2"
ZRANGEBYSCORE temperature:nyc 1672444800 1672448400
Leaderboard Architecture:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Redis Sorted Set Leaderboard โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Score โ Member โ Operations โ
โโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโค
โ 1050 โ player1 โ ZADD score โ
โ 920 โ player3 โ ZINCRBY +50 โ
โ 850 โ player2 โ ZREVRANGE 0 2 โ
โ 720 โ player4 โ ZREVRANK player2 โ
โ 650 โ player5 โ ZRANGEBYSCORE โ
โ โ โ 800 1000 โ
โโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโ
โฒ
โ
โโโโ Scores automatically sorted,
O(log N) operations
10. SCAN - Safe Key Iterationโ
Purpose: Incrementally iterate keys without blocking
Why not KEYS?
KEYS *blocks Redis (use in production with extreme caution)SCANis non-blocking, production-safe
Examples:
# Iterate through all keys matching pattern
SCAN 0 MATCH user:* COUNT 100
# Iterate specific type
SCAN 0 MATCH product:* COUNT 50 TYPE string
# Hash scan
HSCAN user:123 0 MATCH *name* COUNT 10
# Set scan
SSCAN tags:article:456 0 COUNT 20
# Sorted set scan
ZSCAN leaderboard 0 COUNT 15
SCAN Iteration Pattern:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SCAN Iteration Process โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Initial: SCAN 0 MATCH user:* COUNT 5 โ
โ Returns: โ
โ 1) "5" (next cursor) โ
โ 2) ["user:1", "user:2", "user:3"] โ
โ โ
โ Next: SCAN 5 MATCH user:* COUNT 5 โ
โ Returns: โ
โ 1) "12" (next cursor) โ
โ 2) ["user:4", "user:5"] โ
โ โ
โ Next: SCAN 12 MATCH user:* COUNT 5 โ
โ Returns: โ
โ 1) "0" (iteration complete) โ
โ 2) ["user:6"] โ
โ โ
โ Cursor 0 = Done โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐๏ธ Data Structure Selection Guideโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Choosing the Right Data Structure โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโค
โ Use Case โ Structure โ Best For โ Example โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโค
โ Simple Cache โ String โ Single vals โ SET/GET โ
โ โ โ With TTL โ user:session โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโค
โ Object Store โ Hash โ Multiple โ HSET/HGETALL โ
โ โ โ fields โ user:profile โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโค
โ Queue โ List โ FIFO/LIFO โ LPUSH/RPOP โ
โ โ โ โ jobs:queue โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโค
โ Unique Items โ Set โ Membership โ SADD/SISMEMBERโ
โ โ โ Operations โ article:tags โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโค
โ Rankings โ Sorted Set โ Scores โ ZADD/ZRANGE โ
โ โ โ Ranges โ leaderboard โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโค
โ Time Series โ Sorted Set โ Timestamps โ ZADD ts:data โ
โ โ or Stream โ Sequences โ XADD log:streamโ
โโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
โก Advanced & Modern Patternsโ
11. Pipeline - Batch Operationsโ
Purpose: Reduce round-trip time
# Without pipeline (3 round trips)
SET user:1:name "John"
SET user:1:email "john@email.com"
SET user:1:age 30
# With pipeline (1 round trip)
MULTI
SET user:1:name "John"
SET user:1:email "john@email.com"
SET user:1:age 30
EXEC
12. Transactions - Atomic Operationsโ
MULTI
GET account:123:balance
DECRBY account:123:balance 100
INCRBY account:456:balance 100
EXEC
13. Lua Scripting - Complex Atomic Operationsโ
EVAL "local current = redis.call('GET', KEYS[1])
if tonumber(current) < tonumber(ARGV[1]) then
return redis.call('INCR', KEYS[1])
else
return nil
end" 1 rate:limit:ip 10
14. Streams - Modern Message Queueโ
# Producer
XADD mystream * sensor-id 1234 temperature 19.8
# Consumer group
XGROUP CREATE mystream mygroup 0
# Consumer
XREADGROUP GROUP mygroup consumer1 COUNT 1 BLOCK 5000 STREAMS mystream >
๐ Security & Production Commandsโ
15. AUTH - Authenticationโ
AUTH your_strong_password_here
# Better: Use Redis 6+ ACL
ACL SETUSER alice on >password ~cache:* +get +set
16. CONFIG - Configuration Managementโ
# Get configuration
CONFIG GET maxmemory
CONFIG GET requirepass
# Set configuration (prefer redis.conf for persistence)
CONFIG SET maxmemory 2gb
CONFIG SET save "900 1 300 10 60 10000"
# Never expose these in production:
CONFIG SET requirepass "" # DON'T!
CONFIG SET protected-mode no # DON'T!
17. INFO - Monitoringโ
# Get all info (verbose)
INFO
# Get specific sections
INFO memory
INFO stats
INFO clients
INFO replication
# Critical metrics to monitor:
INFO memory # used_memory, maxmemory, evictions
INFO stats # instantaneous_ops_per_sec, keyspace_hits/misses
INFO persistence # rdb_last_save_time, aof_rewrite_in_progress
๐ฏ Memory Optimization Commandsโ
18. MEMORY USAGEโ
MEMORY USAGE large:key
MEMORY STATS
MEMORY PURGE # Attempts to defragment (Redis 4+)
19. Efficient Data Encodingโ
# Use appropriate data types
# Strings under 44 bytes use EMBSTR encoding
SET small:key "short" # More efficient
# Hashes with <= 512 fields use ziplist
HSET config:app field1 "val1" ... field500 "val500"
# Lists with small elements use ziplist
LPUSH recent:items "item1" "item2" # Check list-max-ziplist* configs
๐ Performance Optimization Patternsโ
Connection Pooling Diagram:โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ App โ โ Connection โ โ Redis โ
โ Server โโโโโโโถโ Pool โโโโโโโถโ Server โ
โ โ โ โ โ โ
โ Request 1 โ โ โโโโโโโโโโโโโ โ โ โ
โ โโโโโโโโโ โ โ โ Connectionโโโโผโโโโโโถโ Process โ
โ GET key โ โ โ 1 โ โ โ Request โ
โ โ โ โโโโโโโโโโโโโ โ โ โ
โ Request 2 โ โ โโโโโโโโโโโโโ โ โ โ
โ โโโโโโโโโ โ โ โ Connectionโโโโผโโโโโโถโ Process โ
โ SET key โโโโโฌโโถโ โ 2 โ โ โ Request โ
โ โ โ โ โโโโโโโโโโโโโ โ โ โ
โ Request 3 โ โ โ โโโโโโโโโโโโโ โ โ โ
โ โโโโโโโโโ โ โโโถโ โ Connectionโโโโผโโโโโโถโ Process โ
โ INCR key โ โ โ 3 โ โ โ Request โ
โ โ โ โโโโโโโโโโโโโ โ โ โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
20. Monitor Slow Logsโ
# Configure slow log
CONFIG SET slowlog-log-slower-than 10000 # 10ms
CONFIG SET slowlog-max-len 128
# View slow logs
SLOWLOG GET 10
SLOWLOG LEN
SLOWLOG RESET
๐ซ Anti-Patterns to Avoidโ
Bad Patterns:โ
# 1. KEYS * in production (use SCAN instead)
KEYS * # BLOCKS SERVER!
# 2. Large values in single key
SET huge:key "[...10MB of data...]" # Blocks during serialization
# 3. Not using TTL for cache
SET cache:key "data" # Might fill memory
# 4. Using Redis as primary database
# Redis is in-memory - data can be lost
# 5. Not monitoring memory usage
# Leads to OOM (Out Of Memory) errors
Good Patterns:โ
# 1. Use pipelining for bulk operations
MULTI
SET key1 "val1"
SET key2 "val2"
EXEC
# 2. Use appropriate data structures
# For object with many fields: Hash, not multiple String keys
# 3. Always set TTL for cache
SET session:abc "data" EX 3600
# 4. Use Lua scripts for complex atomic operations
# 5. Monitor with INFO and SLOWLOG
๐ Replication & High Availability Commandsโ
21. Replication Managementโ
# On replica
REPLICAOF master-ip 6379
REPLICAOF NO ONE # Promote to master
# Check replication status
INFO replication
# Monitor replication lag
redis-cli --latency
22. Sentinel & Cluster Commandsโ
# Sentinel
SENTINEL MASTERS
SENTINEL GET-MASTER-ADDR-BY-NAME mymaster
# Cluster
CLUSTER NODES
CLUSTER INFO
CLUSTER SLOTS
๐งน Maintenance Commandsโ
23. Backup & Persistenceโ
# Manual save (blocks)
SAVE # Synchronous, blocks
BGSAVE # Asynchronous, forks
# Check persistence status
INFO persistence
LASTSAVE # Unix timestamp of last save
# AOF operations
BGREWRITEAOF
24. Cleanup & Debuggingโ
# Flush data (CAUTION!)
FLUSHDB # Current database
FLUSHALL # All databases
# Debug object
DEBUG OBJECT keyname
# Monitor commands in real-time
MONITOR # Use sparingly, verbose
# Client management
CLIENT LIST
CLIENT KILL addr ip:port
CLIENT PAUSE timeout_ms # Pause clients
๐ Monitoring Dashboard Metricsโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Redis Health Dashboard โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโค
โ Memory Usage โ Command Stats โ Connections โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ Used: 1.2GB/2GBโ Ops/sec: 5,000 โ Connected: 45 โ
โ 60% โ Hits: 98% โ Max: 10,000 โ
โ Peak: 1.8GB โ Misses: 2% โ Rejected: 0 โ
โ Evictions: 0 โ Cmd/sec: 8,000 โ Timeout: 0 โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ Replication โ Persistence โ Keyspace โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโค
โ Role: Master โ RDB: Enabled โ DB0 Keys: 45K โ
โ Slaves: 2 โ Last Save: 2m โ Expires: 12K โ
โ Lag: 0.5s โ AOF: Enabled โ Avg TTL: 300s โ
โ Status: OK โ Size: 500MB โ Hit Rate: 99% โ
โโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโ
๐ Quick Reference Cheat Sheetโ
Caching Patterns:โ
# Cache-aside pattern
1. GET cache:key
2. If nil, fetch from DB
3. SET cache:key value EX 300
# Write-through pattern
1. Update DB
2. SET cache:key new_value EX 300
# Write-behind pattern
1. SET cache:key new_value EX 300
2. Async update to DB
Key Design Best Practices:โ
Good: Bad:
user:123:profile user_profile_123
session:abc123 abc123_session
product:789:views product_views_789
geo:nyc:users newyork_users_list
Pattern: object_type:id:field
TTL Strategies:โ
# Cache: 5 minutes - 1 hour
SET cache:key value EX 300
# Session: 30 minutes - 24 hours
SET session:key data EX 1800
# Rate limit: 1 second - 1 minute
SET rate:limit:ip 1 EX 60 NX
# Leaderboard: 1 day - 7 days
EXPIRE leaderboard:daily 86400
๐ Troubleshooting Commandsโ
25. Diagnose Issuesโ
# Check if Redis is alive
PING # Should return PONG
# Check latency between commands
redis-cli --latency
# Monitor commands in real-time (debug)
MONITOR
# Check memory fragmentation
INFO memory | grep fragmentation
# List connected clients
CLIENT LIST
# Check slow queries
SLOWLOG GET 5
26. Performance Testingโ
# Benchmark tool (outside redis-cli)
redis-benchmark -t set,get -n 100000 -q
# Pipeline benchmark
redis-benchmark -t set,get -n 100000 -q -P 16
๐จ Visualization: Data Structure Memory Layoutโ
String:
โโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโ
โ Header โ Data โ Null โ
โ Type โ "value"โ term โ
โโโโโโโโโโดโโโโโโโโโดโโโโโโโโโ
Hash (ziplist encoding):
โโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโฌโโโโโ
โ ZL โ F1 โ V1 โ F2 โ V2 โ ZL โ
โbytesโ"name"โ"John"โ"age"โ"30"โend โ
โโโโโโดโโโโโดโโโโโดโโโโโดโโโโโดโโโโโ
List (quicklist):
โโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโ
โ Node โ Node โ Node โ
โ [ziplistโ [ziplistโ [ziplistโ
โ "item1" โ "item4" โ "item7" โ
โ "item2" โ "item5" โ โ
โ "item3" โ "item6" โ โ
โโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโ
๐ Further Learning Pathโ
Next Steps:โ
- Redis Streams - Modern message queues
- Redis Modules - RediSearch, RedisJSON, RedisGraph
- Redis Cluster - Horizontal scaling
- Redis Sentinel - High availability
- Redis Enterprise - Commercial features
Production Checklist:โ
- Set
maxmemoryand eviction policy - Enable persistence (RDB/AOF)
- Configure backups
- Set up monitoring
- Implement connection pooling
- Use key expiration
- Regular maintenance
- Disaster recovery plan
Remember: Redis is single-threaded for commands. One slow command blocks all others. Always test commands in staging before production, and monitor performance metrics regularly.