Redis: Redis Configuration
Redis configuration is flexible and powerful. This lesson covers the configuration file and common options.
1. Config File Location
Different installation methods place the config file in different locations:
| Installation Method | Config File Location |
|---|---|
| apt (Ubuntu) | /etc/redis/redis.conf |
| yum (CentOS) | /etc/redis.conf |
| Source | Source directory redis.conf, must copy manually |
| Homebrew (Mac) | /usr/local/etc/redis.conf |
| Windows | Install directory redis.windows.conf |
(1) Creating Config File After Source Installation
BASH
# Create config directory
sudo mkdir -p /etc/redis
# Copy config file
sudo cp /tmp/redis-7.0.11/redis.conf /etc/redis/redis.conf
# Create data directory
sudo mkdir -p /var/lib/redis
sudo chown redis:redis /var/lib/redis
2. Config File Format
Redis config files use a simple key-value format:
CONF
# This is a comment
# Basic format
config-key value
# Example
port 6379
bind 127.0.0.1
daemonize yes
💡 Note: Config keys and values are separated by a space — no equals sign needed.
3. Core Config Options
(1) Network Configuration
CONF
# Bind IP address
# 127.0.0.1 — local access only
# 0.0.0.0 — allow all IPs (dangerous!)
# 192.168.1.100 — bind to a specific IP
bind 127.0.0.1
# Listen port
port 6379
# Protected mode (recommended to keep on)
# yes: only allow local connections unless bind or password is set
# no: allow remote connections
protected-mode yes
# TCP connection backlog size
tcp-backlog 511
# Client idle timeout in seconds (0 means never disconnect)
timeout 0
# TCP keepalive interval in seconds
tcp-keepalive 300
⚠️ Security warning:
bind 0.0.0.0 allows access from all IPs — you must set a password!
(2) General Configuration
CONF
# Run as daemon (background)
daemonize no
# PID file location
pidfile /var/run/redis/redis-server.pid
# Log level
# debug: lots of debug info
# verbose: lots of useful info
# notice: moderately verbose (recommended for production)
# warning: only important messages
loglevel notice
# Log file location
# Empty string means output to stdout
logfile /var/log/redis/redis-server.log
# Number of databases (default 16, numbered 0-15)
databases 16
(3) Memory Management
CONF
# Maximum memory usage
# Format: 1kb, 5mb, 2gb, etc.
# 0 means unlimited (dangerous!)
maxmemory 256mb
# Eviction policy when memory limit is reached
# volatile-lru: evict keys with TTL set (LRU algorithm)
# allkeys-lru: evict any key (LRU algorithm)
# volatile-lfu: evict keys with TTL set (LFU algorithm)
# allkeys-lfu: evict any key (LFU algorithm)
# volatile-random: randomly evict keys with TTL set
# allkeys-random: randomly evict any key
# volatile-ttl: evict keys with the shortest TTL
# noeviction: don't evict, return error (default)
maxmemory-policy allkeys-lru
# LRU/LFU sample size
# Higher values are more accurate but use more CPU
maxmemory-samples 5
💡 How to choose an eviction policy?
- Caching scenarios:
allkeys-lruorallkeys-lfu - Persistence scenarios:
noevictionorvolatile-lru
(4) Persistence Configuration (RDB)
CONF
# RDB snapshot trigger conditions
# save <seconds> <changes>
# At least 1 change in 900 seconds → trigger snapshot
save 900 1
save 300 10
save 60 10000
# Disable RDB (comment out save or set to empty)
# save ""
# RDB file name
dbfilename dump.rdb
# RDB file storage directory
dir /var/lib/redis
# RDB compression (recommended to keep on)
rdbcompression yes
# RDB checksum (recommended to keep on)
rdbchecksum yes
(5) Persistence Configuration (AOF)
CONF
# Enable AOF
appendonly no
# AOF file name
appendfilename "appendonly.aof"
# AOF sync strategy
# always: sync on every write (safest, slowest)
# everysec: sync once per second (recommended)
# no: let the OS decide (fastest, least safe)
appendfsync everysec
# Disable fsync during AOF rewrite
no-appendfsync-on-rewrite no
# AOF file size triggers rewrite
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
💡 RDB vs AOF:
- RDB: snapshot, small files, fast recovery, but may lose data
- AOF: log, safer data, but larger files, slower recovery
- Recommendation: enable both RDB and AOF
(6) Security Configuration
CONF
# Set password (before Redis 6.0)
requirepass yourpassword
# Rename dangerous commands
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
rename-command DEBUG ""
⚠️ Note: Redis 6.0+ recommends using ACL for more flexible and secure access control.
(7) Limits Configuration
CONF
# Maximum number of client connections
# 10000 means up to 10000 clients
maxclients 10000
# Maximum input buffer
client-query-buffer-limit 1gb
# Maximum output buffer
# normal: regular clients
# replica: replica nodes
# pubsub: pub/sub clients
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
4. Using the CONFIG Command
You can view and modify configuration at runtime.
(1) View Configuration
REDIS
# View all config
CONFIG GET *
# Returns alternating list of config keys and values
# View specific config
CONFIG GET port
1) "port"
2) "6379"
CONFIG GET maxmemory
1) "maxmemory"
2) "0"
# View multiple configs (using wildcard)
CONFIG GET maxmemory*
1) "maxmemory"
2) "0"
3) "maxmemory-policy"
4) "noeviction"
5) "maxmemory-samples"
6) "5"
(2) Modify Configuration
REDIS
# Set max memory
CONFIG SET maxmemory 256mb
OK
# Set log level
CONFIG SET loglevel "debug"
OK
# Set number of databases
CONFIG SET databases 32
OK
⚠️ Note: Changes made with CONFIG SET take effect immediately but are lost on restart. Use CONFIG REWRITE to save to the config file.
(3) Save Configuration to File
REDIS
# Write current config to the config file
CONFIG REWRITE
OK
CONFIG REWRITE will:
- Read the original config file
- Overwrite with current runtime config
- Preserve comments and formatting
(4) Reset Configuration
REDIS
# Reset statistics
CONFIG RESETSTAT
OK
5. Common Configuration Scenarios
(1) Scenario 1: Development Environment
CONF
bind 127.0.0.1
port 6379
daemonize no
logfile ""
databases 16
save ""
appendonly no
(2) Scenario 2: Production Environment
CONF
bind 127.0.0.1
port 6379
daemonize yes
logfile /var/log/redis/redis.log
databases 16
# Memory management
maxmemory 2gb
maxmemory-policy allkeys-lru
# Persistence
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
# Security
requirepass your_strong_password
rename-command FLUSHALL ""
rename-command FLUSHDB ""
(3) Scenario 3: Cache Server Configuration
CONF
bind 127.0.0.1
port 6379
daemonize yes
maxmemory 4gb
maxmemory-policy allkeys-lru
# No persistence needed for caching
save ""
appendonly no
(4) Scenario 4: Message Queue Configuration
CONF
bind 127.0.0.1
port 6379
daemonize yes
maxmemory 1gb
maxmemory-policy noeviction
# Message queues need persistence
appendonly yes
appendfsync everysec
6. Using Command-Line Arguments
You can override config values when starting Redis:
BASH
# Override a single config
redis-server --port 6380
redis-server --maxmemory 512mb
redis-server --requirepass mypassword
# Override multiple configs
redis-server --port 6380 --maxmemory 512mb --daemonize yes
# Use config file + override some settings
redis-server /etc/redis/redis.conf --port 6380
7. Dynamic Configuration Adjustment
(1) Adjusting Memory Limits
REDIS
# Check current memory usage
INFO memory
used_memory:1048576
used_memory_human:1.00M
# Adjust max memory
CONFIG SET maxmemory 512mb
OK
# Save config
CONFIG REWRITE
OK
(2) Adjusting Log Level
REDIS
# Temporarily enable debug logging
CONFIG SET loglevel debug
OK
# Restore after debugging
CONFIG SET loglevel notice
OK
(3) Adjusting Slow Query Log
REDIS
# Set slow query threshold (microseconds)
CONFIG SET slowlog-log-slower-than 10000
OK
# Set max slow query log entries
CONFIG SET slowlog-max-len 128
OK
# View slow query log
SLOWLOG GET 10
8. Configuration Verification
(1) Check Config File Syntax
BASH
# Check at startup
redis-server /etc/redis/redis.conf --test-memory 100
(2) View Running Configuration
BASH
# View from command line
redis-cli CONFIG GET maxmemory
# View all config
redis-cli CONFIG GET *
▶ Example
View and modify runtime configuration:
BASH
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
127.0.0.1:6379> CONFIG SET maxmemory 256mb
OK
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "268435456"
❓ FAQ
Q Do I need to restart Redis after changing configuration?
A Config changes via CONFIG SET take effect immediately without restart. However, they are lost on restart — use CONFIG REWRITE to persist them.
Q How do I view Redis default configuration?
A The
redis.conf file in the Redis source distribution has detailed default config with explanations.Q What happens if maxmemory is set to 0?
A 0 means unlimited memory. Redis will use as much memory as available, which may lead to OOM and the system killing the process.
Q How do I disable dangerous commands?
A Use
rename-command FLUSHALL "" in the config file to rename a command to an empty string.Q Can a config file include other config files?
A Yes, use
include /path/to/other.conf.📖 Summary
- Config file location varies by installation method; source installs need manual copying
- Core config areas: network, memory, persistence, security
- CONFIG GET/SET views and modifies runtime config
- CONFIG REWRITE saves config to file
- Different scenarios need different config strategies
- Always set passwords and memory limits in production
📝 Exercises
- View config: Use CONFIG GET to check your Redis config, find the values of maxmemory and maxmemory-policy
- Modify config: Use CONFIG SET to change maxmemory to 128mb, then save with CONFIG REWRITE
- Scenario config: Write a Redis config file for a caching scenario
- Disable commands: Disable the FLUSHALL and FLUSHDB commands in the config file
9. Next Lesson
In the next lesson, we will learn Redis Data Types Overview, covering the 5 basic data types in Redis.