Redis Cache Setup for Magento 2 Sessions

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

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.