Log inSign up

Run Redis or Valkey as a secured in-memory store

Install redis-server with apt on Ubuntu, valkey-server or redis-server on Debian, the redis module stream or EPEL valkey on Rocky and Alma, and pkg install redis on FreeBSD. Then keep it bound to 127.0.0.1, add an ACL user with a long password, switch on append-only persistence if the data matters, and set maxmemory with an eviction policy so the cache can never starve the rest of the server.

Topic: Databases · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9, FreeBSD 14 · Updated 2026-12-03

Should you install Redis or Valkey?

Both work for caching, sessions, queues and rate limits, and both speak the same protocol, so client libraries for PHP, Python, Node.js and Go connect to either without changes. The choice comes down to licensing and what your distro packages.

Redis changed its license in 2024, which led the Linux Foundation community to fork the last BSD-licensed code as Valkey. Redis 8 later added the AGPL as a license option. In practice:

  • Ubuntu 24.04 and 22.04 ship redis-server in the archive, built from the older BSD-licensed series.
  • Debian 13 carries valkey-server next to redis-server; Debian 12 offers redis-server from the 7.0 series.
  • Rocky Linux 9 and AlmaLinux 9 ship Redis in AppStream (6.2 by default, 7 as a module stream), and EPEL provides valkey.
  • FreeBSD 14 has both redis and valkey in the package collection.

If you are starting fresh and have no tie to a vendor feature, Valkey is a sound default where your distro offers it. Everything below applies to both; the file and command names change from redis to valkey as noted.

How do you install it on Ubuntu or Debian?

Use apt and check which version the archive offers before installing. The package starts the service right away and listens only on the loopback address.

sudo apt update
apt policy redis-server valkey-server
# Redis from the archive
sudo apt install -y redis-server
redis-server --version
sudo systemctl enable --now redis-server
# or Valkey on Debian 13
sudo apt install -y valkey-server
valkey-server --version
sudo systemctl enable --now valkey-server

For the newest Redis release on Ubuntu or Debian, add the upstream APT repository and install the redis package from it:

sudo apt install -y curl gpg lsb-release
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt update && sudo apt install -y redis

The main config lives at /etc/redis/redis.conf (or /etc/valkey/valkey.conf). Log in first as your sudo user; creating a sudo user covers that step.

How do you install it on Rocky Linux, AlmaLinux or FreeBSD?

On the Enterprise Linux 9 family, pick a module stream for Redis or enable EPEL for Valkey, then enable the unit. FreeBSD uses pkg and sysrc.

Rocky Linux 9 and AlmaLinux 9

sudo dnf module list redis
sudo dnf module install -y redis:7
sudo systemctl enable --now redis
redis-server --version
# Valkey instead
sudo dnf install -y epel-release
sudo dnf install -y valkey
sudo systemctl enable --now valkey

The config file is /etc/redis/redis.conf for Redis 7 and /etc/valkey/valkey.conf for Valkey.

FreeBSD 14

sudo pkg install -y redis
sudo sysrc redis_enable=YES
sudo service redis start
redis-cli ping

On FreeBSD the config sits at /usr/local/etc/redis.conf. Replace redis with valkey in the three commands to get the fork.

How do you require a password with ACLs?

Create named users in an ACL file and switch off the open default user. ACLs replace the old single requirepass line and let you give each application only the commands and key prefixes it needs.

Generate a long random secret first:

openssl rand -base64 32

Point the server at an ACL file by adding this line to the config file:

aclfile /etc/redis/users.acl

Then create that file, owned by the service user (redis, or valkey for the fork) with mode 640:

user default off
user admin on >Replace-With-Long-Secret-1 ~* &* +@all
user shop on >Replace-With-Long-Secret-2 ~shop:* -@all +@read +@write +@keyspace -@dangerous
sudo chown redis:redis /etc/redis/users.acl
sudo chmod 640 /etc/redis/users.acl
sudo systemctl restart redis-server     # redis on Rocky and Alma, valkey-server for Valkey

The shop user can only touch keys that start with shop: and cannot run admin commands such as FLUSHALL or CONFIG. Test both accounts:

redis-cli --user shop --askpass SET shop:hello world
redis-cli --user shop --askpass GET shop:hello
redis-cli --user admin --askpass ACL LIST

Use valkey-cli in place of redis-cli when you run the fork.

Where should Redis listen?

Keep it on the loopback addresses, which is the packaged default, and leave protected-mode yes in place. The relevant lines in the config file look like this:

bind 127.0.0.1 -::1
protected-mode yes
port 6379

Confirm what the process actually listens on:

sudo ss -ltnp | grep 6379

For apps on the same machine you can go one step further and use a Unix socket, which skips TCP entirely:

unixsocket /run/redis/redis-server.sock
unixsocketperm 770

Add the web server user (for example www-data) to the redis group so it can open the socket. When a second server needs access, carry the traffic over an SSH tunnel or a WireGuard link rather than opening 6379 publicly; see remote database access. If you ever must listen on a public address, enable tls-port 6380 with a certificate, set port 0 to close plain text, and allow only the client address in your firewall.

How do you choose persistence: RDB, AOF or none?

Pick none for a pure cache, RDB snapshots when losing a few minutes of writes is fine, and AOF when queued jobs or sessions must survive a crash. Many setups run RDB and AOF together.

# RDB snapshot after 3600 s with 1 change, 300 s with 100 changes, 60 s with 10000 changes
save 3600 1 300 100 60 10000
dbfilename dump.rdb
dir /var/lib/redis
# append-only file, fsync once per second
appendonly yes
appendfsync everysec

For a cache that may lose everything on restart, disable both:

save ""
appendonly no

Since version 7, the AOF is a directory of base and incremental files that the server rewrites in the background. Trigger a snapshot by hand and check the result with:

redis-cli --user admin --askpass BGSAVE
redis-cli --user admin --askpass INFO persistence | grep -E 'rdb_last_bgsave_status|aof_enabled'

Copy dump.rdb and the appendonlydir folder off the server as part of your backup routine; the optional daily backups add-on keeps a whole-disk copy on top of that.

How do you cap memory with maxmemory?

Set maxmemory to a fixed share of RAM and choose an eviction policy, otherwise the dataset can grow until the kernel kills a process. A cache should evict old keys; a queue or session store should refuse writes instead.

# a cache on a 2 GB server
maxmemory 512mb
maxmemory-policy allkeys-lru
# a store that must not lose keys
# maxmemory-policy noeviction

Background saves fork the process, so leave headroom: on a small server keep maxmemory under half of RAM when persistence is on. Two kernel settings remove the warnings the server prints at start:

echo 'vm.overcommit_memory = 1' | sudo tee /etc/sysctl.d/90-redis.conf
sudo sysctl --system

A swap file protects against sudden spikes, but a store that lives in swap becomes slow, so size the plan so the data fits in memory.

Read live figures at any time:

redis-cli --user admin --askpass INFO memory | grep -E 'used_memory_human|maxmemory_human|mem_fragmentation_ratio'

How do you test, upgrade and troubleshoot?

Use the CLI tools for a round trip test, upgrade with the normal package manager, and read the journal when the service fails to start.

redis-cli --user shop --askpass PING
redis-cli --user admin --askpass --latency
redis-benchmark -q -n 20000 --user admin -a 'Replace-With-Long-Secret-1'
sudo journalctl -u redis-server -n 50 --no-pager
sudo tail -n 50 /var/log/redis/redis-server.log

Routine updates arrive with sudo apt upgrade or sudo dnf upgrade; a restart briefly drops connections, and the dataset reloads from RDB or AOF. Common start failures are an ACL file with the wrong owner, a typo in a user line, and a dir path the service user cannot write. If clients report NOAUTH or WRONGPASS, check that the application config names the user as well as the password. To move from Redis to Valkey, stop Redis, copy dump.rdb into the Valkey data directory and start Valkey: it reads the file as is.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1small session store or cache1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A1-2cache plus app on one server1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4larger data set with persistence2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Install Redis or Valkey

Is Valkey compatible with Redis clients?

Yes. Valkey forked from Redis 7.2.4 and keeps the same wire protocol and command set, so libraries such as phpredis, redis-py and ioredis connect without code changes.

Do I still need requirepass?

No. requirepass only sets a password on the default user. ACL files are clearer: switch the default user off and create one user per application with its own key prefix.

How much RAM does Redis need?

The server itself needs only a few megabytes. Plan memory for your data set plus room for background saves, which can briefly need extra memory while writes continue. A 1 to 2 GB server suits a session store or a page cache for one site.

Why does Redis warn about overcommit_memory?

Background saves use fork. With the default kernel setting, a fork on a busy server can fail. Setting vm.overcommit_memory = 1 lets the fork succeed without copying memory up front.

Can I run several Redis instances on one server?

Yes. Give each its own config file, port or socket, data directory and systemd unit. On Ubuntu and Debian the redis-server@ template unit reads /etc/redis/redis-NAME.conf for each instance.

Should I open port 6379 to reach Redis from home?

No. Use an SSH tunnel such as ssh -N -L 6379:127.0.0.1:6379 deploy@198.51.100.24 and point your client at localhost. Open ports are scanned constantly.