How to Configure Redis Caching for Magento 2 Sessions and Cache
Configure Redis for Magento 2 by editing app/etc/env.php to use Cm_Cache_Backend_Redis for cache backends and setting session.save to redis, using separate databases (0, 1, 2) for default cache, page cache, and sessions respectively.
Magento 2 supports Redis as a high-performance backend for both session storage and full-page caching, significantly reducing database load and improving response times. The community-curated resource list at aleron75/mageres documents essential patches and monitoring tools that optimize Redis integration for production environments. This guide shows you exactly how to configure Redis caching for Magento 2 sessions and cache using production-tested settings from the repository.
Why Use Redis for Magento 2?
Redis provides an in-memory key/value store that outperforms traditional file or database storage for Magento 2's ephemeral data:
- Sub-millisecond latency for read/write operations reduces page load times
- Built-in TTL and expiration support aligns perfectly with session lifetimes and cache invalidation
- Shared storage across web nodes enables true load-balanced sessions without sticky sessions
Configuring Redis in app/etc/env.php
The primary configuration file for Redis integration is app/etc/env.php. This PHP return array defines both the cache frontend backends and the session save handler.
<?php
return [
'backend' => [
'frontName' => 'admin'
],
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '0',
'compress_data' => '1'
]
],
'page_cache' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0',
'lifetimelimit' => '57600'
]
]
]
],
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '2',
'compression_threshold' => '2048',
'compression_library' => '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' => '2592000'
]
]
];
Cache Backend Configuration
Magento 2 uses two distinct cache frontends: default for generic cache metadata and page_cache for full-page cache storage. Both utilize the Cm_Cache_Backend_Redis class from the cm/cache-backend-redis package.
database: Assign0for default cache and1for page cache to maintain key separationcompress_data: Enable (1) for default cache to reduce network overhead; disable (0) for page cache to maximize throughputlifetimelimit: Set to57600seconds (16 hours) for page cache entries to balance freshness with hit rates
Session Storage Configuration
The session configuration uses the native redis save handler with parameters distinct from the cache backends. According to the source analysis from aleron75/mageres, proper session tuning requires attention to locking behavior.
database: Use2to isolate session keys from cache datadisable_locking: Keep at0for standard installations; set to1only when using the community session lock patchmax_concurrency: Controls how many concurrent requests can wait for a session lock before breaking (default6)break_after_frontendandbreak_after_adminhtml: Define wait timeouts in seconds for frontend (5) and admin (30) sessions
Optimizing Session Performance with the Redis Patch
By default, Magento 2 applies a read lock for every session write operation, which creates bottlenecks under high concurrency. The Magento 2 Redis session patch documented in aleron75/mageres at line 147 removes these unnecessary read locks, improving throughput significantly.
To install the patch:
composer require olivertar/m2_redis_patch
bin/magento setup:upgrade
bin/magento cache:clean
After installation, change disable_locking to '1' in the env.php session configuration to prevent conflicts with the optimized locking mechanism.
Monitoring Redis Health
The Admin Redis Report module referenced in aleron75/mageres at line 294 provides real-time visualization of Redis health statistics directly within the Magento admin panel, eliminating the need for command-line monitoring.
Install the module via Composer:
composer require element119/module-admin-redis-report
bin/magento module:enable Element119_AdminRedisReport
bin/magento setup:upgrade
After deployment, navigate to System → Tools → Redis Report to view memory usage, hit rates, and connection statistics.
Prerequisites and Server Setup
Before modifying app/etc/env.php, ensure your infrastructure meets these requirements:
- Redis Server (version 5.0 or higher) installed and network-accessible from Magento web nodes
- PHP Redis Extension (
php-redispackage orpecl install redis) enabled in your PHP configuration - Firewall rules allowing communication between web nodes and Redis on port
6379(or your configured port)
Summary
- Configure Redis in
app/etc/env.phpusingCm_Cache_Backend_Redisfor cache andsave => 'redis'for sessions - Use separate Redis databases (
0,1,2) to prevent key collisions between default cache, page cache, and sessions - Install the olivertar/m2_redis_patch to eliminate session read locks and improve concurrency under high traffic
- Deploy the element119/module-admin-redis-report for visual monitoring of Redis health metrics within the admin panel
Frequently Asked Questions
What is the recommended Redis database numbering scheme for Magento 2?
Use database 0 for the default cache, database 1 for the full-page cache, and database 2 for sessions. This separation prevents key namespace collisions and allows you to flush the cache independently without destroying active user sessions.
How does the Redis session patch improve performance?
The patch removes unnecessary read locks that Magento 2 applies by default during session writes. By eliminating these locks, the patch increases concurrent request handling capacity and reduces latency under high-traffic conditions, as documented in the aleron75/mageres repository.
Can I use the same Redis instance for both cache and sessions?
Yes, you can use a single Redis instance by assigning different database numbers to each service. However, for high-traffic production environments, consider deploying separate Redis instances or using Redis Cluster to prevent resource contention and ensure predictable performance.
How do I verify Redis is working correctly after configuration?
Verify functionality by checking that keys populate in the assigned databases using redis-cli -n 2 KEYS 'sess_*' for sessions. Additionally, confirm reduced MySQL session table activity and check the Admin Redis Report dashboard under System → Tools for connection metrics and memory utilization.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →