Configure Magento 2 to Use Redis for Cache & Session Storage

Configure Magento 2 to Use Redis for Cache & Session Storage

[Updated on April 8, 2025] Looking for a way to speed up your Magento 2 store? Configure Magento 2 with Redis to replace the default cache. Users can set up a Redis page and default caching for better store performance.

This tutorial covers the advantages and setting up Redis for Magento 2.

Key Takeaways

  • Redis keeps Magento 2 cache data in memory, so pages load faster for your visitors.

  • Magento 2 has built-in support for Redis to manage cache and session data.

  • File-based caching slows things down, especially when your store gets more traffic.

  • 5 steps to connect Redis as the main cache storage for their store.

  • 5 Steps show how to move session storage to Redis for better performance.

  • A troubleshooting section walks you through common Redis issues and how to fix them.

Overview of Redis: A High-Performance Database

Redis is a lightweight database and handles caching, data storage, and message brokering.

Redis keeps data in RAM faster than databases using hard drives. It manages thousands of read and write requests, which is ideal for quick operations. It supports data types like:

  • Strings

  • Lists

  • Sets

  • Hashes.

This makes Redis versatile for cache and session storage in Magento 2. By setting up Redis, developers can build web apps faster. Redis outperforms traditional page cache systems by storing everything in memory.

Why Is Redis So Fast: Advantages of Using Redis with Magento 2

1. In-Memory Data Storage

Redis speeds up Magento 2 with in-memory data storage

Redis stores data in RAM faster than disk-based storage like SSDs or HDDs. RAM access is much quicker, making Redis ideal for Magento 2 caching. Performance depends on your Magento’s server resources, such as CPU and RAM.

2. Optimized Data Structures

Redis uses smart data structures. For example, it has lists that let you add items in O(1) time. It also features sorted sets for ranking. These boost Magento caching performance, especially during busy times. Results can change depending on your server setup.

3. Single-Threaded Event Loop

Redis uses a single-threaded event loop. This setup processes commands without delays from thread-switching. It makes Redis reliable for Magento 2. Newer versions (6.0+) offer optional multi-threading for I/O.

4. Asynchronous I/O Operations

Redis handles input and output without waiting for each task to finish. It manages many requests at once, not pausing for one to complete before starting another. This is key for busy Magento stores.

5. Pipelining

Redis supports pipelining, letting clients send many commands in one request. This cuts the wait time for responses. Pipelining speeds up the Redis page cache, especially for many commands.

6. Memory Management

Redis uses memory management techniques, like eviction policies, to manage resources. It deletes older or less-used data when memory runs low, using rules like Least Recently Used (LRU). It compresses data when possible. Redis runs even with large datasets.

7. Minimal Network Overhead

Redis reduces network overhead in Magento 2

Redis uses a simple, lightweight protocol that reduces network delays. This helps data move faster between the server and the client. Redis responds, making it valuable for Magento on a server or Amazon Cloud.

Magento 2 and Redis: Enhancing Store Performance

Magento 2 has Redis support built-in. You can set up Redis for cache, sessions, and full-page cache for better speed and stability.

1. Native Support with No Extra Steps

Redis works out of the box with Magento 2. Redis works out of the box with Magento 2. You do not need any third-party tools.

2. Organized Cache Handling

Magento groups cache entries into types, like layouts, configuration, and HTML blocks. As your product catalog grows, the cache expands. Redis manages large cache groups faster than the file system.

3. Faster Than Traditional Caching

Magento’s default setup uses files for cache, which isn’t ideal:

  • The system checks every file during cache cleanup.

  • Big stores with many products create thousands of cache files.

  • Redis avoids file scanning with a database, speeding things up.

4. Handles Heavy Traffic

Redis handles high traffic loads in Magento 2

Redis handles high-traffic Magento stores:

  • Faster page loads.

  • Administrative tasks.

  • Suits multi-server setups.

5. Uses Databases for Different Needs

Redis supports many databases in one instance:

  • Uses 0 by default for the general cache.

  • Use database 0 for the configuration cache.

  • Use database 1 for full-page cache.

  • Assign 2 for session data.

6. Easy to Connect and Scale

Redis connects in how you connect it:

  • Works via 127.0.0.1 or a Unix socket.

  • The default port is 6379.

  • Set in the env.php file or main configuration.

  • Some setups use ElastiCache for Redis scaling.

7. Tag-Based Cache

Redis deletes caches using tags, clearing only linked content. This avoids slow file searches, keeping the admin panel cache clean and fast.

8. Customizable Settings

Redis settings are manageable:

  • Use the backend_options array in the config.

  • Set values like compress_data or page_cache.

  • The Redis setup file stores these tweaks.

9. Trusted by the Magento Community

Redis is trusted by the Magento community for cache and sessions

Redis is popular on Magento Stack Exchange:

  • Pairs with varnish and Redis.

  • Works via magento\framework\cache\backend\redis.

  • Developers rely on it for Magento and eCommerce stores.

5 Steps to Configure Magento 2 to Use Redis as Cache Storage

Step 1: Configure Default Cache

To set Redis as the default cache:

  • Navigate to your Magento 2 folder:

cd /path/to/magento2

  • Then run this command:

`bin/magento setup:config:set \

--cache-backend=redis \

--cache-backend-redis-server=127.0.0.1 \

--cache-backend-redis-port=6379 \

--cache-backend-redis-db=0`

  1. 127.0.0.1: This is the default IP address for the Redis server. Replace it with the correct IP if it's on a remote server.

  2. 6379: The default port for Redis.

  3. 0: The database number for the default cache.

Step 2: Configure Page Cache

Let's set Redis to handle the page cache:

  • Run this command:

`bin/magento setup:config:set \

--page-cache=redis \

--page-cache-redis-server=127.0.0.1 \

--page-cache-redis-port=6379 \

--page-cache-redis-db=1`

  1. 127.0.0.1: Redis server’s IP address.

  2. 6379: Redis port.

  3. 1: The database number for the page cache.

Step 3: Configure Session Storage

To use Redis for storing sessions:

  • Run this command:

`bin/magento setup:config:set \

--session-save=redis \

--session-save-redis-host=127.0.0.1 \

--session-save-redis-port=6379 \

--session-save-redis-db=2`

Adjust the settings for your Redis setup as needed.

Step 4: Flush Magento Cache

Once you have configured Redis, you need to flush Magento’s cache:

  • Clear the cache directories:

rm -rf var/cache var/page_cache var/session

  • Flush all Redis databases:

redis-cli flushall

  • If you want to flush a specific database, use:

redis-cli -n <db_number> flushdb

Swap <db_number> with the correct database number.

Step 5: Verify Configuration

To ensure Redis is working:

  • Run redis-cli ping. It will reply with "PONG."

  • To track Redis commands, use redis-cli monitor.

  • You can also check the Magento cache folders:

    1. var/cache

    2. var/page_cache

    3. var/session.

5 Steps to Configure Redis for Session Storage in Magento 2

Step 1: Set Redis as Session Storage

Magento’s command-line tool handles setup. From the Magento root directory, run:

`bin/magento setup:config:set \

--session-save=redis \

--session-save-redis-host=127.0.0.1 \

--session-save-redis-port=6379 \

--session-save-redis-db=2 \

--session-save-redis-log-level=4`

Here’s what each line does:

  • --session-save=redis: Sets Redis for sessions.

  • --session-save-redis-host: Enter the IP address.

  • --session-save-redis-port: Set the port number.

  • --session-save-redis-db: Sets database for sessions (suggested choice is 2).

  • --session-save-redis-log-level: Sets logging level (4 for warnings only).

Step 2: (Optional) Use a Second Redis Instance for Sessions

  • You can separate sessions from other Redis data. One way is to use a different Redis database. You can opt for a separate instance.

  • Install Redis by running sudo apt install redis-server.

  • Then configure a separate database for sessions:

`bin/magento setup:config:set \

--session-save=redis \

--session-save-redis-host=127.0.0.1 \

--session-save-redis-port=6379 \

--session-save-redis-db=2`

Note: Many Redis instances rely on your server setup. Check with your hosting provider for compatibility.

Step 3: Make Sure It’s Working

  • Test Redis with:

    redis-cli ping

  • A PONG reply means Redis is running. To track activity:

    redis-cli monitor

Browse your store and check for Redis logging session commands.

Step 4: Clear Magento’s Cache

  • After setup, clear the cache:

    php bin/magento cache:clean

  • Flush the Redis session database:

    redis-cli flushdb -n 2

  • Ensure the database number matches your setup.

Step 5: Test That Sessions Work

Check if sessions work:

  • Log in to your site.

  • Add an item to your cart.

  • Refresh the page to confirm persistence.

Browsing your site usually suffices to verify.

Magento 2 Redis Issues: Common Problems and Reliable Fixes

Issue Symptoms Cause Fix
Redis Connection Failed The site feels slow or shows error messages. Magento logs may show connection errors. Redis or Magento settings might be off. You can use redis-cli ping. It make sure the host settings are correct.
Wrong Redis Port or Host Magento cannot connect. Error logs mention port or host problems. Magento is trying to connect to the wrong Redis port or IP address. Check Redis with netstat -tulpn. Update Magento’s Redis settings to match.
Wrong Redis Database Number Sessions or cache data disappear or overlap. Some pages may break. One Redis database is being used for more than one service. Assign separate databases for cache, full page cache, and sessions (e.g., 0, 1, 2).
PHP Redis Module Missing Redis features stop working. Errors mention a missing extension. The PHP Redis extension is not installed on the server. Run php -m.
Redis Overload or Memory Limits The site becomes slow or stops responding. Redis logs may show memory issues. Redis is running out of memory, or no memory limit is set. Run redis-cli info memory to check memory. Set a memory limit and an eviction policy.
Magento Cache Not Clearing Admin changes do not show up on the site. Pages show old content. Magento cache is stuck, or Redis is not clearing old data. Run php bin/magento cache:clean. Use redis-cli flushdb to clear the right Redis DB.
Session Timeouts The system logs users out too soon. Cart items may vanish after short periods. Session lifetime is too short, or Redis clears sessions early. Increase session lifetime in Magento. Check Redis settings to keep sessions longer.

FAQs

1. Why use Redis with Magento 2?

Redis stores the cache in memory and works faster. Magento loads pages faster and handles more users at once. This improves both speed and performance. It also reduces the pressure on your server’s hard drive.

2. Is Redis faster than file-based caching?

Redis is faster because it keeps data in memory. It keeps data in memory, so it responds. This helps Magento perform well during busy times. File cache can slow down your site when you are under high load.

3. How can I check if Redis is working with Magento?

Use the redis-cli track command in your server terminal. Refresh your website and watch for Redis activity in the output. If you see commands, Redis is active. This is a quick way to confirm it’s working.

4. What is the purpose of using different Redis databases?

Each cache type can have its own Redis database number. This keeps the session, page, and default cache separate. It helps avoid conflicts between cache types. It also makes it easier to clear one without touching the others.

5. Can I host Redis on a different server?

Redis can run on a separate server from Magento. In your Magento settings, use the Redis server’s IP address. Make sure the Redis port is open and accessible. It works well for cloud-based websites.

Summary

Configure Magento 2 to use Redis by installing it on your server. Then, update the Magento configuration to use it for cache and session storage. Consider the following advantages of using Redis:

  • In-Memory Data Storage: Redis stores data in memory faster than disk-based storage.

  • Optimized Data Structures: Redis’s design supports data structures for fast operations.

  • Asynchronous I/O Operations: Redis starts new tasks without waiting.

Ready to improve your Magento 2 performance with Redis? Explore managed Magento hosting to configure Redis for faster speed.

[Updated on April 8, 2025]

Nikita Parmar
Nikita Parmar
Technical Writer

Nikita is a skilled content writer who simplifies complex ideas for the Magento audience. She excels at creating SEO-friendly articles and informative blog posts about Magento. She consistently delivers clear, engaging, and audience-focused content.


Get the fastest Magento Hosting! Get Started