Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 42 ยท Operations, Security and Performance

Running Redis in Production

Run Redis in production: Docker Compose and Kubernetes basics, managed services, Linux settings, backups, rolling upgrades, capacity planning and Valkey.

Redis is easy to start and surprisingly easy to run badly. The default configuration is designed for a laptop: it has no memory limit, no password, and it assumes that losing the data is not a catastrophe. Moving to production means answering a handful of questions on purpose. Where will it run, and who patches it? How much memory does it need, and what happens when that runs out? How is data backed up, and how do you upgrade without downtime? Which Redis-compatible server do you actually want?

This lesson is a practical checklist. It builds on persistence, replication, Sentinel, Cluster, security, performance and monitoring and ties them together into a deployment.

Redis cannot run on this page, so the Docker, Kubernetes, shell and redis.conf snippets are read-only examples: copy them as a starting point, not as a finished design. The two runnable TryIts are plain JavaScript stand-ins and not Redis: a capacity calculator whose numbers are stated assumptions (not measurements), and a small configuration checker with a few of the rules an experienced operator applies by eye.

Where can Redis run?

A server you run Full control, you do the patching.
Containers Docker Compose or Kubernetes.
Managed service The provider runs it for you.
1 / 2

Choosing where to run it

You have three realistic options, and the right one depends on your team more than on Redis.

A managed service is the default choice for most teams. Cloud providers and Redis's own company sell hosted Redis-compatible databases: Amazon ElastiCache, Google Cloud Memorystore, Azure's managed Redis offering and Redis Cloud are well-known examples (names and engine choices change, so check each provider's current documentation). You get provisioning, automatic failover, backups, patching and metrics without running servers. The trade-offs are cost at scale, a limited set of settings (commands such as CONFIG are often restricted), instances that are typically reachable only from inside the provider's private network, and an upgrade schedule you do not fully control.

Self-managed servers (virtual machines or bare metal) give full control and the lowest cost per gigabyte, and in exchange everything in this lesson is your job.

Containers sit in between. Docker Compose is fine for a single host. Kubernetes can run Redis, but it is stateful: it needs a persistent volume, a stable network identity and careful memory limits, and you will probably want an operator or a well-maintained chart for replication and failover rather than hand-written manifests. Evaluate how actively any operator or chart is maintained before depending on it.

Docker Compose basics

For a single host, a Compose file with a pinned image, a named volume, a health check and a localhost-only port is a solid start. The official image keeps its data in /data.

YAML
services:
  redis:
    image: redis:8             # in production, pin an exact version tag
    command: ["redis-server", "/usr/local/etc/redis/redis.conf",
              "--requirepass", "${REDIS_PASSWORD}"]
    environment:
      REDISCLI_AUTH: ${REDIS_PASSWORD}   # lets the health check log in
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf:ro
      - redis-data:/data
    ports:
      - "127.0.0.1:6379:6379"  # reachable from this host only
    mem_limit: 2g
    stop_grace_period: 30s     # time for a clean shutdown and final save
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5

volumes:
  redis-data:

And a matching redis.conf:

Text
# Inside the container Redis listens on all addresses;
# the published port in Compose decides who can really connect.
bind 0.0.0.0 -::*
# Leaves room under the 2 GB container limit.
maxmemory 1536mb
# Primary data must not be evicted. Use allkeys-lru or allkeys-lfu for a pure cache.
maxmemory-policy noeviction
appendonly yes
appendfsync everysec
save 3600 1 300 100 60 10000
dir /data

Two details matter here. The container memory limit (2 GB) is deliberately higher than maxmemory (1.5 GB), because Redis also needs memory for client buffers, replication buffers and the copy-on-write cost of background saves; a container whose limit equals maxmemory gets killed by the kernel. And a password given on the command line is visible in the process list inside the container, so for stricter setups mount an ACL file or a secret instead.

Kubernetes basics

A single Redis node on Kubernetes is a StatefulSet with one replica, a volume claim for /data, equal memory requests and limits, and a probe. This sketch shows the essentials, and is not a high-availability setup.

YAML
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: redis           # a headless Service gives the pod a stable DNS name
  replicas: 1
  selector:
    matchLabels: { app: redis }
  template:
    metadata:
      labels: { app: redis }
    spec:
      containers:
        - name: redis
          image: redis:8       # pin an exact version
          args: ["/etc/redis/redis.conf", "--requirepass", "$(REDIS_PASSWORD)"]
          env:
            - name: REDIS_PASSWORD
              valueFrom: { secretKeyRef: { name: redis-auth, key: password } }
            - name: REDISCLI_AUTH
              valueFrom: { secretKeyRef: { name: redis-auth, key: password } }
          ports: [{ containerPort: 6379 }]
          resources:
            requests: { memory: 2Gi, cpu: 500m }
            limits: { memory: 2Gi }
          readinessProbe:
            exec: { command: ["redis-cli", "ping"] }
            periodSeconds: 10
          volumeMounts:
            - { name: data, mountPath: /data }
            - { name: config, mountPath: /etc/redis }
      volumes:
        - name: config
          configMap: { name: redis-config }
  volumeClaimTemplates:
    - metadata: { name: data }
      spec:
        accessModes: ["ReadWriteOnce"]
        resources: { requests: { storage: 10Gi } }

Things that bite people on Kubernetes: a memory limit that is too close to maxmemory (the pod is killed with OOMKilled), a liveness probe that is too aggressive during a long data load or save (the pod restarts in a loop), storage that is slow enough to stall the append-only file, and node-level Linux settings that a pod cannot change. For replication and automatic failover you need Sentinel or Redis Cluster running as additional pods, which is where operators earn their keep.

Linux settings that matter

Redis prints warnings at startup when the host is badly configured; read them. Three settings come up again and again, and most are host-wide, so in Docker or Kubernetes you set them on the node, not inside the container.

Terminal
# 1. Let the fork() for background saves succeed even when memory looks tight
sudo sysctl -w vm.overcommit_memory=1

# 2. Allow a longer queue of pending connections (Redis defaults to tcp-backlog 511)
sudo sysctl -w net.core.somaxconn=1024

# 3. Avoid transparent huge pages, which can cause latency spikes after fork
echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabled

To keep the first two across reboots, put them in a file such as /etc/sysctl.d/99-redis.conf. The huge-pages setting is usually made permanent in the boot configuration or a small systemd unit. Also make sure the process may open enough files: the limit on open files caps maxclients. Finally, avoid swapping: a Redis process whose memory is pushed out to disk can be slower than a traditional database, so size the machine so that swap is never needed.

The redis.conf file and CONFIG at runtime

Redis reads its settings from a plain text file given as the first argument (redis-server /etc/redis/redis.conf), and optionally from the command line. Each line is a directive name followed by its values, # starts a comment, and the file you get with the source or package documents every option in comments. Later lines win over earlier ones, and command-line options (redis-server --port 6380 --maxmemory 256mb) override the file.

A short tour of the directives you will meet most often:

Directive What it controls
bind, port, protected-mode Which addresses and port accept clients (see security).
requirepass, aclfile Authentication for the default user, or a file of ACL users.
dir, dbfilename, appendonly, appendfsync, save Where and how data is written to disk (see persistence).
maxmemory, maxmemory-policy The memory ceiling and what to evict at it (see eviction).
maxclients, timeout, tcp-keepalive Connection limits and idle handling.
replicaof, masterauth Make this server a replica (see replication).
loglevel, logfile, slowlog-log-slower-than What is logged and which slow commands are recorded.
include /path/other.conf Splits settings across files, for example one per environment.

CONFIG GET, SET and REWRITE

Most settings can be inspected and changed on a running server, without a restart:

Text
CONFIG GET maxmemory*          # glob pattern; returns name and value pairs
CONFIG SET maxmemory 512mb     # takes effect immediately
CONFIG SET maxmemory-policy allkeys-lru save "300 100"   # several settings, applied together
CONFIG REWRITE                 # write the running settings back into redis.conf
CONFIG RESETSTAT               # reset the counters shown by INFO

Four rules explain nearly every surprise:

  1. CONFIG SET is not persistent. It changes memory only. After a restart the file wins again, so a fix made during an incident vanishes unless you follow it with CONFIG REWRITE (or edit the file).
  2. CONFIG REWRITE keeps your comments and layout as far as it can and only touches the lines it must. It fails if Redis was started without a config file ("The server is running without a config file").
  3. Some settings cannot change at runtime, for example daemonize, databases and cluster-enabled. CONFIG SET returns an error for those, so they need a restart.
  4. Multi-setting CONFIG SET is all or nothing: if one value is invalid, none of them change.

The CONFIG command can be withheld from users with ACLs (for example by removing the @dangerous category), and managed services often disable it entirely and offer a settings screen instead. Never expose a writable CONFIG command to untrusted users: CONFIG SET dir plus CONFIG SET dbfilename was once used to make Redis write files anywhere the process could.

The model below behaves like CONFIG GET, SET and REWRITE on a small set of settings: glob matching, atomic multi-set, read-only settings, and a config file that only changes on REWRITE.

JS CONFIG GET, SET and REWRITE

Press Run (or Ctrl + Enter) to see the console output here.

No output.

After the first restart the policy and memory limit return to the file's values, because CONFIG SET alone never touched the file. The second restart keeps 512mb because CONFIG REWRITE ran first. The invalid maxmemory-policy left appendonly unchanged too, which is the all-or-nothing rule at work.

Backups and upgrades

Backups. The RDB snapshot file is a compact, point-in-time copy and the usual unit of backup. Redis writes it to a temporary file and renames it into place, so copying dump.rdb is safe. A typical routine:

Terminal
redis-cli BGSAVE                          # ask for a snapshot in the background
redis-cli INFO persistence                # wait until rdb_bgsave_in_progress is 0
cp /data/dump.rdb /backups/dump-$(date +%F).rdb
# or pull a snapshot over the network without touching the server's disk:
redis-cli --rdb /backups/remote-$(date +%F).rdb

Take backups from a replica when you have one, so the primary does not pay for the fork, ship the files off the machine (object storage, another region) and practice restoring them. A backup that was never restored is a hope, not a backup. With the append-only file, the data lives in a directory (appendonlydir) of several files that belong together, so copy it as a whole.

Upgrades. Upgrading a single node means downtime, so production setups with a replica do a rolling upgrade: upgrade the replicas first, wait until they are in sync again, hand over the primary role with the FAILOVER command (or CLUSTER FAILOVER per shard in a cluster), and then upgrade the old primary, which is now a replica. Read the release notes between your version and the target, test the new version with your real commands, scripts and client library in staging, and keep a backup taken just before.

flowchart LR
  A["Upgrade a replica"] --> B["Wait until it is in sync"]
  B --> C["FAILOVER: replica becomes primary"]
  C --> D["Upgrade the old primary"]

A rolling upgrade keeps one node serving at every step.

Capacity planning

Redis keeps the whole dataset in RAM, so capacity planning is mostly memory arithmetic. The honest way to get the numbers is to measure: load a representative sample, ask MEMORY USAGE key for typical keys, look at INFO memory, and scan for big keys with redis-cli --bigkeys or --memkeys. Then add headroom for three things: growth, the allocator's fragmentation, and, if you persist data, the copy-on-write memory a background save can need while writes continue. A common rule of thumb is to plan for the instance to use well below the machine's memory when persistence is on, and more headroom still for write-heavy workloads.

The TryIt below turns those ideas into a small calculator. Every default is an assumption written at the top of the code, which you are meant to change: 80 bytes of bookkeeping per key, a 20% allocator factor, 25% growth and a copy-on-write multiplier. Real overheads depend on the data type, the Redis version and the encoding, so treat the output as the shape of the calculation, not as a quote.

JS A capacity planner built on stated assumptions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice what the three rows teach. The page cache holds five times as many keys as the sessions workload but costs only about 20% more in total, because it needs no persistence headroom and no replica. The sessions workload costs roughly three times its dataset in total RAM because of the persistence headroom plus a replica. The small job queue needs little memory per node but is replicated twice, so replicas multiply the bill. The calculator is also a reminder of why maxmemory must always sit below the RAM of the machine or container.

A configuration checker

Experienced operators scan a config for the same handful of mistakes. The second TryIt encodes a few of them as rules: a server reachable from everywhere without a password, no maxmemory, an eviction policy that does not match the role (a cache should evict; primary data must not), persistence without the matching kernel setting, huge pages, and a connection backlog the kernel will silently cap. It is a teaching sketch with simple rules, not a replacement for Redis's own startup warnings or a security audit.

JS A tiny redis.conf checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first case is the classic accident: a development file with bind 0.0.0.0 and protected-mode no copied to a server. Every rule fires, and the two errors are the ones that lead to real data theft and ransom incidents, as described in Securing Redis. The second case is nearly right, but its maxmemory of 1,900 MB leaves almost no room in a 2 GB container, which is exactly the mistake that ends in OOMKilled. The third passes: bounded memory, no eviction for primary data, durable writes and a password.

Redis, Valkey and the alternatives

In 2024 the licence of Redis changed from the permissive BSD licence to source-available licences, and the community and several cloud providers responded by creating Valkey, a fork of Redis 7.2.4 hosted by the Linux Foundation under the BSD licence. In 2025, Redis 8 added the AGPLv3 as an additional licence option and merged formerly separate modules (JSON, search, time series, probabilistic structures) into the core. As a result you now have real choices, and the licence terms are worth reading with your legal team before you decide.

The main choices today

Redis The original. Redis 8 bundles JSON, search and more.
Valkey Open-source fork of Redis 7.2, drop-in for most uses.
Others KeyDB, Dragonfly and Garnet speak the Redis protocol.
Memcached A simple cache only, with no data structures.

Compatibility differs per version and feature. Test your real commands.

1 / 2

For a plain cache or a straightforward use of strings, hashes, sorted sets and lists, any Redis-compatible server and any mainstream client library will work. The differences appear at the edges: Lua scripts, modules and the query engine, cluster behavior, and the exact set of commands. The practical advice is the same as for upgrades: pick the server your managed provider or your team can support well, test your actual workload against it in staging, and write your application against the common commands so that switching later stays possible.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A container has a 2 GB memory limit. Which maxmemory is the safest choice?

    Redis needs memory beyond the dataset for client buffers, replication buffers and the copy-on-write cost of saves. A maxmemory equal to the limit invites an out-of-memory kill.
  2. 2. Why is vm.overcommit_memory=1 recommended on a Redis host that persists data?

    BGSAVE and AOF rewrites fork the process. With strict overcommit rules the fork can fail even though copy-on-write needs little extra memory.
  3. 3. What is the safest way to upgrade a primary with one replica without long downtime?

    A rolling upgrade keeps one node serving at all times. The FAILOVER command hands the primary role to the upgraded replica.
  4. 4. Which statement about backups is the most important?

    A backup on the failed disk is lost with it, and a backup that was never restored may not work when you need it.
  5. 5. What is Valkey?

    Valkey was created after the 2024 Redis licence change, starting from the last BSD-licensed Redis code, and is developed in the open.

Summary

  • Pick the hosting model first: managed services remove most operations work, containers and Kubernetes need stateful care, and self-managed servers give control at the price of doing everything yourself.
  • Always set maxmemory, an eviction policy that matches the role, a password or ACLs and a private network; keep container limits above maxmemory.
  • Set vm.overcommit_memory=1, avoid transparent huge pages, and align somaxconn with tcp-backlog; most of these are host-wide settings.
  • Back up from a replica, move copies off the machine and rehearse restores; upgrade by rolling replicas first and failing over.
  • Plan capacity by measuring real data, then add headroom for growth, fragmentation, forks and replicas.
  • Redis, Valkey and other compatible servers are real alternatives: check licences and test the features you use.

Now put everything together in a first hands-on build: Project: URL Shortener with Click Stats.