Magento 2 Varnish & Redis Setup

⚡ Magento 2 Varnish & Redis Setup – Supercharge Your Store Speed

Want to make your Magento 2 store fly? Varnish and Redis are your best performance buddies. Varnish handles blazing-fast full-page caching, while Redis supercharges your sessions and backend cache. Let’s set them up step-by-step!

🚀 What Are Varnish & Redis?

  • Varnish – A reverse proxy that serves full-page cache super fast
  • Redis – An in-memory data store used for caching and session storage

🧰 Prerequisites

  • Magento 2 installed
  • Varnish & Redis installed on your server

🧱 Step 1: Configure Redis in Magento 2

Add the following to your env.php file for cache and session storage:

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '127.0.0.1',
                'port' => '6379',
                'database' => '0'
            ]
        ],
        'page_cache' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '127.0.0.1',
                'port' => '6379',
                'database' => '1'
            ]
        ]
    ]
],
'session' => [
    'save' => 'redis',
    'redis' => [
        'host' => '127.0.0.1',
        'port' => '6379',
        'database' => '2',
        'timeout' => '2.5'
    ]
]

Try It Now

🧱 Step 2: Enable Varnish in Magento

  1. Go to Stores > Configuration > Advanced > System > Full Page Cache
  2. Set Caching Application to Varnish Cache
  3. Save Config and click Export VCL for Varnish 6

🔧 Step 3: Apply VCL to Varnish

Replace Varnish’s default config (usually in /etc/varnish/default.vcl) with the exported VCL file and restart Varnish:

sudo cp /path/to/exported.vcl /etc/varnish/default.vcl
sudo systemctl restart varnish

Try It Now

🚀 Step 4: Test Redis & Varnish

  • Check Redis: redis-cli monitor
  • Check Varnish headers using curl -I http://yourdomain.com and look for X-Magento-Cache-Debug: HIT

✅ Summary

By setting up Varnish and Redis in Magento 2, you unlock lightning-fast performance and better scalability. Your store is now optimized to handle more users and run smoother than ever!