How to Run Valkey on a $6 VPS With Persistence

by David Park
How to Run Valkey on a $6 VPS With Persistence

What You'll End Up With

You'll have Valkey 7.2 — the Linux Foundation's open-source Redis fork — running on an Ubuntu 24.04 VPS, managed by systemd, with AOF persistence enabled, a 256 MB memory cap, and a password-protected TCP socket. No Docker overhead, no managed Redis bill ($15–$30/month on most clouds). A Hetzner CAX11 ARM instance at $3.90/month handles this comfortably; a CX22 at $5.77/month gives you headroom.

Prerequisites

  • Ubuntu 24.04 LTS VPS (root or sudo user)
  • At least 512 MB RAM, 1 vCPU
  • Ports 6379 closed to the public (firewall in place — this guide uses ufw)
  • Basic comfort with systemctl and editing files with nano or vim

1. Update the System and Install Build Dependencies

Valkey is not yet in Ubuntu's default apt repos, so you'll build from source. First, refresh the package index and pull in the tools you need.

sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential pkg-config libssl-dev tcl

This installs GCC, make, OpenSSL headers (for TLS support), and Tcl (used by Valkey's test suite).


2. Download and Compile Valkey 7.2

Fetch the official release tarball from GitHub, verify the checksum, and compile.

cd /usr/local/src
sudo wget https://github.com/valkey-io/valkey/archive/refs/tags/7.2.5.tar.gz -O valkey-7.2.5.tar.gz
sudo wget https://github.com/valkey-io/valkey/releases/download/7.2.5/valkey-7.2.5.tar.gz.sha256
sudo sha256sum -c valkey-7.2.5.tar.gz.sha256

Expected output:

valkey-7.2.5.tar.gz: OK

If the checksum fails, re-download; do not proceed with a corrupt archive.

sudo tar xzf valkey-7.2.5.tar.gz
cd valkey-7.2.5
sudo make -j$(nproc)
sudo make install

make install copies valkey-server, valkey-cli, and related binaries to /usr/local/bin.


3. Create a Dedicated System User and Directories

Never run Valkey as root. Create a locked-down system account and the directories it needs.

sudo useradd --system --no-create-home --shell /usr/sbin/nologin valkey
sudo mkdir -p /etc/valkey /var/lib/valkey /var/log/valkey
sudo chown valkey:valkey /var/lib/valkey /var/log/valkey
sudo chmod 750 /var/lib/valkey /var/log/valkey

/var/lib/valkey is the data directory where AOF and RDB files will live. /var/log/valkey holds the server log.


4. Write the Valkey Configuration File

Create /etc/valkey/valkey.conf. Every directive below is deliberate — nothing is left at an insecure default.

sudo nano /etc/valkey/valkey.conf

Paste the following block exactly:

# Network
bind 127.0.0.1
port 6379
protected-mode yes

# Authentication — replace with a strong random password
requirepass REPLACE_WITH_STRONG_PASSWORD

# Memory
maxmemory 256mb
maxmemory-policy allkeys-lru

# Persistence — AOF
appendonly yes
appendfilename "valkey.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

# Persistence — RDB snapshot (safety net)
save 3600 1
save 300 100
save 60 10000
dbfilename dump.rdb

# Paths
dir /var/lib/valkey
logfile /var/log/valkey/valkey.log
loglevel notice

# Performance
tcp-backlog 511
tcp-keepalive 300
hz 10
dynamic-hz yes

# Disable dangerous commands
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command DEBUG ""
rename-command CONFIG "VALKEY_CONFIG_7x9q"

Generate the password with:

openssl rand -base64 32

Replace REPLACE_WITH_STRONG_PASSWORD with the output. Store it in your password manager now.

Set strict permissions on the config file so only root and the valkey user can read it:

sudo chown root:valkey /etc/valkey/valkey.conf
sudo chmod 640 /etc/valkey/valkey.conf

5. Create the systemd Unit File

Create /etc/systemd/system/valkey.service:

sudo nano /etc/systemd/system/valkey.service

Paste:

[Unit]
Description=Valkey In-Memory Data Store
After=network.target
Documentation=https://valkey.io/docs/

[Service]
Type=notify
User=valkey
Group=valkey
ExecStart=/usr/local/bin/valkey-server /etc/valkey/valkey.conf --supervised systemd
ExecStop=/usr/local/bin/valkey-cli -a REPLACE_WITH_STRONG_PASSWORD shutdown nosave
Restart=on-failure
RestartSec=5s

# Hardening
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/valkey /var/log/valkey
PrivateTmp=yes
CapabilityBoundingSet=
AmbientCapabilities=

# Resource limit — keeps OOM killer away
LimitNOFILE=65535
LimitCORE=0

[Install]
WantedBy=multi-user.target

Replace REPLACE_WITH_STRONG_PASSWORD in the ExecStop line with the same password you set in valkey.conf.

Reload systemd and enable the service:

sudo systemctl daemon-reload
sudo systemctl enable --now valkey

6. Tune the OS for Valkey

Two kernel settings prevent common Valkey warnings that degrade performance under load.

Disable Transparent Huge Pages:

echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

Make it permanent across reboots:

sudo nano /etc/rc.local

Add before exit 0:

echo never > /sys/kernel/mm/transparent_hugepage/enabled

Raise the vm.overcommit_memory setting:

echo 'vm.overcommit_memory = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Expected output:

vm.overcommit_memory = 1

These two changes eliminate the WARNING overcommit_memory is set to 0 and THP warnings that appear in /var/log/valkey/valkey.log on a stock Ubuntu install.


7. Lock Down the Firewall

Port 6379 must never be reachable from the internet. Confirm ufw is blocking it:

sudo ufw status verbose

If port 6379 is not listed as explicitly allowed, it's already blocked. If you see a rule permitting it, remove it:

sudo ufw delete allow 6379/tcp

If another app on the same server needs to reach Valkey, it connects via 127.0.0.1:6379 — no firewall rule needed.


Verify It Works

Check the service status:

sudo systemctl status valkey

Expected output (truncated):

● valkey.service - Valkey In-Memory Data Store
     Loaded: loaded (/etc/systemd/system/valkey.service; enabled)
     Active: active (running) since ...

Ping the server:

valkey-cli -a REPLACE_WITH_STRONG_PASSWORD ping

Expected output:

PONG

Write and read a key:

valkey-cli -a REPLACE_WITH_STRONG_PASSWORD set testkey "serverhabit"
valkey-cli -a REPLACE_WITH_STRONG_PASSWORD get testkey

Expected output:

OK
"serverhabit"

Confirm AOF file is being written:

ls -lh /var/lib/valkey/

Expected output:

-rw-r--r-- 1 valkey valkey  88 ... valkey.aof
-rw-r--r-- 1 valkey valkey 107 ... dump.rdb

Tail the log for errors:

sudo tail -f /var/log/valkey/valkey.log

You should see Server initialized and Ready to accept connections with no WARNING lines.


Troubleshooting

valkey.service fails to start — "Permission denied" on config file Run sudo chown root:valkey /etc/valkey/valkey.conf && sudo chmod 640 /etc/valkey/valkey.conf, then sudo systemctl restart valkey.

NOAUTH Authentication required when running valkey-cli You omitted -a YOUR_PASSWORD. Always pass -a or set REDISCLI_AUTH in your shell: export REDISCLI_AUTH=YOUR_PASSWORD.

WARNING: 32 bit instance detected on 64-bit VPS This is cosmetic on modern kernels if the binary compiled correctly. Verify with valkey-server --version; it should say 64 bit.

AOF file not appearing Confirm appendonly yes is in valkey.conf and the dir path is /var/lib/valkey. Check ownership: ls -la /var/lib/valkey — must be valkey:valkey.

High memory usage beyond maxmemory 256mb Valkey's RSS can exceed maxmemory due to memory fragmentation. Check valkey-cli -a PASSWORD info memory and look at mem_fragmentation_ratio. A ratio above 1.5 means fragmentation is significant; a restart reclaims it on low-traffic servers.

make fails with cc: not found Run sudo apt install -y build-essential and retry.


Next Steps

With Valkey on VPS running and persistent, a few additions are worth considering:

  • TLS between app and Valkey: Add tls-port 6380 and point to your Let's Encrypt certs if the app runs on a different server.
  • Valkey Sentinel: For a two-node Hetzner setup, Sentinel gives you automatic failover without a managed service.
  • Logrotate: Add /etc/logrotate.d/valkey to rotate /var/log/valkey/valkey.log weekly so it doesn't fill the disk on a busy instance.
  • Monitoring: Expose Valkey metrics to Prometheus via redis_exporter (fully compatible with Valkey) and graph them in Grafana — both run comfortably on the same $6 VPS with this configuration.

The total monthly cost for this stack: $3.90–$5.77 for the VPS, $0 for the software. That's the whole point.