Where to start First steps track · 15 stages
Linux Labs

Redis — in-memory key-value store

5 min Ubuntu 24.04 · Redis 7

Cache, queues, and sessions in RAM; KEYS * blocks the server on large databases — use --scan instead.

Cheat sheet

6 commands Printable version
  • redis-cli ping check the server is alive (expect PONG)
  • redis-cli info memory current memory usage
  • redis-cli --bigkeys find the largest keys
  • redis-cli --scan --pattern 'x:*' safe key scan, does not block the server
  • redis-cli slowlog get 10 10 most recent slow commands
  • redis-cli config set maxmemory 512mb change a parameter on the fly

Overview

Redis is a fast in-memory key-value store (cache, queues, sessions, a broker for Channels/Celery). The redis-server service runs under systemd, listens on port 6379, config at /etc/redis/redis.conf.

bash
systemctl status redis-server
redis-cli ping                    # → PONG if alive
ss -tlnp | grep 6379              # is it listening (see ss)

Configuration

Key settings in /etc/redis/redis.conf:

bind 127.0.0.1                    # listen locally only (safe default)
requirepass SECRET                 # password (REQUIRED if exposed to the network)
maxmemory 256mb
maxmemory-policy allkeys-lru      # what to evict when full (for a cache)
appendonly yes                    # AOF persistence (durable)
save 900 1                        # RDB snapshot: if ≥1 change in 900s
  • RDB — periodic snapshots (compact, can lose the last few seconds).
  • AOF (appendonly yes) — an operation log (durable, more precise recovery).

Change on the fly without a restart: redis-cli config set maxmemory 512mb plus config rewrite. After editing the file — sudo systemctl restart redis-server.

Recipes

1

redis-cli

bash
redis-cli                         # interactive console
redis-cli -a SECRET                # with a password (or AUTH inside)
redis-cli info server             # version, uptime
redis-cli info memory             # memory usage
redis-cli info clients            # number of connections
redis-cli dbsize                  # number of keys
redis-cli --scan --pattern 'x:*'  # SAFE key scan (non-blocking)
redis-cli set k v ex 60           # write a key with a 60-second TTL
redis-cli get k                   # read a key
redis-cli ttl k                   # seconds left before the key expires
redis-cli del k                   # delete a key
redis-cli --bigkeys               # find large keys (what eats memory)
redis-cli monitor                 # stream of ALL commands (debug; adds load!)
redis-cli slowlog get 10          # 10 most recent slow commands

Troubleshooting

5

Could not connect to Redis (6379)

The service isn't running (systemctl status redis-server) or it only listens on 127.0.0.1 while the client connects over the network. For network access, fix bind and make sure to set requirepass (an open Redis is an instant compromise).

OOM / memory usage grew

redis-cli info memory, --bigkeys. For a cache, set maxmemory plus maxmemory-policy, otherwise Redis eats all RAM and the OOM killer kills it (see memory-swap).

Data disappeared after a restart

By default data lives only in memory. Enable persistence (appendonly yes and/or save) if the data matters.

Slow / high latency

redis-cli --latency, slowlog get. Common causes: KEYS/SMEMBERS on huge structures, swapping (Redis should NEVER swap — see memory-swap), a background RDB fork under memory pressure.

NOAUTH Authentication required

requirepass is set — connect with redis-cli -a SECRET or send AUTH as the first command.

Danger zone

KEYS * enumerates every key and blocks the server until it finishes — on a database with millions of keys this feels like a full Redis outage for every client at once; use --scan instead, which never blocks. FLUSHALL/FLUSHDB erase all data immediately with no confirmation step — there is no "are you sure?" and no undo. A Redis instance without bind 127.0.0.1 and without requirepass, exposed to the network, gets compromised almost instantly: never publish port 6379 without a password.

See also

  • systemctl — managing systemd services.
  • ss — Who is listening on which ports and which connections are open — a modern netstat replacement.
Loading…