# ==============================================================================
# REDIS CONFIGURATION FOR STREAM PROCESSOR
# ==============================================================================
# 
# Optimized Redis configuration for the Stream Processor application.
# This configuration is tuned for Celery task queue, Django caching,
# session management, and real-time stream processing operations.
#
# Configuration areas covered:
# - Memory management and eviction policies
# - Persistence and durability
# - Network and connection settings
# - Security configuration
# - Performance optimization
# - Celery-specific optimizations
#
# Author: Stream Processor Development Team
# Version: 1.0.0
# Redis Version: 7.x
# Last Updated: 2025-01-27
# ==============================================================================

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

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

# Connection settings optimized for Django + Celery
timeout 300
tcp-keepalive 60
tcp-backlog 511

# Client connection limits
maxclients 1000

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

# Maximum memory usage (adjust based on container limits)
maxmemory 512mb

# Memory eviction policy for cache data
# allkeys-lru: Evict any key using LRU when memory limit is reached
# Suitable for mixed cache and task queue usage
maxmemory-policy allkeys-lru

# Memory sampling for LRU eviction
maxmemory-samples 5

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

# RDB Snapshots - Balanced approach for task queue persistence
# Save snapshot if at least 1 key changed in 900 seconds
# Save snapshot if at least 10 keys changed in 300 seconds
# Save snapshot if at least 10000 keys changed in 60 seconds
save 900 1
save 300 10
save 60 10000

# RDB file settings
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data

# Stop writes if RDB snapshot fails (important for task queue)
stop-writes-on-bgsave-error yes

# AOF (Append Only File) - Disabled for performance
# Enable if you need stronger durability guarantees
appendonly no
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
aof-use-rdb-preamble yes

# ==============================================================================
# LOGGING CONFIGURATION
# ==============================================================================

# Log level: debug, verbose, notice, warning
loglevel notice

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

# Syslog settings (disabled)
syslog-enabled no

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

# Database count (default 16 is sufficient)
databases 16

# Hash table optimization
hash-max-ziplist-entries 512
hash-max-ziplist-value 64

# List optimization
list-max-ziplist-size -2
list-compress-depth 0

# Set optimization
set-max-intset-entries 512

# Sorted set optimization
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

# HyperLogLog optimization
hll-sparse-max-bytes 3000

# Stream optimization (for real-time data)
stream-node-max-bytes 4096
stream-node-max-entries 100

# ==============================================================================
# CELERY OPTIMIZATIONS
# ==============================================================================

# Lazy freeing for better performance with large objects
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
replica-lazy-flush yes

# Key expiration optimization
# Note: active-expire-cycle-lookups-per-loop removed (not available in Redis 7.x)

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

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

# Protected mode (disabled in Docker environment)
protected-mode no

# Authentication (configure if needed)
# requirepass your_redis_password

# ==============================================================================
# CLIENT BEHAVIOR
# ==============================================================================

# Client output buffer limits
# Format: client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

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

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

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

# Replication settings (currently single instance)
# replicaof <masterip> <masterport>
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-ping-replica-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 1mb
repl-backlog-ttl 3600

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

# Slow log settings for performance monitoring
slowlog-log-slower-than 10000
slowlog-max-len 128

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

# Latency monitoring threshold (microseconds)
latency-monitor-threshold 100

# ==============================================================================
# ADVANCED CONFIGURATION
# ==============================================================================

# Hash function for cluster (not used in single instance)
# cluster-require-full-coverage yes

# Lua scripting
lua-time-limit 5000

# Notify keyspace events (useful for monitoring)
# K - Keyspace events
# E - Keyevent events
# g - Generic commands (DEL, EXPIRE, RENAME, ...)
# $ - String commands
# l - List commands
# s - Set commands
# h - Hash commands
# z - Sorted set commands
# x - Expired events
# e - Evicted events
# A - Alias for g$lshzxe
notify-keyspace-events ""

# ==============================================================================
# STREAM PROCESSOR SPECIFIC SETTINGS
# ==============================================================================

# Optimizations for Django session storage
# Sessions typically have TTL, so optimize for that
# Note: active-expire-cycle-lookups-per-loop removed (not available in Redis 7.x)

# Optimizations for Celery task results
# Task results are typically short-lived
active-defrag-ignore-bytes 100mb
active-defrag-threshold-lower 10
active-defrag-threshold-upper 100
active-defrag-cycle-min 1
active-defrag-cycle-max 25

# Memory usage optimization for task queues
# Note: jemalloc-bg-thread removed (not available in Redis 7.x)

# ==============================================================================
# MONITORING AND STATS
# ==============================================================================

# Enable stats collection
# Note: info-stats-enabled removed (not available in Redis 7.x)

# ==============================================================================
# DOCKER SPECIFIC SETTINGS
# ==============================================================================

# Disable background saving fork optimization in containers
rdb-save-incremental-fsync yes

# Optimize for container environment
stop-writes-on-bgsave-error yes

# ==============================================================================
# CELERY TASK QUEUE OPTIMIZATIONS
# ==============================================================================

# Optimize for task queue patterns:
# 1. Frequent LPUSH/RPOP operations (task queuing)
# 2. SET/GET operations with TTL (task results)
# 3. Hash operations (task metadata)
# 4. Pub/Sub for task events

# List optimization for task queues
list-max-ziplist-size -2
list-compress-depth 0

# Hash optimization for task metadata
hash-max-ziplist-entries 512
hash-max-ziplist-value 64

# ==============================================================================
# PRODUCTION DEPLOYMENT NOTES
# ==============================================================================
#
# For production deployment, consider:
# 1. Enable authentication with requirepass
# 2. Configure SSL/TLS if needed
# 3. Set up Redis Sentinel for high availability
# 4. Monitor memory usage and adjust maxmemory
# 5. Enable AOF if stronger durability is required
# 6. Configure proper backup strategy for RDB files
# 7. Monitor slow queries and optimize accordingly
# 8. Set up monitoring with Redis metrics
# 9. Consider Redis Cluster for horizontal scaling
# 10. Tune eviction policy based on workload patterns
# 11. Configure proper log rotation
# 12. Set up alerts for memory usage and connection limits
#
# Celery-specific considerations:
# - Monitor task queue lengths
# - Set appropriate task result expiration
# - Monitor failed task patterns
# - Configure dead letter queues if needed
# - Optimize serialization format (pickle vs json)
#
# Django caching considerations:
# - Monitor cache hit rates
# - Set appropriate cache timeouts
# - Use cache versioning for deployments
# - Monitor session storage usage
#
# ==============================================================================