Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 16Redis · Data

Install and set up Redis (or Valkey)

Redis keeps data in memory, so it answers in less than a millisecond. Apps use it as a cache, for sessions, queues, rate limits and counters. Valkey is the open-source copy of Redis with the same commands (RHEL 10 ships Valkey). This guide installs Redis or Valkey on RHEL and Ubuntu, makes the settings that keep it safe and stable (local only, a password, a memory limit, persistence), and shows the daily commands and fixes.

  • 9 Lessen
  • 10 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

16.1

Know it before you install

Redis is a key → value store in RAM. When memory is full, it removes keys by a rule (eviction) or refuses writes. It can save the data to disk (RDB snapshots and/or the AOF log) so it survives a restart.

ItemRHEL 9RHEL 10Ubuntu / Debian
Packageredisvalkeyredis-server
Serviceredisvalkeyredis-server
Config/etc/redis/redis.conf/etc/valkey/valkey.conf/etc/redis/redis.conf
Clientredis-clivalkey-cliredis-cli
Data/var/lib/redis//var/lib/valkey//var/lib/redis/
Port6379/tcp6379/tcp6379/tcp

Let op

Redis has no real security when it is open to the network without a password. Attackers scan the internet for port 6379 and use open servers to write SSH keys or crypto miners. Never make it public.

16.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install redis
systemctl enable --now redis
redis-cli ping
PONG

On RHEL 10: dnf -y install valkey, systemctl enable --now valkey, and valkey-cli ping. Everything below works the same — only the names change.

16.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y redis-server
redis-cli ping
PONG

Op het werk

For the newest Redis version use the official repository packages.redis.io, or the container redis:8 / valkey/valkey:8.

16.4

First configuration: the settings to make first

Edit the config file (RHEL 9 and Ubuntu: /etc/redis/redis.conf). Find each setting and change it:

/etc/redis/redis.conf
bind 127.0.0.1 -::1
protected-mode yes
port 6379
requirepass a-very-long-random-password-here
maxmemory 512mb
maxmemory-policy allkeys-lru
appendonly yes
appendfsync everysec
supervised systemd
rename-command FLUSHALL ""
rename-command CONFIG   ""
SettingWhy
bind 127.0.0.1 -::1Listen only on this server. Add the private IP only when app servers need it.
requirepassEvery client must AUTH first. Better: ACL users (below).
maxmemoryRedis may use only this much RAM — the server does not run out of memory.
maxmemory-policy allkeys-lruFor a cache: remove the least used keys when full. For queues/sessions use noeviction.
appendonly yesWrite every change to the AOF log; lose at most 1 second of data after a crash.
rename-command … ""Turn off dangerous commands (empty all data, change config at runtime).

Set the kernel settings Redis asks for in its log, and restart:

echo 'vm.overcommit_memory = 1' > /etc/sysctl.d/90-redis.conf
sysctl --system
systemctl restart redis
redis-cli -a 'a-very-long-random-password-here' --no-auth-warning ping
PONG

Users with their own passwords and limited rights (ACL, Redis 6+), instead of one shared password:

redis-cli
ACL SETUSER app on >app-long-password ~app:* +@read +@write -@dangerous
ACL SETUSER default off
ACL LIST
CONFIG REWRITE

(Run CONFIG REWRITE before you turn the CONFIG command off, or put the users in an aclfile.)

When app servers on other machines need Redis, add the private IP to bind, and open the port only for them:

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" port port="6379" protocol="tcp" accept'
firewall-cmd --reload

16.5

Daily use: the commands and the real uses

redis-cli --user app --pass app-long-password
redis-cli INFO memory | grep -E 'used_memory_human|maxmemory_human|evicted'
redis-cli INFO keyspace
redis-cli --scan --pattern 'session:*' | head
redis-cli SLOWLOG GET 10
redis-cli --latency
redis-cli --bigkeys
redis-cli BGSAVE

The basic data commands:

redis-cli
SET page:home "<html>…" EX 300
GET page:home
TTL page:home
INCR rate:192.0.2.5
EXPIRE rate:192.0.2.5 60
LPUSH jobs '{"id":42}'
BRPOP jobs 0
HSET user:42 name Alice plan pro
HGETALL user:42

Real uses at work:

  • Cache for web apps and WordPress/WooCommerce (object cache), API responses.
  • PHP and app sessions shared by many web servers.
  • Job queues (Sidekiq, Celery, BullMQ), rate limits, counters, leaderboards.

Let op

Do not use KEYS * or MONITOR on a busy production server — they block or slow Redis. Use --scan and SLOWLOG.

16.6

Make it safe

Security checklist:

  • Never public: bind on localhost or a private IP, firewall, protected-mode yes.
  • ACL users with long passwords; the default user off.
  • Dangerous commands off or not allowed by ACL (FLUSHALL, FLUSHDB, CONFIG, DEBUG).
  • maxmemory always set.
  • TLS (tls-port) when traffic goes between servers or across networks.
  • Keep it updated; run it as the redis user (the package does this).

16.7

Update and remove

dnf -y upgrade redis
apt-get install --only-upgrade -y redis-server
dnf -y remove redis
apt-get purge -y redis-server

Back up the data files (dump.rdb, appendonlydir/) before a major upgrade.

Commando's in deze les

16.8

Fix common problems

ProblemCauseFix
NOAUTH Authentication requiredA password is set.redis-cli -a PASS or AUTH user pass; give the app the password.
OOM command not allowed when used memory > 'maxmemory'Memory full with policy noeviction.Raise maxmemory, set TTLs, or use allkeys-lru for a cache.
MISCONF Redis is configured to save RDB snapshots, but it's currently unable to persist to diskThe disk is full or the folder is not writable.Free disk space; check rights on /var/lib/redis.
Could not connect … Connection refused from another serverRedis listens only on localhost, or firewall.Add the private IP to bind, open 6379 for the app subnet.
WARNING Memory overcommit must be enabled! in the logKernel setting.vm.overcommit_memory = 1 (see above).
Redis is slow sometimesBig keys, KEYS *, or saving to disk.SLOWLOG GET, --bigkeys; appendfsync everysec.
Service does not start after editingA wrong line in the config.journalctl -u redis -n 30; fix the line.

16.9

Next steps

1Why is an open Redis port on the internet so dangerous?

Attackers can read and change all data, and use Redis to write files like SSH keys on the server.

2Which maxmemory-policy fits a cache, and which fits a job queue?

A cache: allkeys-lru. A queue or sessions: noeviction (no data may disappear).

Alle commando's in dit hoofdstuk