Use Redis for default cache
Commerce provides command-line options to configure the Redis page and default caching. Although you can configure caching by editing the <Commerce-install-dir>app/etc/env.php file, using the command line is the recommended method, especially for initial configurations. The command line provides validation, ensuring the configuration is syntactically correct.
You must install Redis before continuing.
Configure Redis default caching
Run the setup:config:set command and specify parameters that specific to Redis default caching.
bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-<parameter>=<value>...
With the following parameters:
-
--cache-backend=redisenables the Redis default caching. If this feature has already been enabled, omit this parameter. -
--cache-backend-redis-<parameter>=<value>is a list of key-and-value pairs that configure the default caching:
cache-backend-redis-server127.0.0.1cache-backend-redis-port6379cache-backend-redis-dbImportant: If you use Redis for more than one type of caching, the database numbers must be different. It is recommended that you assign the default caching database number to 0, the page-caching database number to 1, and the session storage database number to 2.
0cache-backend-redis-passwordauth command, which requires clients to authenticate to access the database. The password is configured directly in Redis’ configuration file: /etc/redis/redis.conf--cache-backend-redis-use-luaLUA: Lua enables us to run part of the application logic inside Redis, improving performance and ensuring data consistency through its atomic execution.
0--cache-backend-redis-use-lua-on-gcLUA: Lua enables us to run part of the application logic inside Redis, improving performance and ensuring data consistency through its atomic execution.
1Example command
The following example enables Redis default caching, sets the host to 127.0.0.1, and assigns the database number to 0. Redis uses default values for all other parameters.
bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db=0
Configure Redis page caching
To configure Redis page caching on Commerce, run the setup:config:set command with additional parameters.
bin/magento setup:config:set --page-cache=redis --page-cache-redis-<parameter>=<value>...
With the following parameters:
-
--page-cache=redisenables Redis page caching. If this feature has already been enabled, omit this parameter. -
--page-cache-redis-<parameter>=<value>is a list of key-and-value pairs that configure page caching:
page-cache-redis-server127.0.0.1page-cache-redis-port6379page-cache-redis-dbImportant: If you use Redis for more than one type of caching, the database numbers must be different. It is recommended that you assign the default caching database number to 0, the page-caching database number to 1, and the session storage database number to 2.
0page-cache-redis-passwordauth command, which requires clients to authenticate to access the database. Configure the password within the Redis configuration file: /etc/redis/redis.confExample command
The following example enables Redis page caching, sets the host to 127.0.0.1, and assigns the database number to 1. All other parameters are set to the default value.
bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1
Review Commerce environment configuration
Running the commands to configure Redis caching, updates the Commerce environment configuration (<Commerce-install-dir>app/etc/env.php):
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379'
],
],
'page_cache' => [
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0'
]
]
]
],
Configure additional caching options
This section describes how to enable optional configuration settings that are disabled by default.
Redis preload feature
Since Commerce stores configuration data in the Redis cache, we can preload data that is reused between pages. To find keys that must be preloaded, analyze data that is transferred from Redis to Commerce. We suggest preloading data that is loaded on every page, such as SYSTEM_DEFAULT, EAV_ENTITY_TYPES, DB_IS_UP_TO_DATE.
Redis uses the pipeline in order to composite load requests. Keys should include the database prefix; for example, if database prefix is 061_, preload key looks like: 061_SYSTEM_DEFAULT
'cache' => [
'frontend' => [
'default' => [
'id_prefix' => '061_',
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => 'redis',
'database' => '0',
'port' => '6379',
'password' => '',
'compress_data' => '1',
'compression_lib' => '',
'preload_keys' => [
'061_EAV_ENTITY_TYPES',
'061_GLOBAL_PLUGIN_LIST',
'061_DB_IS_UP_TO_DATE',
'061_SYSTEM_DEFAULT',
],
]
],
'page_cache' => [
'id_prefix' => '061_'
]
]
]
In case you are using the preload feature with the L2 cache, do not forget to add the :hash suffix to your keys, since L2 cache only transfers the hash of the data, not the data itself:
'preload_keys' => [
'061_EAV_ENTITY_TYPES:hash',
'061_GLOBAL_PLUGIN_LIST:hash',
'061_DB_IS_UP_TO_DATE:hash',
'061_SYSTEM_DEFAULT:hash',
],
Parallel generation
Eliminate waiting for locks by enabling the allow_parallel_generation option.
This option is disabled by default, and Adobe recommends disabling it until you have a large number of configurations or blocks.
To enable parallel generation:
bin/magento setup:config:set --allow-parallel-generation
Since this option is a flag, you cannot disable it with a command. You must manually set the configuration value to false:
'cache' => [
'frontend' => [
'default' => [
'id_prefix' => 'b0b_',
'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
'backend_options' => [
'server' => 'redis',
'database' => '0',
'port' => '6379',
'password' => '',
'compress_data' => '1',
'compression_lib' => ''
]
],
'page_cache' => [
'id_prefix' => 'b0b_'
]
],
'allow_parallel_generation' => false
],
Verify the Redis connection
To verify that Redis and Commerce are working together, log in to the server running Redis, open a terminal, and use the Redis monitor command or the ping command.
Redis monitor command
redis-cli monitor
Sample page-caching output:
1476826133.810090 [0 127.0.0.1:52366] "select" "1"
1476826133.816293 [0 127.0.0.1:52367] "select" "0"
1476826133.817461 [0 127.0.0.1:52367] "hget" "zc:k:ea6_GLOBAL__DICONFIG" "d"
1476826133.829666 [0 127.0.0.1:52367] "hget" "zc:k:ea6_DICONFIG049005964B465901F774DB9751971818" "d"
1476826133.837854 [0 127.0.0.1:52367] "hget" "zc:k:ea6_INTERCEPTION" "d"
1476826133.868374 [0 127.0.0.1:52368] "select" "1"
1476826133.869011 [0 127.0.0.1:52369] "select" "0"
1476826133.869601 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_DEFAULT__10__235__32__1080MAGENTO2" "d"
1476826133.872317 [0 127.0.0.1:52369] "hget" "zc:k:ea6_INITIAL_CONFIG" "d"
1476826133.879267 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL_PRIMARY_PLUGIN_LIST" "d"
1476826133.883312 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL__EVENT_CONFIG_CACHE" "d"
1476826133.898431 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DB_PDO_MYSQL_DDL_STAGING_UPDATE_1" "d"
1476826133.898794 [0 127.0.0.1:52369] "hget" "zc:k:ea6_RESOLVED_STORES_D1BEFA03C79CA0B84ECC488DEA96BC68" "d"
1476826133.905738 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_STORE_DEFAULT_10__235__32__1080MAGENTO2" "d"
... more ...
1476826210.634998 [0 127.0.0.1:52439] "hmset" "zc:k:ea6_MVIEW_CONFIG" "d" "a:18:{s:19:\"design_config_dummy\";a:4:{s:7:\"view_id\";s:19:\"design_config_dummy\";s:12:\"action_class\";s:39:\"Magento\\Theme\\Model\\Indexer\\Mview\\Dummy\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:0:{}}s:14:\"customer_dummy\";a:4:{s:7:\"view_id\";s:14:\"customer_dummy\";s:12:\"action_class\";s:42:\"Magento\\Customer\\Model\\Indexer\\Mview\\Dummy\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:0:{}}s:13:\"cms_page_grid\";a:4:{s:7:\"view_id\";s:13:\"cms_page_grid\";s:12:\"action_class\";s:43:\"Magento\\Catalog\\Model\\Indexer\\Category\\Flat\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:1:{s:8:\"cms_page\";a:3:{s:4:\"name\";s:8:\"cms_page\";s:6:\"column\";s:7:\"page_id\";s:18:\"subscription_model\";N;}}}s:21:\"catalog_category_flat\";a:4:{s:7:\"view_id\";s:21:\"catalog_category_flat\";s:12:\"action_class\";s:43:\"Magento\\Catalog\\Model\\Indexer\\Category\\Flat\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:6:{s:23:\"catalog_category_entity\";a:3:{s:4:\"name\";s:23:\"catalog_category_entity\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";N;}s:31:\"catalog_category_entity_decimal\";a:3:{s:4:\"name\";s:31:\"catalog_category_entity_decimal\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:27:\"catalog_category_entity_int\";a:3:{s:4:\"name\";s:27:\"catalog_category_entity_int\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:28:\"catalog_category_entity_text\";a:3:{s:4:\"name\";s:28:\"catalog_category_entity_text\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:31:\"catalog_category_entity_varchar\";a:3:{s:4:\"name\";s:31:\"catalog_category_entity_varchar\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:32:\"catalog_category_entity_datetime\";a:3:{s:4:\"name\";s:32:\"catalog_category_entity_datetime\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}}}s:24:\"catalog_category_product\";a:4:{s:7:\"view_id\";s:24:\"catalog_category_product\";s:12:\"action_class\";s:46:\"Magento\\Catalog\\Model\\Indexer\\Category\\Product\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:2:{s:23:\"catalog_category_entity\";a:3:{s:4:\"name\";s:23:\"catalog_category_entity\";s:6:\"column\"
... more ...
Redis ping command
redis-cli ping
The expected response is: PONG
If both commands succeeded, Redis is set up properly.
Inspecting compressed data
To inspect compressed Session data and Page Cache, the RESP.app supports the automatic decompression of Commerce 2 Session and Page cache and displays PHP session data in a human-readable form.