Skip to main content
Back to Blog
MagentoJune 10, 20258 min read

How to Set Up Redis Caching for Magento 2

Learn how to configure Redis caching for Magento 2 to dramatically reduce page load times, offload database queries, and improve storefront performance under heavy traffic.

Introduction

Redis is an in-memory data store that serves as one of the most effective performance upgrades for any Magento 2 installation. Out of the box, Magento uses the filesystem for cache storage and database tables for session management. Both approaches become bottlenecks under load. Redis eliminates these bottlenecks by keeping cache entries and session data in RAM, reducing read latency from milliseconds to microseconds.

This guide walks through installing Redis, configuring Magento 2 to use it for both default cache and full-page cache, moving sessions to Redis, and verifying that everything works correctly.

Prerequisites

  • Magento 2.4.x installed on Ubuntu 22.04 or 24.04
  • Root or sudo access to the server
  • PHP 8.2 or 8.3 with the phpredis extension

Installing Redis on Ubuntu

sudo apt update
sudo apt install -y redis-server

# Enable and start Redis
sudo systemctl enable redis-server
sudo systemctl start redis-server

# Verify Redis is running
redis-cli ping
# Expected output: PONG

Adjust the Redis configuration at /etc/redis/redis.conf to set a reasonable memory limit:

sudo sed -i 's/^# maxmemory .*/maxmemory 512mb/' /etc/redis/redis.conf
sudo sed -i 's/^# maxmemory-policy .*/maxmemory-policy allkeys-lru/' /etc/redis/redis.conf
sudo systemctl restart redis-server

Configuring Magento 2 Default Cache

Edit the app/etc/env.php file in your Magento root directory to point the default cache backend at Redis:

'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',
            ],
        ],
    ],
],

Using separate database numbers (0 for default, 1 for full-page cache) keeps cache types isolated and allows independent flushing.

Moving Sessions to Redis

Session storage in Redis prevents session locking issues and improves checkout performance. Add the following to app/etc/env.php:

'session' => [
    'save' => 'redis',
    'redis' => [
        'host' => '127.0.0.1',
        'port' => '6379',
        'database' => '2',
        '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',
    ],
],

After updating the configuration, flush the cache:

bin/magento cache:flush
bin/magento cache:status

Verification and Monitoring

Confirm Redis is actively receiving keys:

redis-cli info keyspace
# Should show db0, db1, db2 with active keys

redis-cli monitor
# Watch real-time commands (Ctrl+C to stop)

For production monitoring, consider integrating Redis metrics into your existing observability stack via Prometheus and the Redis exporter.

For more tips on improving Magento storefront speed, read our guide on boosting Magento 2 performance. If you need hands-on help, explore our Magento 2 speed optimization service.

Redis caching transforms Magento 2 performance by moving cache, full-page cache, and sessions from disk and database into memory. The setup is straightforward and the results are immediate. Monitor your Redis instance, set appropriate memory limits, and enjoy a significantly faster storefront.

Need help with this?

Our team handles this kind of work daily. Let us take care of your infrastructure.