How to configure Magento 2 to use Redis
Redis - High performance Database
Redis is an open source high performance database, in-memory data structure store. Redis is a key-value store meaning is primarily design to store data using a unique Key.
Redis is tremendously popular among developers all over the globe, and chances are you use many applications that utilize Redis, under the covers, in one way or another.
Why is redis so fast?
Redis uses the memory to store data. The entire data set, like memcached, is stored in-memory so it is extremely fast. Redis is a fantastic choice if you want a highly scalable data store shared by multiple processes or multiple servers. The biggest advantages over memcached is that is cache tag support included and can store data persistent.
Advantages of using Redis with Magento 2
- Extreme fast in-memory storage
- Vertical and Horizontal Scaleable
- Shareable among Multi Servers
- Cache Tag Support
Magento 2 and Redis
Out of the box Magento 2 is shipped with redis support and we can use it for cache, sessions and full page cache. Magento 2 cache entries are organized in groups like Configuration, Layous, Blocks HTML Output and so on. If you have shop(s) with a huge amount of products your cache will be very big. Because of that it's highly recommended to use redis as cache backend. Out of the box Magento 2 uses the file system as cache storage which isn't really recommended because it's the slowest cache storage you can use. For each cache group deletion Magento needs to open all files to check if it's in the cache group or not. If you do this with thousand of files, which you will have with a big shop, it will extremely slow down your shop.
How to configure Magento 2 to use Redis as cache storage?
To configure Redis for Magento 2 cache, just add the following example to your app/etc/env.php:
'cache' => array ( 'frontend' => array ( 'default' => array ( 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => array ( 'server' => '127.0.0.1', 'port' => '6379', 'persistent' => 0, 'database' => '0', 'force_standalone' => '0', 'connect_retries' => '10', 'read_timeout' => '30', 'automatic_cleaning_factor' => '0', 'compress_data' => '1', 'compress_tags' => '1', 'compress_threshold' => '20480', 'compression_lib' => 'gzip', ), ), ), ),
If redis is working you should not see any files and directories in the folder var/cache/ anymore.
Further you can connect with "redis-cli info" to check if there entries in the databases.
How to configure Magento 2 to use Redis as session storage?
To configure Magento 2 to use Redis for sessions, just add the following lines to your app/etc/env.php:
'session' => array ( 'save' => 'redis', 'redis' => array ( 'host' => '127.0.0.1', 'port' => '6379', 'password' => '', 'timeout' => '5', 'persistent_identifier' => '', 'database' => '1', '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' ) )
Make sure to use any database number greater than 0 because database 0 is used for cache