Configure Redis Cache Server #88

Open
opened 2025-09-14 08:14:26 +00:00 by tonym · 0 comments
Owner

Redis Cache Server Configuration

Overview

Redis is an in-memory data structure store used as a database, cache, and message broker. This configuration provides high-performance caching for applications like Immich, session storage, and general application caching needs.

Installation

From Community Applications

  1. Open AppsCommunity Applications
  2. Search for "Redis"
  3. Select "redis" by LinuxServer.io
  4. Click Install

Docker Configuration

Container Settings:

Repository: lscr.io/linuxserver/redis:latest
Network Type: Custom br0 (192.168.0.16)
Fixed IP Address: 192.168.0.16

Port Mappings:

Container Port: 6379 → Host Port: 6379 (TCP)

Volume Mappings:

Host Path: /mnt/cache/appdata/redis → Container Path: /config
Host Path: /mnt/cache/appdata/redis/data → Container Path: /data

Environment Variables:

PUID=99
PGID=100
TZ=America/Chicago
REDIS_PASSWORD=secure_redis_password_2024

Advanced Settings:

Privileged: No
Console shell command: Bash
Restart Policy: unless-stopped

Initial Setup

1. Redis Configuration

Create /mnt/cache/appdata/redis/redis.conf:

# Network Configuration
bind 0.0.0.0
port 6379
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300

# General Configuration
daemonize no
pidfile /data/redis.pid
loglevel notice
logfile /config/logs/redis.log
databases 16

# Security
requirepass secure_redis_password_2024

# Memory Management
maxmemory 512mb
maxmemory-policy allkeys-lru

# Persistence
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data

# AOF (Append Only File)
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes

# Slow Log
slowlog-log-slower-than 10000
slowlog-max-len 128

# Client Configuration
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

# Advanced
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes

2. First Run Configuration

# Connect to Redis container
docker exec -it redis bash

# Connect to Redis CLI
redis-cli -a secure_redis_password_2024

# Test connection
ping
# Should return PONG

# Set test key
set test "Hello Redis"
get test

# Check info
info server

exit

3. Create Database Assignments

# Database 0: Default/General Cache
# Database 1: Immich Cache
# Database 2: Session Storage
# Database 3: Application Cache
# Database 4: Temporary Data
# Database 5-15: Reserved for future use

Service Integration

Connection Strings for Applications

Immich:

REDIS_HOSTNAME=192.168.0.16
REDIS_PORT=6379
REDIS_PASSWORD=secure_redis_password_2024
REDIS_DBINDEX=1

General Application Cache:

REDIS_URL=redis://:secure_redis_password_2024@192.168.0.16:6379/0

Session Storage:

REDIS_HOST=192.168.0.16
REDIS_PORT=6379
REDIS_PASSWORD=secure_redis_password_2024
REDIS_DB=2

Performance Optimization

1. Memory Analysis

# Connect to Redis
redis-cli -a secure_redis_password_2024

# Check memory usage
info memory

# Analyze memory by key type
memory usage [key]

# Get memory stats
memory stats

# Check slow queries
slowlog get 10

2. Monitoring Commands

# Monitor real-time commands
monitor

# Check client connections
client list

# View configuration
config get '*'

# Check key statistics
info keyspace

Backup Integration

Kopia Backup Configuration

Add to Kopia backup policy:

# Redis data and configuration
/mnt/cache/appdata/redis/data/
/mnt/cache/appdata/redis/redis.conf
/mnt/cache/appdata/redis/logs/

Automated Redis Backups

Create backup script /mnt/cache/appdata/redis/backup.sh:

#!/bin/bash
BACKUP_DIR="/mnt/cache/appdata/redis/backups"
DATE=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR

# Force Redis to save current dataset
docker exec redis redis-cli -a secure_redis_password_2024 BGSAVE

# Wait for background save to complete
while [ $(docker exec redis redis-cli -a secure_redis_password_2024 LASTSAVE) -eq $(docker exec redis redis-cli -a secure_redis_password_2024 LASTSAVE) ]; do
    sleep 1
done

# Copy RDB file
cp /mnt/cache/appdata/redis/data/dump.rdb "$BACKUP_DIR/redis_dump_$DATE.rdb"

# Copy AOF file if it exists
if [ -f /mnt/cache/appdata/redis/data/appendonly.aof ]; then
    cp /mnt/cache/appdata/redis/data/appendonly.aof "$BACKUP_DIR/redis_aof_$DATE.aof"
fi

# Cleanup old backups (keep 7 days)
find $BACKUP_DIR -name "redis_*" -mtime +7 -delete

echo "Redis backup completed: $DATE"

Make executable: chmod +x /mnt/cache/appdata/redis/backup.sh

Add to cron: 0 3 * * * /mnt/cache/appdata/redis/backup.sh

Monitoring Setup

Uptime Kuma Integration

Monitor Name: Redis Cache Server
Monitor Type: Port
Hostname: 192.168.0.16
Port: 6379
Heartbeat Interval: 60 seconds
Retries: 3

Health Check Script

Create /mnt/cache/appdata/redis/health_check.sh:

#!/bin/bash
result=$(docker exec redis redis-cli -a secure_redis_password_2024 ping)
if [[ $result == "PONG" ]]; then
    echo "Redis is healthy"
    exit 0
else
    echo "Redis is not responding"
    exit 1
fi

Performance Monitoring Script

Create /mnt/cache/appdata/redis/monitor.sh:

#!/bin/bash
echo "=== Redis Performance Report ==="
echo "Date: $(date)"
echo ""

# Memory usage
echo "Memory Usage:"
docker exec redis redis-cli -a secure_redis_password_2024 info memory | grep used_memory_human

# Keyspace info
echo ""
echo "Keyspace:"
docker exec redis redis-cli -a secure_redis_password_2024 info keyspace

# Connected clients
echo ""
echo "Connected Clients:"
docker exec redis redis-cli -a secure_redis_password_2024 info clients | grep connected_clients

# Commands per second
echo ""
echo "Commands Stats:"
docker exec redis redis-cli -a secure_redis_password_2024 info stats | grep instantaneous_ops_per_sec

# Slow log
echo ""
echo "Recent Slow Queries:"
docker exec redis redis-cli -a secure_redis_password_2024 slowlog get 5

Testing Procedures

1. Basic Functionality Test

# Test connection
docker exec redis redis-cli -a secure_redis_password_2024 ping

# Test set/get
docker exec redis redis-cli -a secure_redis_password_2024 set test_key "test_value"
docker exec redis redis-cli -a secure_redis_password_2024 get test_key

# Test expiration
docker exec redis redis-cli -a secure_redis_password_2024 setex temp_key 10 "expires_in_10s"
docker exec redis redis-cli -a secure_redis_password_2024 ttl temp_key

2. Performance Test

# Benchmark Redis performance
docker exec redis redis-benchmark -h localhost -p 6379 -a secure_redis_password_2024 -t set,get -n 10000 -q

# Test with different data sizes
docker exec redis redis-benchmark -h localhost -p 6379 -a secure_redis_password_2024 -t set,get -n 1000 -d 1024 -q

3. Persistence Test

# Force save
docker exec redis redis-cli -a secure_redis_password_2024 BGSAVE

# Check last save time
docker exec redis redis-cli -a secure_redis_password_2024 LASTSAVE

# Restart container and verify data persistence
docker restart redis
sleep 5
docker exec redis redis-cli -a secure_redis_password_2024 get test_key

Troubleshooting

Common Issues

Connection Refused:

# Check container status
docker ps | grep redis

# Check logs
docker logs redis

# Restart container
docker restart redis

Memory Issues:

# Check memory usage
docker exec redis redis-cli -a secure_redis_password_2024 info memory

# Clear specific database
docker exec redis redis-cli -a secure_redis_password_2024 -n 0 FLUSHDB

# Clear all databases (DANGER!)
# docker exec redis redis-cli -a secure_redis_password_2024 FLUSHALL

Performance Issues:

# Check slow log
docker exec redis redis-cli -a secure_redis_password_2024 slowlog get 20

# Monitor commands in real-time
docker exec redis redis-cli -a secure_redis_password_2024 monitor

# Check client connections
docker exec redis redis-cli -a secure_redis_password_2024 client list

Persistence Issues:

# Check if saves are failing
docker exec redis redis-cli -a secure_redis_password_2024 info persistence

# Force background save
docker exec redis redis-cli -a secure_redis_password_2024 BGSAVE

# Check disk space
df -h /mnt/cache/appdata/redis/

Maintenance Tasks

Daily

  • Monitor memory usage and performance
  • Check backup completion
  • Review slow queries

Weekly

  • Analyze key patterns and usage
  • Clean up expired keys
  • Review client connections

Monthly

  • Update Redis version
  • Optimize configuration based on usage
  • Review and update backup retention
  • Performance tuning review

Quarterly

  • Full Redis maintenance
  • Configuration optimization
  • Security audit
  • Disaster recovery testing

Security Considerations

Authentication

  • Use strong Redis password
  • Regularly rotate password
  • Limit network access via firewall if needed

Network Security

  • Use fixed IP address for predictable access
  • Monitor for unauthorized connection attempts
  • Consider Redis AUTH command logging

Data Security

  • Regular encrypted backups
  • Monitor for sensitive data in cache
  • Implement key expiration policies

Advanced Configuration

Redis Sentinel (High Availability)

# For production environments, consider Redis Sentinel
# This provides automatic failover and monitoring

Redis Cluster (Scaling)

# For high-performance requirements, consider Redis Cluster
# This provides horizontal scaling across multiple nodes

Dependencies

  • Before Redis: None (base infrastructure)
  • After Redis: Immich, applications requiring caching

Notes

  • Redis is in-memory storage - plan for adequate RAM
  • Configure appropriate persistence based on use case
  • Monitor memory usage to prevent OOM conditions
  • Consider Redis modules for extended functionality
  • Regular monitoring is crucial for optimal performance

Configuration updated for Unraid 7.0.1 - NastyNAS Server

# Redis Cache Server Configuration ## Overview Redis is an in-memory data structure store used as a database, cache, and message broker. This configuration provides high-performance caching for applications like Immich, session storage, and general application caching needs. ## Installation ### From Community Applications 1. Open **Apps** → **Community Applications** 2. Search for **"Redis"** 3. Select **"redis"** by **LinuxServer.io** 4. Click **Install** ### Docker Configuration **Container Settings:** ``` Repository: lscr.io/linuxserver/redis:latest Network Type: Custom br0 (192.168.0.16) Fixed IP Address: 192.168.0.16 ``` **Port Mappings:** ``` Container Port: 6379 → Host Port: 6379 (TCP) ``` **Volume Mappings:** ``` Host Path: /mnt/cache/appdata/redis → Container Path: /config Host Path: /mnt/cache/appdata/redis/data → Container Path: /data ``` **Environment Variables:** ``` PUID=99 PGID=100 TZ=America/Chicago REDIS_PASSWORD=secure_redis_password_2024 ``` **Advanced Settings:** ``` Privileged: No Console shell command: Bash Restart Policy: unless-stopped ``` ## Initial Setup ### 1. Redis Configuration Create `/mnt/cache/appdata/redis/redis.conf`: ```ini # Network Configuration bind 0.0.0.0 port 6379 protected-mode yes tcp-backlog 511 timeout 0 tcp-keepalive 300 # General Configuration daemonize no pidfile /data/redis.pid loglevel notice logfile /config/logs/redis.log databases 16 # Security requirepass secure_redis_password_2024 # Memory Management maxmemory 512mb maxmemory-policy allkeys-lru # Persistence save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /data # AOF (Append Only File) appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes # Slow Log slowlog-log-slower-than 10000 slowlog-max-len 128 # Client Configuration client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 # Advanced hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes ``` ### 2. First Run Configuration ```bash # Connect to Redis container docker exec -it redis bash # Connect to Redis CLI redis-cli -a secure_redis_password_2024 # Test connection ping # Should return PONG # Set test key set test "Hello Redis" get test # Check info info server exit ``` ### 3. Create Database Assignments ```bash # Database 0: Default/General Cache # Database 1: Immich Cache # Database 2: Session Storage # Database 3: Application Cache # Database 4: Temporary Data # Database 5-15: Reserved for future use ``` ## Service Integration ### Connection Strings for Applications **Immich:** ``` REDIS_HOSTNAME=192.168.0.16 REDIS_PORT=6379 REDIS_PASSWORD=secure_redis_password_2024 REDIS_DBINDEX=1 ``` **General Application Cache:** ``` REDIS_URL=redis://:secure_redis_password_2024@192.168.0.16:6379/0 ``` **Session Storage:** ``` REDIS_HOST=192.168.0.16 REDIS_PORT=6379 REDIS_PASSWORD=secure_redis_password_2024 REDIS_DB=2 ``` ## Performance Optimization ### 1. Memory Analysis ```bash # Connect to Redis redis-cli -a secure_redis_password_2024 # Check memory usage info memory # Analyze memory by key type memory usage [key] # Get memory stats memory stats # Check slow queries slowlog get 10 ``` ### 2. Monitoring Commands ```bash # Monitor real-time commands monitor # Check client connections client list # View configuration config get '*' # Check key statistics info keyspace ``` ## Backup Integration ### Kopia Backup Configuration Add to Kopia backup policy: ```bash # Redis data and configuration /mnt/cache/appdata/redis/data/ /mnt/cache/appdata/redis/redis.conf /mnt/cache/appdata/redis/logs/ ``` ### Automated Redis Backups Create backup script `/mnt/cache/appdata/redis/backup.sh`: ```bash #!/bin/bash BACKUP_DIR="/mnt/cache/appdata/redis/backups" DATE=$(date +%Y%m%d_%H%M%S) mkdir -p $BACKUP_DIR # Force Redis to save current dataset docker exec redis redis-cli -a secure_redis_password_2024 BGSAVE # Wait for background save to complete while [ $(docker exec redis redis-cli -a secure_redis_password_2024 LASTSAVE) -eq $(docker exec redis redis-cli -a secure_redis_password_2024 LASTSAVE) ]; do sleep 1 done # Copy RDB file cp /mnt/cache/appdata/redis/data/dump.rdb "$BACKUP_DIR/redis_dump_$DATE.rdb" # Copy AOF file if it exists if [ -f /mnt/cache/appdata/redis/data/appendonly.aof ]; then cp /mnt/cache/appdata/redis/data/appendonly.aof "$BACKUP_DIR/redis_aof_$DATE.aof" fi # Cleanup old backups (keep 7 days) find $BACKUP_DIR -name "redis_*" -mtime +7 -delete echo "Redis backup completed: $DATE" ``` Make executable: `chmod +x /mnt/cache/appdata/redis/backup.sh` Add to cron: `0 3 * * * /mnt/cache/appdata/redis/backup.sh` ## Monitoring Setup ### Uptime Kuma Integration **Monitor Name:** Redis Cache Server **Monitor Type:** Port **Hostname:** 192.168.0.16 **Port:** 6379 **Heartbeat Interval:** 60 seconds **Retries:** 3 ### Health Check Script Create `/mnt/cache/appdata/redis/health_check.sh`: ```bash #!/bin/bash result=$(docker exec redis redis-cli -a secure_redis_password_2024 ping) if [[ $result == "PONG" ]]; then echo "Redis is healthy" exit 0 else echo "Redis is not responding" exit 1 fi ``` ### Performance Monitoring Script Create `/mnt/cache/appdata/redis/monitor.sh`: ```bash #!/bin/bash echo "=== Redis Performance Report ===" echo "Date: $(date)" echo "" # Memory usage echo "Memory Usage:" docker exec redis redis-cli -a secure_redis_password_2024 info memory | grep used_memory_human # Keyspace info echo "" echo "Keyspace:" docker exec redis redis-cli -a secure_redis_password_2024 info keyspace # Connected clients echo "" echo "Connected Clients:" docker exec redis redis-cli -a secure_redis_password_2024 info clients | grep connected_clients # Commands per second echo "" echo "Commands Stats:" docker exec redis redis-cli -a secure_redis_password_2024 info stats | grep instantaneous_ops_per_sec # Slow log echo "" echo "Recent Slow Queries:" docker exec redis redis-cli -a secure_redis_password_2024 slowlog get 5 ``` ## Testing Procedures ### 1. Basic Functionality Test ```bash # Test connection docker exec redis redis-cli -a secure_redis_password_2024 ping # Test set/get docker exec redis redis-cli -a secure_redis_password_2024 set test_key "test_value" docker exec redis redis-cli -a secure_redis_password_2024 get test_key # Test expiration docker exec redis redis-cli -a secure_redis_password_2024 setex temp_key 10 "expires_in_10s" docker exec redis redis-cli -a secure_redis_password_2024 ttl temp_key ``` ### 2. Performance Test ```bash # Benchmark Redis performance docker exec redis redis-benchmark -h localhost -p 6379 -a secure_redis_password_2024 -t set,get -n 10000 -q # Test with different data sizes docker exec redis redis-benchmark -h localhost -p 6379 -a secure_redis_password_2024 -t set,get -n 1000 -d 1024 -q ``` ### 3. Persistence Test ```bash # Force save docker exec redis redis-cli -a secure_redis_password_2024 BGSAVE # Check last save time docker exec redis redis-cli -a secure_redis_password_2024 LASTSAVE # Restart container and verify data persistence docker restart redis sleep 5 docker exec redis redis-cli -a secure_redis_password_2024 get test_key ``` ## Troubleshooting ### Common Issues **Connection Refused:** ```bash # Check container status docker ps | grep redis # Check logs docker logs redis # Restart container docker restart redis ``` **Memory Issues:** ```bash # Check memory usage docker exec redis redis-cli -a secure_redis_password_2024 info memory # Clear specific database docker exec redis redis-cli -a secure_redis_password_2024 -n 0 FLUSHDB # Clear all databases (DANGER!) # docker exec redis redis-cli -a secure_redis_password_2024 FLUSHALL ``` **Performance Issues:** ```bash # Check slow log docker exec redis redis-cli -a secure_redis_password_2024 slowlog get 20 # Monitor commands in real-time docker exec redis redis-cli -a secure_redis_password_2024 monitor # Check client connections docker exec redis redis-cli -a secure_redis_password_2024 client list ``` **Persistence Issues:** ```bash # Check if saves are failing docker exec redis redis-cli -a secure_redis_password_2024 info persistence # Force background save docker exec redis redis-cli -a secure_redis_password_2024 BGSAVE # Check disk space df -h /mnt/cache/appdata/redis/ ``` ## Maintenance Tasks ### Daily - Monitor memory usage and performance - Check backup completion - Review slow queries ### Weekly - Analyze key patterns and usage - Clean up expired keys - Review client connections ### Monthly - Update Redis version - Optimize configuration based on usage - Review and update backup retention - Performance tuning review ### Quarterly - Full Redis maintenance - Configuration optimization - Security audit - Disaster recovery testing ## Security Considerations ### Authentication - Use strong Redis password - Regularly rotate password - Limit network access via firewall if needed ### Network Security - Use fixed IP address for predictable access - Monitor for unauthorized connection attempts - Consider Redis AUTH command logging ### Data Security - Regular encrypted backups - Monitor for sensitive data in cache - Implement key expiration policies ## Advanced Configuration ### Redis Sentinel (High Availability) ```bash # For production environments, consider Redis Sentinel # This provides automatic failover and monitoring ``` ### Redis Cluster (Scaling) ```bash # For high-performance requirements, consider Redis Cluster # This provides horizontal scaling across multiple nodes ``` ## Dependencies - **Before Redis:** None (base infrastructure) - **After Redis:** Immich, applications requiring caching ## Notes - Redis is in-memory storage - plan for adequate RAM - Configure appropriate persistence based on use case - Monitor memory usage to prevent OOM conditions - Consider Redis modules for extended functionality - Regular monitoring is crucial for optimal performance --- *Configuration updated for Unraid 7.0.1 - NastyNAS Server*
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: tonym/NastyNAS#88
No description provided.