DeveloperBootstrap

Databases

Redis Enterprise Guide

Deploy Redis enterprise clusters with persistence, Redis Cluster, Sentinel, pub/sub patterns, and security hardening on Ubuntu 24.04 LTS.

What you'll learn

  • Install and configure Redis 7 for production
  • Choose and configure the right persistence mode (RDB vs AOF)
  • Set up Redis Sentinel for automatic failover
  • Configure Redis Cluster for horizontal scaling
  • Implement pub/sub and streams patterns
  • Secure Redis with authentication and TLS
  • Monitor with redis-cli and Prometheus exporter

Installation & Config

Bash
1# Install Redis 7 on Ubuntu 24.04
2sudo apt install -y redis-server
3sudo systemctl enable --now redis-server
4
5# Key redis.conf settings for production
6bind 127.0.0.1 ::1
7protected-mode yes
8requirepass "your_strong_password"
9maxmemory 2gb
10maxmemory-policy allkeys-lru
11tcp-keepalive 300
12timeout 300
13
14sudo systemctl restart redis-server
15redis-cli -a "your_strong_password" ping   # PONG

Persistence

INI
1# RDB snapshots — good for backups, some data loss on crash
2save 900 1     # save if >= 1 key changed in 900s
3save 300 10    # save if >= 10 keys changed in 300s
4save 60 10000  # save if >= 10000 keys changed in 60s
5dbfilename dump.rdb
6dir /var/lib/redis
7
8# AOF — every write logged, near-zero data loss
9appendonly yes
10appendfsync everysec   # balance: fsync every second
11
12# Hybrid (recommended for production)
13aof-use-rdb-preamble yes

Redis Sentinel

Bash
1# sentinel.conf (run on 3 servers)
2sentinel monitor mymaster 127.0.0.1 6379 2
3sentinel auth-pass mymaster yourpassword
4sentinel down-after-milliseconds mymaster 5000
5sentinel failover-timeout mymaster 60000
6sentinel parallel-syncs mymaster 1
7
8redis-sentinel /etc/redis/sentinel.conf
9# Or: redis-server /etc/redis/sentinel.conf --sentinel
10
11# Check sentinel state
12redis-cli -p 26379 sentinel masters

Redis Cluster

Bash
1# Enable cluster in redis.conf (on each of 6 nodes: 3 primary + 3 replica)
2cluster-enabled yes
3cluster-config-file nodes.conf
4cluster-node-timeout 5000
5
6# Create cluster (3 primary + 3 replicas)
7redis-cli --cluster create   node1:7000 node2:7001 node3:7002   node4:7003 node5:7004 node6:7005   --cluster-replicas 1
8
9# Check cluster info
10redis-cli -c -p 7000 cluster info
11redis-cli -c -p 7000 cluster nodes

Data Patterns

Bash
1# Session storage (hash)
2HSET session:abc123 userId 42 role admin expiresAt 1735689600
3EXPIRE session:abc123 3600
4
5# Rate limiting (sliding window)
6local key = "rate:" .. userId .. ":" .. math.floor(now/60)
7redis.call("INCR", key)
8redis.call("EXPIRE", key, 60)
9
10# Pub/Sub for real-time notifications
11SUBSCRIBE notifications:user:42   # subscriber
12PUBLISH notifications:user:42 '{"type":"lesson_complete","courseId":"123"}'
13
14# Sorted set leaderboard
15ZADD leaderboard 1500 "user:42"
16ZREVRANGE leaderboard 0 9 WITHSCORES   # top 10

Security

INI
1# ACL-based access control (Redis 6+)
2ACL SETUSER appuser on >strongpassword ~session:* ~cache:* +GET +SET +DEL +EXPIRE
3
4# Disable dangerous commands
5rename-command FLUSHDB ""
6rename-command FLUSHALL ""
7rename-command DEBUG ""
8rename-command CONFIG "CONFIG_ADMIN_ONLY_abc123"
9
10# TLS (Redis 6+)
11tls-port 6380
12tls-cert-file /etc/redis/tls/redis.crt
13tls-key-file /etc/redis/tls/redis.key
14tls-ca-cert-file /etc/redis/tls/ca.crt

Monitoring

Bash
1# Real-time stats
2redis-cli info stats | grep -E 'instantaneous|keyspace|evicted'
3
4# Memory usage
5redis-cli info memory | grep -E 'used_memory_human|mem_fragmentation'
6
7# Key metrics to alert on:
8# - memory usage > 80% of maxmemory
9# - evicted_keys > 0 (cache eviction pressure)
10# - connected_clients near maxclients
11# - keyspace_hits / (hits + misses) < 0.95 (cache hit ratio)
12
13# Prometheus redis_exporter
14docker run -d --name redis_exporter   -e REDIS_ADDR="redis://localhost:6379"   -e REDIS_PASSWORD="yourpassword"   -p 9121:9121 oliver006/redis_exporter