Best practices for Valkey and Redis service configuration
Use these recommendations to configure Valkey or Redis caching and sessions for Adobe Commerce on cloud infrastructure. For on-premises cache configuration, see Cache backend options and storage reference.
- Configure L2 cache, including Symfony L2 cache
- Enable read-only replica connection
- Preload keys
- Enable stale cache
- Separate cache and session
- Compress the cache
- Review configuration examples
ece-tools package. If not, upgrade to the latest version. You can check the version installed in your local environment using the composer show magento/ece-tools CLI command.Configure L2 cache
Configure the L2 cache by setting the VALKEY_BACKEND or REDIS_BACKEND deployment variable in the .magento.env.yaml configuration file.
For Adobe Commerce 2.4.9 and versions later than 2.4.8-p4, 2.4.7-p9, 2.4.6-p14, and 2.4.5-p16, configure L2 cache with Valkey. The Redis configuration examples on this page apply only to supported Adobe Commerce versions that use Redis. See System Requirements for supported cache services by release.
For implementation details, configuration examples, and deployment-specific guidance, see L2 cache configuration for performance optimization.
For Valkey, use:
| code language-yaml |
|---|
|
For environment configuration details, see VALKEY_BACKEND configuration variables in the Commerce on Cloud Infrastructure Guide.
For Redis, use:
| code language-yaml |
|---|
|
For environment configuration details, see REDIS_BACKEND in the Commerce on Cloud Infrastructure Guide.
Configure Symfony L2 cache
Adobe Commerce 2.4.9 and later support the symfony_l2 cache backend. On Adobe Commerce on cloud infrastructure, configure this backend only after your project uses an ece-tools version that supports symfony_l2 in .magento.env.yaml.
The symfony_l2 backend is the cache implementation that Adobe Commerce uses to manage L1 and L2 cache behavior. It does not replace Redis or Valkey as the remote cache service. For Adobe Commerce 2.4.9, configure symfony_l2 with Valkey as the remote backend.
ece-tools support is available for your project, do not configure symfony_l2 manually in app/etc/env.php as a persistent configuration for Adobe Commerce on cloud infrastructure. Deployment can overwrite manual env.php changes. If ece-tools does not apply symfony_l2, Commerce can fall back to file-based cache. This fallback can increase disk I/O, add file system replication overhead on multi-node environments, and degrade performance.When ece-tools support is available, set the Valkey backend variable to symfony_l2 and define the L2 backend options in CACHE_CONFIGURATION.
stage:
deploy:
VALKEY_BACKEND: symfony_l2
CACHE_CONFIGURATION:
_merge: true
frontend:
default:
backend_options:
remote_backend: valkey
remote_backend_options:
server: localhost
database: 1
port: 6370
serializer: igbinary
compression_lib: gzip
persistent_id: magento_l2_default
local_backend: file
local_backend_options:
cache_dir: /dev/shm/magento_l1
To enable stale cache for selected cache types with symfony_l2, define a second frontend with use_stale_cache: true, then map selected cache types to that frontend. Use distinct local cache directories and persistent IDs for each frontend.
stage:
deploy:
VALKEY_BACKEND: symfony_l2
CACHE_CONFIGURATION:
_merge: true
frontend:
default:
backend_options:
remote_backend: valkey
remote_backend_options:
server: localhost
database: 0
port: 6370
serializer: igbinary
compression_lib: gzip
persistent_id: magento_l2_default
local_backend: file
local_backend_options:
cache_dir: /dev/shm/magento_l1
stale_cache_enabled:
backend: symfony_l2
backend_options:
remote_backend: valkey
remote_backend_options:
server: localhost
database: 0
port: 6370
serializer: igbinary
compression_lib: gzip
persistent_id: magento_l2_stale
local_backend: file
local_backend_options:
cache_dir: /dev/shm/magento_l1_stale
use_stale_cache: true
type:
default:
frontend: default
layout:
frontend: stale_cache_enabled
block_html:
frontend: stale_cache_enabled
reflection:
frontend: stale_cache_enabled
config_integration:
frontend: stale_cache_enabled
config_integration_api:
frontend: stale_cache_enabled
translate:
frontend: stale_cache_enabled
L2 cache memory sizing for Adobe Commerce Cloud
L2 cache uses a temporary file system (/dev/shm) as its storage mechanism. Unlike specialized key-value stores, tmpfs has no key eviction policy, so memory usage can grow unbounded. To prevent exhaustion, Adobe Commerce automatically clears the L2 storage when usage reaches a configurable threshold (95% by default). You can control memory consumption by requesting a larger /dev/shm mount or by lowering the cleanup threshold.
Adjust the maximum L2 cache memory usage based on your project requirements. Use one of the following methods:
- Create a support ticket to adjust the
/dev/shmmount size. For this scenario, Adobe recommends setting the/dev/shmmount size to 15 GB. - Adjust the
cleanup_percentageproperty at the application level to cap storage usage and free memory available for other services.
You can adjust the configuration in the deployment configuration under the cache configuration groupcache/frontend/default/backend_options/cleanup_percentage.
cleanup_percentage configurable option was introduced in Adobe Commerce 2.4.4.The following examples show the configuration code in the .magento.env.yaml file:
| code language-yaml |
|---|
|
| code language-yaml |
|---|
|
Cache requirements vary based on your project configuration and custom third-party code. Size L2 cache memory so that the cache can operate without frequent threshold hits.
Ideally, L2 cache memory usage stabilizes below the threshold to avoid frequent storage clearing.
You can check L2 cache storage memory usage on each node of the cluster by running the following CLI command and reviewing the /dev/shm line.
df -h /dev/shm
Usage can vary across nodes, but it should converge to a similar value.
Enable slave connection
Enable the read-only replica connection in the .magento.env.yaml file to let Adobe Commerce use an additional cache connection for reads while continuing to use the primary endpoint for writes. This configuration can reduce read load on the primary cache service and distribute read traffic more effectively.
For Valkey, use:
| code language-yaml |
|---|
|
For environment variable configuration details, see VALKEY_USE_SLAVE_CONNECTION in the Commerce on Cloud Infrastructure Guide.
For Redis, use:
| code language-yaml |
|---|
|
For environment variable configuration details, see REDIS_USE_SLAVE_CONNECTION in the Commerce on Cloud Infrastructure Guide.
Preload keys
Magento usually loads cache entries from Redis or Valkey one key at a time. The preload feature lets you provide a list of frequently used keys that Magento fetches in a single pipeline on first access during a request. Magento then keeps the fetched values in PHP memory for the rest of that request, which reduces repeated round trips to Redis or Valkey and can improve request bootstrap performance for those keys.
You can identify frequently used keys by monitoring active commands on Redis or Valkey:
The preload keys are configured in the .magento.env.yaml configuration file.
| code language-yaml |
|---|
|
To list the keys, run the following command:
| code language-terminal |
|---|
|
After 10 seconds, press Ctrl+C. Then run the following command:
| code language-terminal |
|---|
|
This log lists the keys you can preload. To see the content of a key, run the following command:
| code language-terminal |
|---|
|
The preload keys are configured in the .magento.env.yaml configuration file.
| code language-yaml |
|---|
|
To list the keys, run the following command:
| code language-terminal |
|---|
|
After 10 seconds, press Ctrl+C. Then run the following command:
| code language-terminal |
|---|
|
This log lists the keys you can preload. To see the content of a key, run the following command:
| code language-terminal |
|---|
|
Enable stale cache
Stale cache is an L2 cache feature of RemoteSynchronizedCache. When enabled, Adobe Commerce can serve an existing local cache value from /dev/shm while another request is already regenerating the same entry, instead of making every concurrent request wait. This reduces cache stampedes and lock contention during regeneration of expensive cache entries.
How it works
With RemoteSynchronizedCache, Magento maintains two copies of each cache entry: a local copy in /dev/shm and a remote copy in Redis or Valkey. When the remote copy is unavailable and a regeneration lock already exists for that key, concurrent requests can receive the previous local value instead of waiting until the fresh value is written.
To enable stale cache, configure it in the .magento.env.yaml file.
For Valkey:
| code language-yaml |
|---|
|
For Redis:
| code language-yaml |
|---|
|
full_page cache type is not relevant to Adobe Commerce on Cloud infrastructure projects because they use Fastly.default cache frontend, which applies stale-cache behavior to all cache entries that use that frontend. Magento core cache types generally work as expected with this setting. However, if your project includes custom code or extensions that write to the cache through the generic \Magento\Framework\App\Cache API (for example $this->cache->save()) without a dedicated cache frontend, those entries can also serve stale values during regeneration.default frontend and enable it only for selected cache types, as is commonly done on-premises.Enabling stale cache per cache type individually
You can enable stale cache only for selected cache types by defining a dedicated cache frontend in .magento.env.yaml and mapping the selected cache types to it.
To work correctly, the custom frontend must be defined as a complete frontend under CACHE_CONFIGURATION.frontend. Defining only use_stale_cache: true for a new frontend name is not enough.
Example configurations
For Valkey:
| code language-yaml |
|---|
|
For Redis:
| code language-yaml |
|---|
|
stale_cache_enabled so that the new frontend maintains the same behavior.Separate cache and session instances
Separating the cache from the sessions allows you to manage them independently. It reduces contention between cache and session traffic, prevents cache-related pressure from affecting sessions, and allows each Redis or Valkey instance to be sized and tuned for its own workload.
Follow the steps below to provision a dedicated instance for sessions:
-
Update the
.magento/services.yamlconfiguration file.code language-yaml mysql: type: mysql:10.4 disk: 35000 valkey: type: valkey:8.0 valkey-session: # This is for the new Valkey instance type: valkey:8.0 search: type: elasticsearch:7.9 disk: 5000 rabbitmq: type: rabbitmq:3.8 disk: 2048 -
Update the
.magento.app.yamlconfiguration file.code language-yaml relationships: database: "mysql:mysql" valkey: "valkey:valkey" valkey-session: "valkey-session:valkey" # Relationship of the new Valkey instance search: "search:elasticsearch" rabbitmq: "rabbitmq:rabbitmq" -
Request a new Valkey instance dedicated to sessions on Production and Staging environments.
Submit an Adobe Commerce Support ticket. Include the updated
.magento/services.yamland.magento.app.yamlconfiguration files.This update does not cause any downtime, but it requires a deployment to activate the new service.
-
Verify that the new instance is running, and note the port number.
code language-shell echo $MAGENTO_CLOUD_RELATIONSHIPS | base64 -d | json_pp -
Add the port number to the
.magento.env.yamlconfiguration file.note important IMPORTANT Configure the Valkey session port only if ece-toolsis unable to automatically detect it from theMAGENTO_CLOUD_RELATIONSHIPSValkey session service definition.note NOTE Set disable_lockingto1for best performance. In rare cases where race conditions occur due to high concurrent session activity, set it to0to enable locking.code language-yaml SESSION_CONFIGURATION: _merge: true redis: # keep 'redis' even if you are using Valkey. timeout: 5 disable_locking: 1 bot_first_lifetime: 60 bot_lifetime: 7200 max_lifetime: 2592000 min_lifetime: 60 -
Remove sessions from the default database (
db 0) on the Valkey cache instance.code language-terminal valkey-cli -h 127.0.0.1 -p 6370 -n 0 FLUSHDB
-
Update the
.magento/services.yamlconfiguration file.code language-yaml mysql: type: mysql:10.4 disk: 35000 redis: type: redis:6.0 redis-session: # This is for the new Redis instance type: redis:6.0 search: type: elasticsearch:7.9 disk: 5000 rabbitmq: type: rabbitmq:3.8 disk: 2048 -
Update the
.magento.app.yamlconfiguration file.code language-yaml relationships: database: "mysql:mysql" redis: "redis:redis" redis-session: "redis-session:redis" # Relationship of the new Redis instance search: "search:elasticsearch" rabbitmq: "rabbitmq:rabbitmq" -
Request a new Redis instance dedicated to sessions on Production and Staging environments.
Submit an Adobe Commerce Support ticket. Include the updated
.magento/services.yamland.magento.app.yamlconfiguration files.This update does not cause any downtime, but it requires a deployment to activate the new service.
-
Verify that the new instance is running, and note the port number.
code language-shell echo $MAGENTO_CLOUD_RELATIONSHIPS | base64 -d | json_pp -
Add the port number to the
.magento.env.yamlconfiguration file.note important IMPORTANT Configure the Redis session port only if ece-toolsis unable to automatically detect it from theMAGENTO_CLOUD_RELATIONSHIPSRedis session service definition.note NOTE Set disable_lockingto1for best performance. In rare cases where race conditions occur due to high concurrent session activity, set it to0to enable locking.code language-yaml SESSION_CONFIGURATION: _merge: true redis: timeout: 5 disable_locking: 1 bot_first_lifetime: 60 bot_lifetime: 7200 max_lifetime: 2592000 min_lifetime: 60 -
Remove sessions from the default database (
db 0) on the Redis cache instance.code language-terminal redis-cli -h 127.0.0.1 -p 6370 -n 0 FLUSHDB
Cache compression
If you use more than 6 GB of Redis or Valkey maxmemory, you can enable cache compression to reduce the space consumed by keys. Be aware that this setting trades client-side performance for memory savings. If you have spare CPU capacity, consider enabling it. See Use Redis for session storage or Use Valkey for session storage in the Configuration Guide.
stage:
deploy:
CACHE_CONFIGURATION:
_merge: true
frontend:
default:
backend_options:
compress_data: 4 # 0-9
compress_tags: 4 # 0-9
compress_threshold: 20480 # don't compress files smaller than this value
compression_lib: 'gzip' # snappy and lzf for performance, gzip for high compression (~69%)
Enable asynchronous freeing
To enable lazyfree on Adobe Commerce on cloud infrastructure, submit an Adobe Commerce Support ticket requesting that the following Redis or Valkey configuration be applied to your environments:
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
replica-lazy-flush yes
lazyfree-lazy-user-del yes
When lazyfree is enabled, Redis or Valkey offloads memory reclamation to background threads for evictions, expirations, server-initiated deletes, user deletes, and replica dataset flushes. This reduces main-thread blocking and can lower request latency.
lazyfree-lazy-user-del yes option makes the DEL command behave like UNLINK, which unlinks keys immediately and frees their memory asynchronously.Enable multithreaded I/O
To enable Redis I/O threading on Adobe Commerce on cloud infrastructure, submit an Adobe Commerce Support ticket requesting the I/O threading configuration below. This configuration can improve throughput by offloading socket reads and writes and command parsing from the main thread, at the cost of higher CPU usage. Validate under load and monitor your hosts.
For Redis:
| code language-text |
|---|
|
For Valkey:
| code language-text |
|---|
|
io-threads or disable reads in I/O threads.Increase client timeouts and retries
Increase the Redis or Valkey cache client’s tolerance to short periods of saturation by adjusting the backend options in .magento.env.yaml.
stage:
deploy:
CACHE_CONFIGURATION:
_merge: true
frontend:
default:
backend_options:
connect_retries: 3 # Number of connection retries
remote_backend_options:
read_timeout: 10 # Timeout
These settings can reduce intermittent connection and read-timeout errors during short spikes by retrying connection setup and allowing more time for replies from Redis or Valkey.
Configuration examples
Use the following examples as a starting point for your Redis or Valkey service configurations.
Apply all best practice recommendations
| code language-yaml |
|---|
|
| code language-yaml |
|---|
|
Apply all best practice recommendations and separate stale cache by cache type
| code language-yaml |
|---|
|
| code language-yaml |
|---|
|
Additional information
See the following related topics: