Skip to content

Redis

Installation

RHEL / CentOS

bash
sudo yum install epel-release
sudo yum install redis -y
sudo systemctl start redis.service
sudo systemctl enable redis

Check service status:

sh
sudo systemctl status redis.service
ps -f -u redis

Test the server:

sh
redis-cli ping

Debian / Ubuntu

sh
sudo apt-get update
sudo apt-get -y install redis-server
cd /etc/redis
sudo vi redis.conf
sudo service redis-server restart

Basic Configuration

sh
sudo vi /etc/redis.conf

Recommended changes:

  1. Change the bind address only if the server must accept remote traffic.
  2. Set a password if the instance is reachable outside localhost.
  3. 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.service

Test 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> quit

Heroku

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 --reload

iptables

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 DROP

Redis 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:latest

Compose 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: true

Production-ready recommendations

Recommended Redis settings:

txt
bind 127.0.0.1
protected-mode yes
appendonly yes
appendfsync everysec
tcp-keepalive 60

If Redis is used as a cache, set memory limits and an eviction policy:

txt
maxmemory 512mb
maxmemory-policy allkeys-lru

Operational checklist:

  • do not expose Redis directly to the public internet
  • keep backups if Redis stores durable data
  • require authentication when remote access is unavoidable