How to integrate Redis into Magento v2
Knowledgebase Article
}
Knowledgebase Article
In Magento 2, Redis can be used as page cache and session storage cache, as an alternative to the default File storage cache.
Using Redis instead of the default File storage cache is better and faster because it works by indexing tags in files so that tag operations do not require a full scan of every cache file.
Also the metadata and the cache record are stored in the same file rather than separate files resulting in fewer inodes and fewer file stat, read, write, lock, and unlink operations.
IMPORTANT: If you are using a Magento Hosting Plan, then please consider Redis as session storage cache only, but use LiteMage as page cache, as it is significantly faster!
More important: Always backup before editing configuration files. This tutorial requires you to change Magento's config file - env.php, so please backup before editing. You can download a copy to your local computer, as well as copy the file on the server but with different name (env.php-backup)
Use Redis for session storage:
But first, what is a session?
Session is user-specific data that is stored on the server for each client.
The server maps clients to their sessions with a cookie (or in dire, cross-domain cases, by a SID in the url). Then when you go back to a Magento store a few days after browsing it, and the items you added to cart are still in the cart – that’s sessions!
How quickly data can be put in and got out of the session storage, how volatile it is, and how it is distributed in a clustered environment are all important considerations when choosing how to store sessions.
To use Redis as session storage cache, you will need to add the following to your configuration file: app/etc/env.php:
'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '/home/USER/.kxcache/redis.sock',
'port' => '0',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '2',
'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'
)
),
where you will need to make sure you are using the correct host, port and database.
The host is the "Redis socket address" which you get from your cPanel >> Redis, the port is always 0, and the database must by an unique Redis database number, which is recommended to protect against data loss.
Important: If you use Redis for caching in other applications (for example, if you host multiple web sites in the same hosting account), the database numbers must be different.
You can have up to 16 Redis databases per hosting account.
Use Redis for default and page caching:
If you are not on our Magento Hosting Plans and you do not have access to LiteMage, then you can use Redis for default and page caching:
You will need to add the following to your configuration file: app/etc/env.php:
'cache' =>
array(
'frontend' =>
array(
'default' =>
array(
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' =>
array(
'server' => '/home/USER/.kxcache/redis.sock',
'database' => '3',
'port' => '0'
),
),
'page_cache' =>
array(
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' =>
array(
'server' => '/home/USER/.kxcache/redis.sock',
'port' => '0',
'database' => '4',
'compress_data' => '0'
)
)
)
),
where you will need to make sure you are using the correct host, port and database.
The host is the "Redis socket address" which you get from your cPanel >> Redis, the port is always 0, and the database must by an unique Redis database number, which is recommended to protect against data loss.
Important: If you use Redis for caching in other applications (for example, if you host multiple web sites in the same hosting account), the database numbers must be different.
You can have up to 16 Redis databases per hosting account.
Powered by WHMCompleteSolution