# Redis Configuration File for Global Gala
# This configuration provides persistence, memory limits, and multiple databases

################################## NETWORK #####################################

# Accept connections from any interface
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
tcp-keepalive 300
timeout 0

################################# GENERAL #####################################

# Run Redis in the foreground for Docker
daemonize no

# Set server verbosity to 'notice'
loglevel notice

# Set the number of databases (0-15)
# Database allocation for Global Gala:
# 0: Default/Session data
# 1: Cache data
# 2: Queue data
# 3: Real-time features (websockets, presence)
# 4: Rate limiting
# 5: Analytics/metrics
# 6-15: Reserved for future use
databases 16

################################ SNAPSHOTTING  ################################

# Save the DB on disk:
#   save <seconds> <changes>
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.

# Persistence configuration for production-grade reliability
save 900 1      # Save after 900 sec (15 min) if at least 1 key changed
save 300 10     # Save after 300 sec (5 min) if at least 10 keys changed
save 60 10000   # Save after 60 sec if at least 10000 keys changed

# Compress string objects using LZF when dump .rdb databases
rdbcompression yes
rdbchecksum yes

# The filename for the RDB file
dbfilename dump.rdb

# The working directory for RDB and AOF files
dir /data

################################# REPLICATION #################################

# Enable replica to serve stale data when disconnected from master
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

################################## SECURITY ###################################

# Require a password to access Redis (uncomment and set in production)
# requirepass your_redis_password_here

# Command renaming for security (uncomment in production)
# rename-command FLUSHDB ""
# rename-command FLUSHALL ""
# rename-command KEYS ""
# rename-command CONFIG ""

################################### CLIENTS ####################################

maxclients 10000

############################## MEMORY MANAGEMENT ###############################

# Set a memory usage limit
# For production, adjust based on your server capacity
# This example sets 256MB max memory
maxmemory 256mb

# Memory eviction policy when max memory is reached
# allkeys-lru: Remove any key according to the LRU algorithm
# Best for cache usage which is our primary use case
maxmemory-policy allkeys-lru

# LRU samples for eviction
maxmemory-samples 5

# Enable memory defragmentation
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

############################# LAZY FREEING ####################################

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no

############################ KERNEL OOM CONTROL ##############################

oom-score-adj no
oom-score-adj-values 0 200 800

############################ APPEND ONLY MODE #################################

# AOF (Append Only File) for better durability
# Enable for production environments requiring high durability
appendonly yes

# The name of the append only file
appendfilename "appendonly.aof"

# Fsync modes:
# everysec: fsync every second (good balance between performance and durability)
appendfsync everysec

# Prevent fsync during rewrites
no-appendfsync-on-rewrite no

# Auto rewrite the AOF file
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

# Load truncated AOF file on startup
aof-load-truncated yes

# Use RDB-AOF hybrid persistence
aof-use-rdb-preamble yes

################################ LUA SCRIPTING  ###############################

lua-time-limit 5000

################################ REDIS CLUSTER  ###############################

# Cluster is disabled by default
# cluster-enabled no

################################## SLOW LOG ###################################

# Log queries slower than specified microseconds
slowlog-log-slower-than 10000

# Maximum length of the slow log
slowlog-max-len 128

################################ LATENCY MONITOR ##############################

# Enable latency monitoring (0 = disabled)
latency-monitor-threshold 0

############################# EVENT NOTIFICATION ##############################

# Enable keyspace notifications for expired keys (useful for session management)
notify-keyspace-events Ex

############################### ADVANCED CONFIG ###############################

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
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

# Disable some dangerous commands in production
# These should be uncommented in production
# disable-thp yes

################################## MODULES #####################################

# Load additional Redis modules if needed
# loadmodule /path/to/module.so

################################## MONITORING ##################################

# Enable Redis statistics
# Useful for monitoring tools
enable-module-command yes
enable-protected-configs no
enable-debug-command no