How to integrate Redis into Magento v2 Print

  • Magento, Cache, redis
  • 0

In Magento 2, Redis can be used as page cache and session storage cache, providing a faster alternative to the default File storage cache. Redis indexes tags in files, eliminating the need for full scans of every cache file and storing metadata and cache records in a single file, which reduces the number of inodes and file operations required.

Important: Always backup your configuration files before editing them. This tutorial involves changes to Magento's env.php file. Create a backup on your local computer and also on the server (e.g., env.php-backup).

Using Redis for Session Storage

What is a session?

A session is user-specific data stored on the server for each client. The server maps clients to their sessions using a cookie or, in some cases, a SID in the URL. When you return to a Magento store after browsing, and the items you added to your cart are still there, this is managed by sessions.

Configuration

To use Redis for session storage, add the following to your configuration file: app/etc/env.php:


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


Ensure you are using the correct host, port, and database. The host is typically 127.0.0.1 for a local Redis instance, the default port is 6379, and the database number should be unique to avoid data loss.

Note: If you use Redis for caching in other applications or host multiple websites in the same account, ensure that the database numbers are different. You can have up to 16 Redis databases per hosting account.

 

Using Redis for Default and Page Caching

If you are not using a specialized Magento Hosting Plan and do not have access to LiteMage, you can use Redis for default and page caching.

Configuration

To use Redis for default and page caching, add the following to your configuration file: app/etc/env.php:


'cache' => [
'frontend' => [
'default' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '3'
]
],
'page_cache' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '4',
'compress_data' => '0'
]
]
]
],


Ensure you are using the correct host, port, and database. The host is typically 127.0.0.1 for a local Redis instance, the default port is 6379, and the database number should be unique to avoid data loss.

Note: If you use Redis for caching in other applications or host multiple websites in the same account, ensure that the database numbers are different. You can have up to 16 Redis databases per hosting account.

By following these steps, you can configure Redis for both session storage and caching in Magento 2 on a standard cPanel server. This setup will enhance your store's performance and efficiency.


Was this answer helpful?

« Back