Setting Up Redis for Magento 2 Session Caching
Magento 2 supports two independent Redis connections: one for application cache (blocks, configuration, FPC), another for user sessions. Split into different Redis databases—mandatory practice, otherwise FLUSHDB on cache cleanup deletes all customers' active sessions.
Why Redis for Sessions
By default Magento stores sessions in filesystem (var/session/). As traffic grows, this creates problems:
- thousands of small files on single FS create inode pressure
- file locks on parallel AJAX requests from one user
- on cluster, multiple nodes don't see each other's sessions
Redis solves all three: atomic operations without locks, centralized storage, works with Redis Sentinel/Cluster for HA.
env.php Configuration
Complete configuration in app/etc/env.php:
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'password' => 'strongpassword',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '2', // separate from cache
'compression_threshold' => '2048',
'compression_lib' => 'gzip',
'log_level' => '1',
'max_concurrency' => '6',
'break_after_frontend' => '5',
'break_after_adminhtml' => '30',
'first_lifetime' => '600',
'bot_first_lifetime' => '60',
'bot_lifetime' => '7200',
'disable_locking' => '0',
'min_lifetime' => '60',
'max_lifetime' => '29500',
'sentinel_master' => '',
'sentinel_servers' => '',
'sentinel_connect_retries'=> '5',
'sentinel_verify_master' => '0',
],
],
Application cache—separate block in same env.php:
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '0', // DB 0 for cache
'password' => 'strongpassword',
'compress_data' => '1',
'compress_tags' => '1',
'compression_lib' => 'gzip',
'read_timeout' => '1.5',
],
],
'page_cache' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1', // DB 1 for FPC
'password' => 'strongpassword',
'compress_data' => '0', // FPC uncompressed: more mem, faster
],
],
],
],
Total three Redis databases: 0—cache, 1—FPC, 2—sessions.
Tuning Redis for Magento Load
/etc/redis/redis.conf (Magento-specific parameters):
maxmemory 2gb
maxmemory-policy allkeys-lru
# Sessions shouldn't evict → separate instance
# Better to run two redis: :6379 for cache, :6380 for sessions
save "" # For cache persistence not needed
appendonly no
tcp-keepalive 60
timeout 300
For sessions on separate port /etc/redis/redis-sessions.conf:
port 6380
maxmemory 512mb
maxmemory-policy noeviction # sessions can't evict
appendonly yes # persistence for sessions
appendfsync everysec
Then in env.php for sessions change port to 6380 and database to 0.
Verification
# Ensure sessions written to Redis
redis-cli -p 6380 KEYS "sess_*" | wc -l
# View session content
redis-cli -p 6380 GET "sess_abc123xyz"
# Real-time monitoring
redis-cli -p 6379 MONITOR | grep -i "sess\|cache"
# Cache hit rate
redis-cli -p 6379 INFO stats | grep -E "keyspace_hits|keyspace_misses"
Cache hit rate should exceed 80%. If lower—max_lifetime too small or maxmemory insufficient and LRU evicts fresh records.
Parameter max_concurrency and Locks
max_concurrency = 6 means max 6 parallel requests to single session. For AJAX-heavy pages (filters, compare) increase to 10–15. With disable_locking = 1 remove locks completely—good for read-heavy sessions, but may cause race condition on simultaneous cart adds.
Timeline
Setting two Redis instances, configuring env.php, testing sessions and cache: 1 day. Setting up Redis Sentinel for HA (if required): 1–2 days additional.







