Redis is the closest a database comes to “just running” — a single binary, no schema, no driver-side surprises. The defaults are also wrong for production: open to the network, no password, no persistence configured, and a memory policy that does nothing when the server fills up. This article installs Redis 7 on Ubuntu 22.04/24.04, configures authentication and binding, sets a sensible eviction policy, and verifies the result before any application connects.
How to verify
# Before install
lsb_release -a
dpkg -l | grep redis
ss -tlnp | grep 6379
# After install
sudo systemctl status redis-server --no-pager
redis-cli ping
# Expected: PONG (or NOAUTH if password is set)
redis-cli INFO server | head -20
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy
What’s happening
Ubuntu’s universe repository carries Redis but typically a version behind upstream — for production use the Redis.io apt repository to get current major versions and timely patches. After install, the daemon runs as user redis, listens on 127.0.0.1:6379, has no password (default protected-mode on Ubuntu forces password or local-only), and persists with RDB snapshots only (no AOF).
Memory in Redis is fundamentally constrained — by default, maxmemory is unset (allow up to host RAM) and maxmemory-policy is noeviction (return errors when out of memory). For a cache use case you almost always want a bounded maxmemory and an LRU/LFU eviction policy. For a primary store, configure persistence (RDB + AOF) and a separate sizing approach.
Redis is single-threaded for command execution. A long command (a KEYS * on a million-key database) blocks all other commands. Provision accordingly and never run KEYS in production — use SCAN.
The procedure
-
Add the Redis.io apt repository (recommended for production).
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 -
Install Redis server and CLI.
sudo apt install -y redis sudo systemctl enable --now redis-server sudo systemctl status redis-server --no-pager -
Edit
/etc/redis/redis.confwith the production-minimum hardening:# --- Binding --- bind 127.0.0.1 10.0.0.5 protected-mode yes port 6379 # --- Authentication --- # Choose ONE of these patterns. requirepass is the legacy approach; # ACL users are the modern, granular approach (Redis 6+). requirepass strong-password # Or with ACL: # user default off # user appuser on >strong-password ~cached:* +@read +@write +@string +@hash +@list +@set # --- Memory --- maxmemory 4gb maxmemory-policy allkeys-lru # other policies: noeviction, allkeys-lfu, volatile-lru, volatile-ttl # --- Persistence (see redis-persistence-rdb-aof article) --- save 900 1 save 300 10 save 60 10000 appendonly yes appendfsync everysec # --- Tuning --- tcp-backlog 511 timeout 0 tcp-keepalive 60 databases 16 # --- Logging --- logfile /var/log/redis/redis-server.log loglevel notice -
Restart and confirm.
sudo systemctl restart redis-server redis-cli -a strong-password ping # Expected: PONG -
Lock down kernel settings that Redis warns about on startup.
# Overcommit memory (Redis can fork for BGSAVE) echo 'vm.overcommit_memory = 1' | sudo tee /etc/sysctl.d/99-redis.conf sudo sysctl -p /etc/sysctl.d/99-redis.conf # Disable transparent hugepages (latency spikes) echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled # Persist across reboots — add to a systemd one-shot or /etc/rc.local # Increase somaxconn to match tcp-backlog echo 'net.core.somaxconn = 1024' | sudo tee -a /etc/sysctl.d/99-redis.conf sudo sysctl -p /etc/sysctl.d/99-redis.conf -
Open the firewall for the application subnet:
sudo ufw allow from 10.0.0.0/24 to any port 6379 proto tcp sudo ufw status verbose -
Enable TLS in front of Redis 6+ by setting:
port 0 # disable plaintext tls-port 6380 tls-cert-file /etc/ssl/redis/server.crt tls-key-file /etc/ssl/redis/server.key tls-ca-cert-file /etc/ssl/redis/ca.crt tls-auth-clients optionalAnd connect with
redis-cli --tls --cacert /etc/ssl/redis/ca.crt -h .... -
Verify the production configuration.
redis-cli -a strong-password CONFIG GET maxmemory redis-cli -a strong-password CONFIG GET maxmemory-policy redis-cli -a strong-password CONFIG GET appendonly redis-cli -a strong-password INFO persistence redis-cli -a strong-password INFO memory
Common pitfalls
- Default
protected-mode yesplus a bind line that includes a non-loopback IP means Redis demands a password — without one, the daemon refuses connections from non-loopback. Set the password. KEYS *in production blocks the server for the duration of the scan. UseSCAN cursor MATCH pattern COUNT 100instead, which is incremental.- Without
maxmemory, Redis grows to fill RAM and is killed by the OOM killer or pages to swap, both of which kill latency. - Persistence to a different disk (RDB and AOF on the same disk as the OS) means fsync stalls during snapshots — give Redis a dedicated data volume on persistent SSD.
- Transparent hugepages cause multi-second latency spikes during BGSAVE forks — always disable on Redis hosts.
In the engagements we run, Redis hosts come up with a baseline config that includes requirepass from a vault, persistence to a dedicated volume, sysctl hardening, and the Redis exporter wired to Prometheus before any traffic. We operate that under Managed Operations — and for clients running clustered or Sentinel-managed Redis, the install role extends to the HA bootstrap.