Valkey is the Linux Foundation fork of Redis 7.2, created after Redis changed its license in 2024. It is API-compatible with Redis, actively maintained, and free under BSD. By the end of this guide you will have Valkey 7.2 running on an Ubuntu 24.04 VPS with AOF persistence enabled, a memory cap that prevents OOM kills, password authentication, and a hardened systemd unit. The whole stack costs nothing beyond your VPS bill.
Prerequisites
- Ubuntu 24.04 LTS VPS (1 vCPU, 2 GB RAM minimum — a Hetzner CX22 at $4.49/month works)
- A non-root user with
sudoprivileges - SSH access to the server
- Basic familiarity with
systemctland editing files withnanoorvim
Step 1 — Add the Valkey APT Repository
Valkey is not in Ubuntu's default repos yet. Add the official repository maintained by the Valkey project.
- Install the prerequisites for adding a signed repo.
sudo apt update && sudo apt install -y curl gpg
- Download and store the signing key.
curl -fsSL https://packages.valkey.io/valkey-signing.gpg \
| sudo gpg --dearmor -o /usr/share/keyrings/valkey-archive-keyring.gpg
- Add the repository source file.
echo "deb [signed-by=/usr/share/keyrings/valkey-archive-keyring.gpg] \
https://packages.valkey.io/apt stable main" \
| sudo tee /etc/apt/sources.list.d/valkey.list
- Refresh the package index and install Valkey 7.2.
sudo apt update && sudo apt install -y valkey=7.2.*
Expected output (last two lines):
Setting up valkey (7.2.x-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/valkey-server.service
The installer creates the valkey system user and drops a default config at /etc/valkey/valkey.conf.
Step 2 — Configure Memory Limits and Authentication
Edit the main config file. Every option below targets the exact line already present in the default file — change the value rather than appending a duplicate.
sudo nano /etc/valkey/valkey.conf
Apply these changes:
Bind only to localhost (unless your app is on another host):
bind 127.0.0.1 -::1
Set a strong password — replace CHANGE_ME_NOW with a random 32-character string:
requirepass CHANGE_ME_NOW
Generate one with:
openssl rand -base64 24
Cap memory to 256 MB (adjust for your VPS size; leave 25% of RAM free for the OS):
maxmemory 256mb
Set the eviction policy — allkeys-lru is the safest default for a cache; use noeviction if Valkey holds data you cannot afford to lose:
maxmemory-policy allkeys-lru
Set the working directory where dump files land:
dir /var/lib/valkey
Save and close the file (Ctrl+O, Enter, Ctrl+X in nano).
Step 3 — Enable AOF Persistence
Append-Only File (AOF) persistence writes every write command to disk. It costs a few IOPS but means you lose at most one second of data on a crash, not everything since the last RDB snapshot.
In /etc/valkey/valkey.conf, set:
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
aof-use-rdb-preamble yes
appendfsync everysec flushes the AOF buffer to disk once per second — a good balance between durability and write throughput. aof-use-rdb-preamble yes stores an RDB snapshot at the start of the AOF file, which makes rewrites faster.
You can keep RDB snapshots as a second safety net. Verify the default save lines are present and not commented out:
save 3600 1
save 300 100
save 60 10000
These tell Valkey to write an RDB dump if at least 1 key changed in 3600 seconds, 100 keys in 300 seconds, or 10,000 keys in 60 seconds.
Step 4 — Harden the systemd Unit
The package ships a systemd unit at /lib/systemd/system/valkey-server.service. Override it with a drop-in so your changes survive package upgrades.
sudo mkdir -p /etc/systemd/system/valkey-server.service.d
sudo nano /etc/systemd/system/valkey-server.service.d/hardening.conf
Paste the following:
[Service]
# Restart policy
Restart=always
RestartSec=3
# Resource limits
LimitNOFILE=65535
LimitNPROC=512
# Filesystem isolation
ReadWritePaths=/var/lib/valkey /var/log/valkey /run/valkey
PrivateTmp=true
ProtectSystem=full
NoNewPrivileges=true
# Disable core dumps
LimitCORE=0
Save the file, then reload the daemon and restart Valkey.
sudo systemctl daemon-reload
sudo systemctl restart valkey-server
Restart=always ensures Valkey comes back automatically after a crash. ProtectSystem=full mounts /usr, /boot, and /etc read-only for the service process.
Step 5 — Tune the Kernel for Valkey
Valkey logs two warnings on a stock Ubuntu kernel that hurt performance: low vm.overcommit_memory and transparent huge pages (THP).
Fix overcommit — Valkey needs this to fork safely during RDB saves:
echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.d/60-valkey.conf
Disable THP — THP causes latency spikes during memory compaction:
echo 'never' | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
To make THP persistent across reboots, add a systemd one-shot service:
sudo nano /etc/systemd/system/disable-thp.service
[Unit]
Description=Disable Transparent Huge Pages
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled"
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now disable-thp
Apply the sysctl change immediately:
sudo sysctl -p /etc/sysctl.d/60-valkey.conf
Verify It Works
- Check the service status.
sudo systemctl status valkey-server
Expected:
● valkey-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/valkey-server.service; enabled)
Active: active (running) since ...
- Authenticate and ping.
valkey-cli -a CHANGE_ME_NOW ping
Expected:
PONG
- Write a key and read it back.
valkey-cli -a CHANGE_ME_NOW set test:hello "serverhabit"
valkey-cli -a CHANGE_ME_NOW get test:hello
Expected:
OK
"serverhabit"
- Confirm AOF is active.
valkey-cli -a CHANGE_ME_NOW info persistence | grep aof_enabled
Expected:
aof_enabled:1
- Check memory usage.
valkey-cli -a CHANGE_ME_NOW info memory | grep -E 'used_memory_human|maxmemory_human'
Expected (values will differ):
used_memory_human:1.01M
maxmemory_human:256.00M
- Verify the AOF file exists on disk.
ls -lh /var/lib/valkey/appendonly.aof
Expected:
-rw-r--r-- 1 valkey valkey 88 Jan 1 00:00 /var/lib/valkey/appendonly.aof
Troubleshooting
valkey-server.service fails to start — "Can't open the log file"
Run sudo chown -R valkey:valkey /var/log/valkey. The log directory must be owned by the valkey user.
NOAUTH Authentication required when running valkey-cli
You set requirepass but forgot -a YOUR_PASSWORD. Always pass -a or run AUTH YOUR_PASSWORD inside the CLI session.
WARNING: 32 bit instance detected You installed a 32-bit OS or package. Valkey on 32-bit caps each key at 2 GB but also limits total memory. Use a 64-bit image.
MISCONF Redis is configured to save RDB snapshots but it is currently not able to persist on disk
Disk is full or /var/lib/valkey has wrong permissions. Check df -h and sudo chown -R valkey:valkey /var/lib/valkey.
High latency spikes every few seconds
THP is still enabled. Confirm with cat /sys/kernel/mm/transparent_hugepage/enabled — the active value is wrapped in brackets. If it shows [always], the disable-thp service did not run. Execute sudo systemctl start disable-thp and verify again.
maxmemory eviction causing unexpected key loss
If Valkey holds session data or job queues you cannot lose, change maxmemory-policy from allkeys-lru to volatile-lru (evicts only keys with a TTL) or noeviction (returns errors instead of evicting). Restart the service after editing the config.
AOF file grows too large Valkey rewrites the AOF automatically when it doubles in size. Trigger a manual rewrite with:
valkey-cli -a CHANGE_ME_NOW bgrewriteaof
Next Steps
You have a production-grade Valkey instance running on commodity hardware for pocket change. From here:
- Connect your app — the connection string is
redis://:CHANGE_ME_NOW@127.0.0.1:6379. Most Redis clients (ioredis, redis-py, Jedis) work without modification. - Set up UFW — if you ever bind Valkey to a non-loopback interface, restrict port 6379 with
sudo ufw allow from YOUR_APP_IP to any port 6379. - Monitor with
valkey-cli info— pipe it togrepfor the metrics you care about:connected_clients,evicted_keys,aof_last_write_status. - Schedule AOF backups — a daily
cp /var/lib/valkey/appendonly.aof /backups/valkey/$(date +%F).aofcron job costs nothing and saves you on data loss. - Consider Valkey Cluster — when your dataset outgrows one node, Valkey's built-in cluster mode shards data across multiple VPS instances without extra software.