Redis
Installation
RHEL / CentOS
bash
sudo yum install epel-release
sudo yum install redis -y
sudo systemctl start redis.service
sudo systemctl enable redisCheck service status:
sh
sudo systemctl status redis.service
ps -f -u redisTest the server:
sh
redis-cli pingDebian / Ubuntu
sh
sudo apt-get update
sudo apt-get -y install redis-server
cd /etc/redis
sudo vi redis.conf
sudo service redis-server restartBasic Configuration
sh
sudo vi /etc/redis.confRecommended changes:
- Change the bind address only if the server must accept remote traffic.
- Set a password if the instance is reachable outside localhost.
- Enable keyspace notifications only when an application needs them.
txt
bind 0.0.0.0
requirepass <strong-password>
notify-keyspace-events "KA"Restart Redis:
sh
sudo systemctl restart redis.serviceTest authentication:
sh
$ redis-cli
127.0.0.1:6379> set key1 10
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth <password>
OK
127.0.0.1:6379> set key1 10
OK
127.0.0.1:6379> get key1
"10"
127.0.0.1:6379> quitHeroku
If you are using Heroku Redis and need keyspace notifications:
sh
heroku redis:keyspace-notifications -c KA --remote <remote-server>Firewall
firewall-cmd
bash
sudo firewall-cmd --permanent --new-zone=redis
sudo firewall-cmd --permanent --zone=redis --add-port=6379/tcp
# if required
sudo firewall-cmd --permanent --zone=redis --add-source=client_server_private_IP
sudo firewall-cmd --reloadiptables
bash
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp -s client_servers_private_IP/32 --dport 6379 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -P INPUT DROPRedis Stack in Docker
redis-stack.conf:
txt
port 6379
daemonize yes
notify-keyspace-events "KA"Run a container:
bash
sudo docker run -v ./data:/data -v ./conf/redis-stack.conf:/redis-stack.conf -p 6379:6379 -p 8001:8001 -d --name redis-server redis/redis-stack:latestCompose example:
yaml
services:
redis:
image: redis/redis-stack:latest
container_name: redis-server
restart: unless-stopped
deploy:
resources:
limits:
memory: 2G
logging:
options:
max-size: "10m"
max-file: "3"
networks:
app-network:
ipv4_address: 172.18.88.80
ports:
- "6379:6379"
- "8001:8001"
environment:
TZ: Asia/Kuala_Lumpur
volumes:
- ./data:/data
- ./conf/redis-stack.conf:/redis-stack.conf
networks:
app-network:
name: app-network
external: trueProduction-ready recommendations
Recommended Redis settings:
txt
bind 127.0.0.1
protected-mode yes
appendonly yes
appendfsync everysec
tcp-keepalive 60If Redis is used as a cache, set memory limits and an eviction policy:
txt
maxmemory 512mb
maxmemory-policy allkeys-lruOperational checklist:
- do not expose Redis directly to the public internet
- keep backups if Redis stores durable data
- require authentication when remote access is unavoidable