# ==============================================================================
# REDIS CONFIGURATION FOR ADTLAS
# ==============================================================================
# 
# Optimized Redis configuration for the Adtlas TV advertising platform.
# This configuration is tuned for use as both a cache and message broker
# for Celery task queue in a containerized Django environment.
#
# Configuration areas covered:
# - Memory management and optimization
# - Persistence and durability
# - Security settings
# - Performance tuning
# - Monitoring and logging
#
# Author: Adtlas Development Team
# Version: 1.0.0
# Redis Version: 7.x
# Last Updated: 2025-07-08
# ==============================================================================

# ==============================================================================
# NETWORK AND CONNECTION SETTINGS
# ==============================================================================

# Bind to all interfaces (safe in Docker container)
bind 0.0.0.0

# Default port (will be mapped by Docker)
port 6379

# Connection settings
tcp-keepalive 60
timeout 300
tcp-backlog 511

# Client connection limits
maxclients 1000

# ==============================================================================
# MEMORY MANAGEMENT
# ==============================================================================

# Maximum memory usage (512MB for container)
maxmemory 512mb

# Memory eviction policy for cache usage
# allkeys-lru: Evict least recently used keys when memory limit is reached
maxmemory-policy allkeys-lru

# Memory sampling for LRU algorithm
maxmemory-samples 5

# ==============================================================================
# PERSISTENCE CONFIGURATION
# ==============================================================================

# Enable AOF (Append Only File) for better durability
appendonly yes
appendfilename "appendonly.aof"

# AOF fsync policy for balance between performance and durability
# everysec: fsync every second (good balance)
appendfsync everysec

# Disable RDB snapshots (using AOF for persistence)
save ""

# AOF rewrite settings
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

# ==============================================================================
# SECURITY SETTINGS
# ==============================================================================

# Disable dangerous commands in production
# rename-command FLUSHDB ""
# rename-command FLUSHALL ""
# rename-command KEYS ""
# rename-command CONFIG ""
# rename-command SHUTDOWN SHUTDOWN_SAFE

# Protected mode (will be off in Docker network)
protected-mode no

# ==============================================================================
# PERFORMANCE OPTIMIZATION
# ==============================================================================

# Hash table settings for memory efficiency
hash-max-ziplist-entries 512
hash-max-ziplist-value 64

# List compression settings
list-max-ziplist-size -2
list-compress-depth 0

# Set compression settings
set-max-intset-entries 512

# Sorted set compression settings
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

# HyperLogLog settings
hll-sparse-max-bytes 3000

# Stream settings
stream-node-max-bytes 4096
stream-node-max-entries 100

# ==============================================================================
# LOGGING AND MONITORING
# ==============================================================================

# Log level (notice is good for production)
loglevel notice

# Log to stdout (captured by Docker)
logfile ""

# Database count
databases 16

# ==============================================================================
# CELERY-SPECIFIC SETTINGS
# ==============================================================================

# Settings optimized for Celery message broker usage

# Keyspace notifications for Celery monitoring
notify-keyspace-events Ex

# Lazy freeing for better performance with large data structures
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes

# ==============================================================================
# CLIENT MANAGEMENT
# ==============================================================================

# Client timeout settings
timeout 300

# Client output buffer limits
# Normal clients
client-output-buffer-limit normal 0 0 0

# Replica clients
client-output-buffer-limit replica 256mb 64mb 60

# Pub/sub clients (used by Celery)
client-output-buffer-limit pubsub 32mb 8mb 60

# ==============================================================================
# ADVANCED SETTINGS
# ==============================================================================

# Threading and I/O
# I/O threads for networking (Redis 6+)
io-threads 2
io-threads-do-reads yes

# Enable active rehashing for better memory distribution
activerehashing yes

# Client query buffer settings
client-query-buffer-limit 1gb

# Protocol buffer settings
proto-max-bulk-len 512mb

# ==============================================================================
# MODULE SETTINGS
# ==============================================================================

# Load modules if needed (uncomment as required)
# loadmodule /path/to/redisearch.so
# loadmodule /path/to/redisjson.so

# ==============================================================================
# SLOW LOG CONFIGURATION
# ==============================================================================

# Log slow queries for monitoring
# Log queries slower than 10ms
slowlog-log-slower-than 10000
slowlog-max-len 128

# ==============================================================================
# LATENCY MONITORING
# ==============================================================================

# Enable latency monitoring
latency-monitor-threshold 100

# ==============================================================================
# MEMORY USAGE OPTIMIZATION
# ==============================================================================

# Active defragmentation (Redis 4+)
activedefrag yes
active-defrag-ignore-bytes 100mb
active-defrag-threshold-lower 10
active-defrag-threshold-upper 100
active-defrag-cycle-min 5
active-defrag-cycle-max 75

# ==============================================================================
# DEVELOPMENT AND DEBUGGING
# ==============================================================================

# Enable Lua script debugging (disable in production)
# lua-debugger no

# ==============================================================================
# CLUSTER SETTINGS (for future scaling)
# ==============================================================================

# Cluster mode settings (disabled for single instance)
# cluster-enabled no
# cluster-config-file nodes.conf
# cluster-node-timeout 15000

# ==============================================================================
# NOTES FOR PRODUCTION DEPLOYMENT
# ==============================================================================
#
# For production deployment, consider:
# 1. Enable authentication with requirepass
# 2. Configure SSL/TLS if needed
# 3. Set up Redis Sentinel for high availability
# 4. Implement Redis Cluster for horizontal scaling
# 5. Monitor memory usage and adjust maxmemory
# 6. Enable dangerous command renaming
# 7. Set up proper backup strategy for AOF files
# 8. Configure log rotation
# 9. Monitor slow queries and optimize
# 10. Set up monitoring with Redis Exporter for Prometheus
#
# ==============================================================================
