Overview
The official website address of Redis configuration file:/topics/config
GitHub address:/redis/redis/blob/unstable/
This article is mainly explained based on the configuration file of Redis 6. version, and other versions can also be used as a reference.
Redis configuration instructions
Basic configuration
# Binded IP address (default binding 127.0.0.1, only local access)# It is recommended to bind the specific IP or 0.0.0.0 in the production environment (need to be accompanied by a firewall/password)bind 0.0.0.0 # Listen to port (default 6379)port 6379 # Whether to run in daemon mode (default no), it is recommended to use yes in the production environmentdaemonize yes # PID file pathpidfile /var/run/redis_6379.pid # Log level (debug|verbose|Notice|warning, default notice)loglevel notice # Log file path (set to stdout output to console, default is empty)logfile "/var/log/redis/" # Number of databases (0-15, default 16)databases 16 # Whether to enable protected mode (default yes, only local access or password access is allowed)protected-mode yes # Maximum number of client connections (default 10000, set to 0 to indicate no limit)maxclients 10000
Memory management
# Maximum memory limit (such as 2gb, 512mb, default 0 means no limit, must be set in the production environment)maxmemory 0 # Memory elimination strategy (default noeviction)# - noeviction: prohibit writing, return an error# - allkeys-lru: Remove the least recently used key# - allkeys-random: randomly remove keys# - volatile-lru: Remove the LRU key with expiration time set# - volatile-random: Randomly remove key with expiration time set# - volatile-ttl: Remove the key that is about to expire# - volatile-lfu: Remove the least frequently used key with expiration time# - allkeys-lfu: Remove the least frequently used keysmaxmemory-policy noeviction # Approximate accuracy of LRU/LFU algorithm (the larger the value, the more accurate it is, the default is 50)maxmemory-samples 50 # Whether to enable automatic memory defragmentation (default no)activedefrag no # Minimum memory fragmentation triggered by defragmentation (default 100mb)active-defrag-ignore-bytes 100mb # Minimum fragmentation rate triggered by defragmentation (default 10%)active-defrag-threshold-lower 10 # Maximum fragmentation rate triggered by defragmentation (default 100%)active-defrag-threshold-upper 100 # The lower limit of CPU time used for defragmentation (default 1%)active-defrag-cycle-min 1 # The maximum CPU time limit for defragmentation (default 25%, avoid affecting normal services)active-defrag-cycle-max 25
Persistent configuration
RDB snapshot
# Conditions for automatically generating RDB snapshots (save <seconds> <changes>)# Multiple rules can be set, and any condition will be triggeredsave 900 1 # At least 1 key is modified within 900 secondssave 300 10 # At least 10 keys are modified within 300 secondssave 60 10000 # At least 10,000 keys are modified within 60 seconds# Disable RDB snapshots (comment all save lines)# save "" # RDB file name (default)dbfilename # RDB file storage path (must be a directory)dir ./ # Whether the RDB file is compressed (default yes, compression may affect performance)rdbcompression yes # Whether to add CRC64 checksum to RDB file (default yes, sacrificing about 10% performance for data integrity)rdb-checksum yes # Whether to use incremental fsync when generating RDB in the background (default yes, 4.0+ support)rdb-save-incremental-fsync yes
AOF configuration
# Whether to enable AOF persistence (default no)appendonly no # AOF file name (default)appendfilename "" # AOF synchronization policy (default everysec)# - always: sync to disk every write operation (slowest but safest)# - everysec: Synchronize once per second (taking into account both performance and security)# - no: It is up to the operating system to decide when to sync (fastest but potentially lose data)appendfsync everysec # Whether to pause fsync during AOF rewrite (default no, avoid data loss)no-appendfsync-on-rewrite no # Minimum size for automatic rewrite of AOF files (default 64mb)auto-aof-rewrite-min-size 64mb # The growth rate of automatic rewriting of AOF files (default 100%, that is, the current file is triggered when 2 times the last rewrite)auto-aof-rewrite-percentage 100 # Whether to ignore the last possible incomplete command when loading the AOF file (default yes)aof-load-truncated yes # Whether to use RDB format prefix when rewriting AOF (reduce file size, default yes)aof-use-rdb-preamble yes
Master-slave copy
# Set the current Redis as the slave and specify the master node (master IP and port)replicaof <masterip> <masterport> # Master Node Password (if any)masterauth <password> # Is the slave node read-only (default yes)replica-read-only yes # The network timeout time of master-slave replication (seconds, default 60)repl-timeout 60 # Master-slave replicate heartbeat frequency (seconds, default 10)repl-ping-replica-period 10 # Copy buffer size (used to save write commands that the master node is not synchronized to the slave node, default 1mb)repl-backlog-size 1mb # The persistence time of the copy buffer (seconds, memory will be released after this time and no node connection is available, default 3600)repl-backlog-ttl 3600 # Slave node priority (the smaller the value, the higher the priority, 0 means not participating in the election, default 100)replica-priority 100 # When the master node is not available, will the slave node continue to serve the read request (default yes)replica-serve-stale-data yes # Whether to use diskless replication (the master node sends RDB directly through the network to avoid disk I/O, default no)repl-diskless-sync no #Diskless replication delay time (seconds, wait for more slave nodes to be connected and transmitted together, default 5)repl-diskless-sync-delay 5
Security configuration
# Set the access password (it is recommended to use complex passwords, which must be set in the production environment)requirepass foobared # Whether to enable ACL (access control list, replace traditional password, default no)aclfile /etc/redis/ # Rename dangerous commands (for example, change FLUSHALL to a safe name, or disable the command)# rename-command FLUSHALL "" # Disable commands# rename-command FLUSHALL "FLUSHDB" # Rename command(Not recommended)
Network and Connectivity
# Backlog queue length of TCP connection (default 511, affecting burst connection processing capabilities in a short time)tcp-backlog 511 # TCP connection keep alive time (seconds, default 300)tcp-keepalive 300 # Whether to disable TCP_NODELAY (default no, enable Nagle algorithm to reduce the number of network packets, but may increase latency)tcp-nodelay no # Client idle timeout time (units of seconds, default 0 means no timeout)timeout 0
Slow query log
# Slow query threshold (microseconds, default 10000=10ms)slowlog-log-slower-than 10000 # Maximum length of slow query log (default 128)slowlog-max-len 128
I/O threads
# Whether to enable I/O multi-threading (default no, 4.0+ support, only accelerate network I/O, command execution is still single-threaded)io-threads-do-reads no # Number of I/O threads (it is recommended to set to half of the number of CPU cores, no more than 8, default 4)io-threads 4
Cluster configuration (Redis Cluster)
# Whether to enable cluster mode (default no), when set to yes, the Redis instance will run as the cluster node, participating in cluster management and data distribution.cluster-enabled no # Cluster configuration file (automatically generated, no manual editing required)cluster-config-file # Node timeout time (milliseconds, default 15000)cluster-node-timeout 15000 # The minimum length of replication from a node during failover (seconds, default 10)cluster-replica-validity-factor 10 # Does the cluster require all slots to be allocated (default yes to avoid split brain)cluster-require-full-coverage yes # Can slave nodes be migrated to other master nodes (default yes)cluster-migration-barrier 1
Publish Subscription
# The upper limit of client connections to publish subscriptions (the default is not limited, setting it to 0 means no limit)pubsub-max-connections 0 # Upper limit of message queue length for publish subscription (default 32, prevent memory overflow)client-output-buffer-limit pubsub 32mb 8mb 60
Monitoring and statistics
# Whether to record memory allocation information (default no, recommended to turn off in production environment)memtier yes # Whether to record memory fragmentation information (default yes)meminfo-command yes # Whether to record the detailed information of the memory allocator (default no)malloc-stats no
Other configurations
# Whether to check AOF files at startup (default yes)aof-checksum yes # Whether to enable Lua scripting function (default yes)lua-time-limit 5000 # Maximum execution time of Lua script (milliseconds, default 5000)# Whether to enable progressive rehash of hash tables (default yes, improve performance)activerehashing yes # Whether to enable large page memory (default no, enabling may cause delay problems)transparent-hugepage yes # Whether to enable client tracing (default no, used to debug client request sources)client-tracking no # Redirect mode for client tracking (default OFF)# - ON: Track all clients# - REDIRECT: Redirect trace information to the specified clientclient-tracking-redirection "" # Whether to enable delay monitoring (default no)latency-monitor-threshold 0 # Set to 0 means disable. Set to a specific value (such as 10) to monitor operations exceeding this value.# The history length of delayed monitoring (default 1024)latency-history-length 1024
Profile best practices
- Set maxmemory and a reasonable maxmemory-policy (such as allkeys-lru).
- Enable AOF persistence (appendonly yes) and set appendfsync everysec.
- Configure requirepass and disable dangerous commands (such as FLUSHALL).
- For read-write intensive scenarios, enable I/O threads (io-threads).
- Adjust the activedefrag parameter according to memory fragmentation.
- Disable unnecessary features (such as Lua scripts, publish subscriptions) to reduce resource consumption.
- Disable protected-mode and restrict access through bind and firewall.
- Use ACL to replace traditional password authentication (Redis 6.0+).
- Regularly back up RDB or AOF files to external storage.
This is all about this article about the detailed explanation of Redis configuration files. For more detailed explanation of Redis configuration files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!