Redis in Production
Monitoring, security, and operational best practices
Skills taught: redis.production
Security First
Redis has no authentication by default. An internet-exposed Redis is a critical vulnerability — attackers can read all data, overwrite keys, or achieve remote code execution via config manipulation. Always secure before deploying.
# redis.conf — Security settings
# 1. Bind to private/localhost only (NOT 0.0.0.0)
bind 127.0.0.1
# Or bind to private IP only: bind 10.0.1.5
# 2. Require password
requirepass "use-a-long-random-password-here"
# 3. Enable TLS (Redis 6+)
tls-port 6380
tls-cert-file /etc/redis/redis.crt
tls-key-file /etc/redis/redis.key
tls-ca-cert-file /etc/redis/ca.crt
# 4. Disable dangerous commands
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command DEBUG ""
rename-command CONFIG "CONFIG-ADMIN-ONLY-SECRET"
# 5. Disable protected-mode if binding explicitly
protected-mode yes # keep enabled if bind not set
Access Control Lists (ACLs)
Redis 6+ supports ACLs — per-user permissions on commands and key patterns. This lets you give the app only what it needs, not full admin access.
# Create a restricted app user
redis-cli ACL SETUSER appuser on >app-password ~* +GET +SET +DEL +EXPIRE +TTL
# on = enabled
# > = password
# ~* = access all keys
# + = allow these commands only
# Create a read-only user
redis-cli ACL SETUSER readonly on >readonly-password ~* +@read
# List ACL users
redis-cli ACL LIST
# Check current user
redis-cli ACL WHOAMI
# ACLs in redis.conf
# user appuser on >password ~cache:* +GET +SET +EXPIRE
Memory Management
# redis.conf — Memory settings
# Set to 70-80% of available RAM
maxmemory 3gb
# Eviction policy for cache workloads
maxmemory-policy allkeys-lru
# LRU sample accuracy (higher = more accurate, more CPU)
maxmemory-samples 10
# Check memory usage
redis-cli INFO memory
# used_memory_human: 1.50G
# mem_fragmentation_ratio: 1.08 (ideal: 1.0-1.5)
# Check memory for a specific key
redis-cli MEMORY USAGE mykey SAMPLES 5
# Find big keys
redis-cli --bigkeys
Monitoring and Diagnostics
# Key metrics via INFO
redis-cli INFO memory # memory usage
redis-cli INFO stats # commands/sec, hits/misses
redis-cli INFO keyspace # key count per database
redis-cli INFO replication # replication status
redis-cli INFO clients # connected clients, blocked
# Cache hit rate (from INFO stats)
# keyspace_hits / (keyspace_hits + keyspace_misses)
# Slow log — commands taking > 10ms
redis-cli SLOWLOG GET 10
redis-cli CONFIG SET slowlog-log-slower-than 10000 # microseconds
# Latency monitoring
redis-cli LATENCY LATEST
redis-cli LATENCY HISTORY event
# Real-time command stream (dev/debug only!)
# redis-cli MONITOR # DO NOT run in production — huge performance impact
Linux System Tuning
# 1. Disable Transparent Huge Pages (causes latency spikes)
echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 2. Allow overcommit memory (required for fork/RDB)
sysctl vm.overcommit_memory=1
echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
# 3. Increase max connections
sysctl -w net.core.somaxconn=65535
# 4. Add to /etc/rc.local for persistence
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
# Redis will warn about these in startup log if not set
Production Checklist
- Set
requirepass with a strong password (or use ACLs) - Bind to private IP only — never
0.0.0.0 in production - Enable TLS for in-transit encryption
- Disable
FLUSHALL, FLUSHDB, and DEBUG with rename-command - Set
maxmemory to 70% of RAM and choose an eviction policy - Enable persistence (hybrid AOF recommended)
- Set up Sentinel or Cluster for HA
- Monitor hit rate, memory, and slow log continuously
- Disable Transparent Huge Pages and set
vm.overcommit_memory=1 on Linux - Never run
MONITOR or KEYS * in production
Warning: Never run MONITOR in production — it logs every command and significantly degrades performance. Use SLOWLOG GET for identifying slow commands instead.
Frequently Asked Questions
What is a good maxmemory setting?
Set maxmemory to 70-80% of available RAM to leave headroom for fork() during RDB saves, OS page cache, and OS buffers. With 8GB RAM, set maxmemory to 5-6GB.
How do I find which keys are using the most memory?
Run redis-cli --bigkeys (scans keyspace, reports largest per type). For a specific key: MEMORY USAGE key SAMPLES 5. For the whole instance: INFO memory shows used_memory and fragmentation.
Should I use Redis AUTH or ACLs?
Redis 6+ ACLs are more powerful: they allow per-user command and key-pattern restrictions. For new deployments, prefer ACLs over the single global requirepass password. Use requirepass for compatibility with older Redis versions.