Redis Cache Backend
env.php Configuration
// app/etc/env.php
return [
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Magento\Framework\Cache\Backend\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '0',
'compress_data' => '1'
]
],
'page_cache' => [
'backend' => 'Magento\Framework\Cache\Backend\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0'
]
]
]
]
];
Redis Databases
| Database | Purpose |
|---|---|
| 0 | Default cache |
| 1 | Full page cache |
| 2 | Session storage |
| 3-15 | Custom use |
Setup Command
php bin/magento setup:config:set \
--cache-backend=redis \
--cache-backend-redis-server=127.0.0.1 \
--cache-backend-redis-port=6379 \
--cache-backend-redis-db=0
Redis Session Storage
Session Configuration
// app/etc/env.php
return [
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'database' => '2',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'log_level' => '3',
'max_concurrency' => '20',
'breaker' => [
'timeout' => '15',
'sleep' => '1'
]
]
]
];
Setup Command
php bin/magento setup:config:set \
--session-save=redis \
--session-save-redis-host=127.0.0.1 \
--session-save-redis-port=6379 \
--session-save-redis-db=2
Session Performance
// Redis session vs file session
// File: ~5-10ms per read/write
// Redis: ~0.5-1ms per read/write
// For 100 concurrent users:
// File: 500-1000ms total
// Redis: 50-100ms total
Redis Full Page Cache
FPC Configuration
// app/etc/env.php
return [
'cache' => [
'frontend' => [
'page_cache' => [
'backend' => 'Magento\Framework\Cache\Backend\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0',
'compression_lib' => ''
]
]
]
]
];
FPC with Redis
// Enable FPC
php bin/magento config:set catalog/frontend/flat_catalog 1
// Check FPC hit rate
php bin/magento cache:status | grep fpc
Redis Memory Management
# Check Redis memory usage
redis-cli info memory
# Set memory limit
redis-cli config set maxmemory 2gb
# Set eviction policy
redis-cli config set maxmemory-policy allkeys-lru
Redis Performance Optimization
Redis Configuration
# /etc/redis/redis.conf
maxmemory 4gb
maxmemory-policy allkeys-lru
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
Monitoring
# Check Redis status
redis-cli info
# Monitor commands
redis-cli monitor
# Check slow log
redis-cli slowlog get 10
# Check connected clients
redis-cli client list
Connection Pooling
// env.php - persistent connections
'cache' => [
'frontend' => [
'default' => [
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '0',
'compress_data' => '1',
'compression_lib' => 'gzip'
]
]
]
]
Performance Metrics
| Metric | File Backend | Redis Backend |
|---|---|---|
| Read time | 5-10ms | 0.5-1ms |
| Write time | 5-10ms | 0.5-1ms |
| Memory | Disk-based | RAM-based |
| Concurrency | File locks | Lock-free |
| Scalability | Limited | Horizontal |
Practice Problems
Migrate a Magento store from file-based cache to Redis. Plan the migration steps.
Quiz
1. What Redis database is used for default cache?
2. What is the recommended eviction policy for Redis with Magento?
3. How much faster is Redis vs file-based cache?
4. What env.php key configures Redis sessions?
Flashcards
Question
What Redis database is used for sessions?
Click to reveal answer
Answer
Database 2
Question
How to enable Redis cache backend?
Click to reveal answer
Answer
Set cache.frontend.default.backend to Magento\Framework\Cache\Backend\Redis
Question
What is the recommended Redis eviction policy?
Click to reveal answer
Answer
allkeys-lru for Magento cache
Question
How to check Redis memory usage?
Click to reveal answer
Answer
redis-cli info memory
Question
What Redis feature benefits sessions most?
Click to reveal answer
Answer
In-memory storage with sub-millisecond latency
Revision Notes
Key Takeaways
- 1. Redis databases: 0=default cache, 1=FPC, 2=sessions
- 2. Configure in env.php under cache.frontend and session keys
- 3. Redis provides 5-10x faster cache operations than file backend
- 4. Set maxmemory and allkeys-lru eviction policy
- 5. Monitor with redis-cli info, monitor, and slowlog commands
- 6. Use compress_data for default cache but not FPC
Interview Tips
- • Explain Redis database assignment for Magento
- • Describe the performance benefits of Redis over file cache
- • Discuss Redis memory management and eviction
- • Know the env.php configuration structure
Cheat Sheet
Redis Cheat Sheet
Databases:
- 0: default cache
- 1: FPC
- 2: sessions
env.php:
'cache' => ['frontend' => ['default' => [
'backend' => '...Redis',
'backend_options' => ['server' => '127.0.0.1', 'port' => '6379', 'database' => '0']
]]]
Setup:php bin/magento setup:config:set --cache-backend=redis
Monitor:
redis-cli info
redis-cli monitor
redis-cli slowlog get 10
Performance: 0.5-1ms vs 5-10ms (file)