Redis Cluster Architecture
Redis Cluster Topology
┌─────────────┠┌─────────────┠┌─────────────â”
│ Primary 1 │ │ Primary 2 │ │ Primary 3 │
│ Slots 0- │ │ Slots 5461-│ │ Slots 10923│
│ 5460 │ │ 10922 │ │ 16383 │
└──────┬──────┘ └──────┬──────┘ └──────┬──────┘
│ │ │
┌──────┴──────┠┌──────┴──────┠┌──────┴──────â”
│ Replica 1 │ │ Replica 2 │ │ Replica 3 │
└─────────────┘ └─────────────┘ └─────────────┘
Key Distribution
16384 slots distributed across primary nodes:
- Primary 1: slots 0-5460
- Primary 2: slots 5461-10922
- Primary 3: slots 10923-16383
Each key hashed: HASH = CRC16(key) MOD 16384
Key routes to responsible primary node
Magento Redis Cluster Config
// app/etc/env.php
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Magento\Framework\Cache\Backend\Redis',
'backend_options' => [
'server' => 'redis-cluster.example.com',
'port' => '6379',
'database' => '0',
'compress_data' => '1',
'password' => ''
]
],
'page_cache' => [
'backend' => 'Magento\Framework\Cache\Backend\Redis',
'backend_options' => [
'server' => 'redis-cluster.example.com',
'port' => '6379',
'database' => '1',
'compress_data' => '0'
]
]
]
],
Redis Sentinel for High Availability
Sentinel Architecture
┌─────────────┠┌─────────────â”
│ Sentinel 1 │────▶│ Master │
└─────────────┘ └─────────────┘
┌─────────────┠┌─────────────â”
│ Sentinel 2 │────▶│ Slave 1 │
└─────────────┘ └─────────────┘
┌─────────────┠┌─────────────â”
│ Sentinel 3 │────▶│ Slave 2 │
└─────────────┘ └─────────────┘
Sentinels monitor master, auto-failover on failure
Sentinel Configuration
# sentinel.conf
port 26379
sentinel monitor magento-master 10.0.1.10 6379 2
sentinel down-after-milliseconds magento-master 5000
sentinel failover-timeout magento-master 60000
sentinel parallel-syncs magento-master 1
Magento Sentinel Integration
// Use Sentinel-aware connection
'session' => [
'save' => 'redis',
'redis' => [
'host' => 'sentinel://sentinel1:26379',
'sentinel_master' => 'magento-master',
'timeout' => '2.5',
'database' => '0'
]
],
Memory Management
Redis Memory Configuration
# redis.conf
maxmemory 8gb
maxmemory-policy allkeys-lru
# Memory usage monitoring
redis-cli info memory
# Key metrics:
# used_memory: 6.2GB
# used_memory_rss: 6.8GB
# mem_fragmentation_ratio: 1.09
# evicted_keys: 15234
Memory Usage by Data Type
Data Type | Bytes/Key | Magento Use Case
─────────────|───────────|──────────────────
String | 56 + len | Cache, sessions
Hash | 80 + len | Product data
List | 64 + len | Queue messages
Set | 80 + len | Tag sets
ZSet | 80 + len | Scored data
Memory Optimization
# Check memory usage per database
redis-cli INFO keyspace
redis-cli DBSIZE
# Find memory-hungry keys
redis-cli --bigkeys
redis-cli --memkeys
# Compress large values
# Use zstd compression for cache values
redis-cli CONFIG SET compress-data yes
Eviction Policies
Eviction Policy Comparison
Policy | Description | Use Case
────────────────|────────────────────────────────|──────────────
noeviction | Return errors when full | Critical data
allkeys-lru | LRU across all keys | General cache
volatile-lru | LRU only on keys with TTL | Mixed workloads
allkeys-lfu | LFU across all keys | Hot data caching
volatile-lfu | LFU only on keys with TTL | Mixed with priorities
allkeys-random | Random eviction | Uniform access
volatile-random | Random on keys with TTL | Mixed workloads
volatile-ttl | Evict closest to expire | Time-sensitive data
Magento Eviction Config
// Set eviction policy
'session' => [
'redis' => [
'maxmemory_policy' => 'allkeys-lru',
'compression_threshold' => '2048'
]
],
// Monitor evictions
// redis-cli INFO stats | grep evicted_keys
Key Expiration Strategy
Cache TTL Hierarchy:
- Page cache: 86400s (24h)
- Block cache: 3600s (1h)
- Config cache: 7200s (2h)
- Session: 14400s (4h)
- Full page cache: 86400s (24h)
Quiz
1. How many hash slots does Redis Cluster use?
2. What is Redis Sentinel's primary function?
3. Which eviction policy is best for general caching?
Flashcards
Question
Redis Cluster slots?
Click to reveal answer
Answer
16384 hash slots distributed across primary nodes
Question
Redis Sentinel purpose?
Click to reveal answer
Answer
High availability with automatic failover on master failure
Question
Best eviction policy for caching?
Click to reveal answer
Answer
allkeys-lru: evicts least recently used across all keys
Question
Memory monitoring command?
Click to reveal answer
Answer
redis-cli info memory and redis-cli --bigkeys
Revision Notes
Key Takeaways
- 1. Redis Cluster distributes data across 16384 hash slots
- 2. Redis Sentinel provides automatic failover for high availability
- 3. Monitor memory usage and evictions with redis-cli
- 4. allkeys-lru is best eviction policy for general caching
- 5. Compress large cache values to save memory
Interview Tips
- • Explain Redis Cluster key distribution and slot management
- • Compare Redis Sentinel vs Redis Cluster use cases
- • Discuss memory optimization and eviction strategies
Cheat Sheet
Redis Scaling:
Cluster: 16384 slots, data sharding
Sentinel: HA with auto-failover
Memory:
maxmemory + maxmemory-policy
Monitor: info memory, --bigkeys
Eviction:
allkeys-lru: General cache
volatile-lru: Mixed workloads
noeviction: Critical data
Magento Config:
cache → Redis cluster
sessions → Redis/Sentinel