Magento Redis / Valkey: cache, full page cache and sessions
Out of the box Magento keeps its cache and its sessions in files. In this short lesson they move into Valkey: the default cache and the full page cache with setup:config:set, compressed, with /etc/valkey/valkey.conf ve allkeys-lru; the sessions on their own Valkey with noeviction and an append-only file; and the checks with valkey-cli PING, INFO and MONITOR. Plus a Magento 2.4.9 session trap we measured on the server. Every command can be copied or downloaded.
Valkey or Redis for 2.4.9
Adobe’s system requirements (checked October 2026) list Valkey 9 for Magento Open Source / Adobe Commerce 2.4.9. Valkey is the open-source fork of Redis and speaks the same protocol, so redis-cli and Magento’s Redis code work with it. Our test server runs Valkey 9.0.6, built in Lesson 1 (see our installation guide), on the same machine as Magento.
| Görev | Nerede | Policy | Neden |
|---|---|---|---|
| Varsayılan önbellek | port 6379, database 0 | allkeys-lru, compressed | anything dropped is rebuilt by Magento |
| Tam sayfa önbelleği | port 6379, database 1 | allkeys-lru, compressed | its own database: watch and flush it on its own |
| Oturumlar | own Valkey, port 6380, database 0 | noeviction, appendonly yes | a lost session = logged out, cart gone |
Valkey on the server
Over SSH, as root. PONG means it runs; on our server redis-cli is a link to valkey-cli:
valkey-cli ping redis-cli ping
valkey-cli info server | grep -E '^(valkey_version|tcp_port|config_file):'
The config file without comments: bound to 127.0.0.1, port 6379, maxmemory 1gb ve maxmemory-policy allkeys-lru (when full, the least recently used keys are dropped: right for a cache).
grep -vE '^\s*(#|$)' /etc/valkey/valkey.conf
Persistence: by default Valkey writes RDB snapshots (save 3600 1 300 100 60 10000) and no append-only file. For a cache that is enough: it comes back warm after a restart, and Magento rebuilds whatever is missing.
valkey-cli config get save; valkey-cli config get appendonly
Cache and page cache
As the Magento file owner (in our lessons the user magento), in the Magento folder. This one-liner prints only the cache and session parts of app/etc/env.php; never print the whole file on screen, it holds the crypt key and the database login:
php -r '$e = include "app/etc/env.php"; foreach ($e["cache"]["frontend"] ?? [] as $n => $f) printf("%-10s %s, db %s, compress %s\n", $n, $f["backend"], $f["backend_options"]["database"], $f["backend_options"]["compress_data"]); $s = $e["session"]; printf("%-10s %s%s\n", "session", $s["save"], isset($s["redis"]) ? ", port {$s["redis"]["port"]}, db {$s["redis"]["database"]}" : "");'Default cache in database 0, compressed (compress-data=1: less memory, a little CPU):
bin/magento setup:config:set --cache-backend=valkey \ --cache-backend-valkey-server=127.0.0.1 --cache-backend-valkey-port=6379 \ --cache-backend-valkey-db=0 --cache-backend-valkey-compress-data=1
Full page cache in database 1:
bin/magento setup:config:set --page-cache=valkey \ --page-cache-valkey-server=127.0.0.1 --page-cache-valkey-port=6379 \ --page-cache-valkey-db=1 --page-cache-valkey-compress-data=1
Then bin/magento cache:flush. Our shop uses Varnish as its full page cache (Lesson 2), so database 1 stays empty there; with Stores › Configuration › Advanced › System › Full Page Cache › Caching Application = Built-in Cache, Magento keeps the pages in database 1.
A Valkey for sessions
One Valkey has one eviction policy. With allkeys-lru it may drop sessions when memory is full: customers are logged out and carts are lost. So sessions get their own instance with noeviction (when full it refuses new writes instead of deleting sessions, so watch its memory) and an append-only file, so a restart keeps everyone logged in.
# Valkey for Magento sessions: its own instance, so cache keys can never push sessions out bind 127.0.0.1 port 6380 dir /var/lib/valkey-sessions supervised systemd maxmemory 256mb maxmemory-policy noeviction appendonly yes appendfsync everysec
Save as /etc/valkey/valkey-sessions.conf, or create it with the command below.
cat > /etc/valkey/valkey-sessions.conf <<'EOF' # Valkey for Magento sessions: its own instance, so cache keys can never push sessions out bind 127.0.0.1 port 6380 dir /var/lib/valkey-sessions supervised systemd maxmemory 256mb maxmemory-policy noeviction appendonly yes appendfsync everysec EOF
A data folder and a systemd service, copied from the Valkey service of Lesson 1:
install -d -o valkey -g valkey -m 750 /var/lib/valkey-sessions sed 's|valkey.conf|valkey-sessions.conf|; s|^Description=Valkey|Description=Valkey sessions|' /etc/systemd/system/valkey.service > /etc/systemd/system/valkey-sessions.service grep -E '^(Description|ExecStart)=' /etc/systemd/system/valkey-sessions.service
[Unit] Description=Valkey sessions After=network.target [Service] Type=notify User=valkey ExecStart=/usr/local/bin/valkey-server /etc/valkey/valkey-sessions.conf Restart=always [Install] WantedBy=multi-user.target
The result: /etc/systemd/system/valkey-sessions.service.
systemctl daemon-reload && systemctl enable --now valkey-sessions && valkey-cli -p 6380 ping
Sessions in Valkey (2.4.9 trap)
Magento 2.4.9: setup:config:set accepts --session-save=valkey, but the session save handler factory only has db ve redis handlers (app/etc/di.xml), so with valkey PHP’s own handler is used and the sessions quietly stay in files (/var/lib/php/sessions). The --session-save-valkey-* options are not saved either. We measured both on our 2.4.9 test server: session cookie set, zero keys in Valkey, new session files. Valkey speaks the Redis protocol, so use the redis session handler, pointed at Valkey:
bin/magento setup:config:set --session-save=redis \ --session-save-redis-host=127.0.0.1 --session-save-redis-port=6380 \ --session-save-redis-db=0
It asks Overwrite the existing configuration for session-save?: answer Y (or add -n in a script). Check:
php -r '$e = include "app/etc/env.php"; foreach ($e["cache"]["frontend"] ?? [] as $n => $f) printf("%-10s %s, db %s, compress %s\n", $n, $f["backend"], $f["backend_options"]["database"], $f["backend_options"]["compress_data"]); $s = $e["session"]; printf("%-10s %s%s\n", "session", $s["save"], isset($s["redis"]) ? ", port {$s["redis"]["port"]}, db {$s["redis"]["database"]}" : "");'Check it works
Our server runs OPcache with opcache.validate_timestamps = 0 (Lesson 1, for speed): PHP does not notice the changed env.php until PHP-FPM reloads. We measured it: without the reload new sessions still went to the old database.
systemctl reload php8.5-fpm
The shop still answers:
curl -s https://www.example.com/ | grep -o '<title>[^<]*</title>'
MONITOR prints every command Valkey receives. Watch the session Valkey for three seconds while the customer login page loads (a page that needs a session). MONITOR slows Valkey down: on a live shop, only for a few seconds.
( (sleep 1; curl -s -o /dev/null https://www.example.com/customer/account/login/) & timeout 3 valkey-cli -p 6380 monitor | cut -c1-120 )
Keys per database and memory, on both instances:
valkey-cli info keyspace; valkey-cli -p 6380 info keyspace
for p in 6379 6380; do echo "port $p"; valkey-cli -p $p info memory | grep -E '^(used_memory_human|maxmemory_human|maxmemory_policy):'; done
Our result: cache database 0 with 1142 keys, page cache database 1 with 0 (Varnish is the full page cache), sessions database 0 with 3; memory 7.35M of 1.00G (allkeys-lru) and 1.08M of 256.00M (noeviction).
env.php after the lesson (cache and session parts only):
// app/etc/env.php after Lesson 14 (MageBooster test server, Magento 2.4.9): only the cache and session parts.
// Written by bin/magento setup:config:set; the defaults (id_prefix, serializer, lifetimes, ...) are Magento's own.
'cache' => [
'allow_parallel_generation' => false,
'frontend' => [
'default' => [
'id_prefix' => 'd19_',
'backend' => 'valkey',
'backend_options' => [
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379',
'password' => '',
'compress_data' => '1',
'compression_lib' => '',
'serializer' => 'igbinary',
'use_lua' => '0',
'use_lua_on_gc' => '1'
]
],
'page_cache' => [
'id_prefix' => 'd19_',
'backend_options' => [
'serializer' => 'igbinary',
'server' => '127.0.0.1',
'database' => '1',
'port' => '6379',
'password' => '',
'compress_data' => '1',
'compression_lib' => ''
],
'backend' => 'valkey'
]
]
],
'session' => [
'save' => 'redis',
'redis' => [
'host' => '127.0.0.1',
'port' => '6380',
'password' => '',
'timeout' => '2.5',
'retries' => '0',
'persistent_identifier' => '',
'database' => '0',
'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',
'sentinel_master' => '',
'sentinel_servers' => '',
'sentinel_connect_retries' => '5',
'sentinel_verify_master' => '0'
]
],
İndirmeler
#!/bin/bash
# MageBooster Lesson 14: Redis / Valkey for Magento 2.4.9. Run the parts one by one on the server (as root, unless noted).
# 1) Valkey running? Version, port, config, persistence
valkey-cli ping
redis-cli ping
valkey-cli info server | grep -E '^(valkey_version|tcp_port|config_file):'
grep -vE '^\s*(#|$)' /etc/valkey/valkey.conf
valkey-cli config get save; valkey-cli config get appendonly
# 2) Cache + full page cache in Valkey (as the Magento file owner, in the Magento folder)
cd /var/www/magento
sudo -u magento php -r '$e = include "app/etc/env.php"; foreach ($e["cache"]["frontend"] ?? [] as $n => $f) printf("%-10s %s, db %s, compress %s\n", $n, $f["backend"], $f["backend_options"]["database"], $f["backend_options"]["compress_data"]); $s = $e["session"]; printf("%-10s %s%s\n", "session", $s["save"], isset($s["redis"]) ? ", port {$s["redis"]["port"]}, db {$s["redis"]["database"]}" : "");'
sudo -u magento bin/magento setup:config:set --cache-backend=valkey \
--cache-backend-valkey-server=127.0.0.1 --cache-backend-valkey-port=6379 \
--cache-backend-valkey-db=0 --cache-backend-valkey-compress-data=1
sudo -u magento bin/magento setup:config:set --page-cache=valkey \
--page-cache-valkey-server=127.0.0.1 --page-cache-valkey-port=6379 \
--page-cache-valkey-db=1 --page-cache-valkey-compress-data=1
sudo -u magento bin/magento cache:flush
# 3) A separate Valkey for sessions: port 6380, noeviction, append-only file
cat > /etc/valkey/valkey-sessions.conf <<'EOF'
# Valkey for Magento sessions: its own instance, so cache keys can never push sessions out
bind 127.0.0.1
port 6380
dir /var/lib/valkey-sessions
supervised systemd
maxmemory 256mb
maxmemory-policy noeviction
appendonly yes
appendfsync everysec
EOF
install -d -o valkey -g valkey -m 750 /var/lib/valkey-sessions
sed 's|valkey.conf|valkey-sessions.conf|; s|^Description=Valkey|Description=Valkey sessions|' /etc/systemd/system/valkey.service > /etc/systemd/system/valkey-sessions.service
grep -E '^(Description|ExecStart)=' /etc/systemd/system/valkey-sessions.service
systemctl daemon-reload && systemctl enable --now valkey-sessions && valkey-cli -p 6380 ping
# 4) Sessions in Valkey. Magento 2.4.9: --session-save=valkey leaves the sessions in files -> use the redis handler, pointed at Valkey
sudo -u magento bin/magento setup:config:set --session-save=redis \
--session-save-redis-host=127.0.0.1 --session-save-redis-port=6380 \
--session-save-redis-db=0
sudo -u magento php -r '$e = include "app/etc/env.php"; foreach ($e["cache"]["frontend"] ?? [] as $n => $f) printf("%-10s %s, db %s, compress %s\n", $n, $f["backend"], $f["backend_options"]["database"], $f["backend_options"]["compress_data"]); $s = $e["session"]; printf("%-10s %s%s\n", "session", $s["save"], isset($s["redis"]) ? ", port {$s["redis"]["port"]}, db {$s["redis"]["database"]}" : "");'
# OPcache with validate_timestamps=0 keeps the old env.php until PHP-FPM reloads
systemctl reload php8.5-fpm
# 5) Checks
curl -s https://www.example.com/ | grep -o '<title>[^<]*</title>'
( (sleep 1; curl -s -o /dev/null https://www.example.com/customer/account/login/) & timeout 3 valkey-cli -p 6380 monitor | cut -c1-120 )
valkey-cli info keyspace; valkey-cli -p 6380 info keyspace
for p in 6379 6380; do echo "port $p"; valkey-cli -p $p info memory | grep -E '^(used_memory_human|maxmemory_human|maxmemory_policy):'; done
Sorun giderme
| Belirti | Neden ve çözüm |
|---|---|
Sessions are not in Valkey (no sess_ keys), files in /var/lib/php/sessions | 2.4.9 with --session-save=valkey: use --session-save=redis with the --session-save-redis-* options. Or PHP-FPM was not reloaded after the change. |
| Settings changed in env.php, nothing happens | OPcache without timestamp checks: systemctl reload php8.5-fpm (your PHP version). |
OOM command not allowed when used memory > maxmemory | The session Valkey (noeviction) is full: raise /etc/valkey/valkey.conf or lower the session lifetime. Never switch sessions to an LRU policy. |
| Customers are logged out at random | Sessions share a Valkey with the cache under allkeys-lru: move them to their own instance, as in this lesson. |
Could not connect / Connection refused | systemctl status valkey valkey-sessions, the port in env.php, and bind in the config file. With requirepass, add the password options. |
| Valkey reachable from outside | Dikkat: bind 127.0.0.1 on a single server. Valkey on its own server: a private network, a firewall and a password (requirepass or ACL users). |
Sonraki ders
Lesson 15: Shipping methods.
▶ Tüm MageBooster derslerini YouTube’da izleyin: sırayla tüm dersler · kanala abone olun
