Installing and Configuring Redis
1. Installing Redis Server
| Platform | Command | Notes |
| macOS | brew install redis | Installs Redis 7.4+ stable |
| Ubuntu/Debian | sudo apt install redis-server | Add official PPA for latest version |
| RHEL/CentOS | sudo dnf install redis | Enable EPEL repository first |
| Docker | docker run -d -p 6379:6379 redis:7.4-alpine | Recommended for development |
| From Source | make && make install | Always latest features NEW |
| Redis Stack | docker run -p 6379:6379 redis/redis-stack | Includes JSON, Search, Bloom, TimeSeries |
2. Starting Redis Server
| Command | Description |
redis-server | Start with default config on port 6379 |
redis-server /etc/redis/redis.conf | Start with custom config file |
redis-server --port 6380 --daemonize yes | Override config via CLI flags |
systemctl start redis | Start via systemd on Linux |
brew services start redis | Start as background service on macOS |
3. Connecting with Redis CLI
| Flag | Purpose | Example |
-h | Host | redis-cli -h 192.168.1.10 |
-p | Port | redis-cli -p 6380 |
-a | Password | redis-cli -a secret |
-n | Database index | redis-cli -n 2 |
--tls | TLS connection | redis-cli --tls --cert client.crt |
-x | Read last arg from stdin | echo "data" | redis-cli -x SET key |
--scan | Iterate keys | redis-cli --scan --pattern "user:*" |
4. Configuring redis.conf File
| Directive | Default | Purpose |
bind | 127.0.0.1 -::1 | Listen interfaces |
port | 6379 | TCP listening port |
protected-mode | yes | Block external connections without auth |
requirepass | — | Authentication password |
maxmemory | 0 | Memory limit (e.g. 2gb) |
maxmemory-policy | noeviction | Eviction algorithm |
save | 3600 1 300 100 | RDB snapshot trigger |
appendonly | no | Enable AOF persistence |
databases | 16 | Number of logical databases |
5. Setting Configuration at Runtime
Example: Live config changes via CONFIG SET
CONFIG SET maxmemory 4gb
CONFIG SET maxmemory-policy allkeys-lru
CONFIG SET save "900 1 300 10 60 10000"
CONFIG SET timeout 300
| Command | Effect |
CONFIG SET key value | Apply immediately (volatile) |
CONFIG RESETSTAT | Reset INFO counters |
6. Getting Configuration Values
Example: Inspect runtime configuration
CONFIG GET maxmemory
CONFIG GET max*
CONFIG GET *timeout*
CONFIG GET save
| Pattern | Returns |
CONFIG GET name | Single key/value pair |
CONFIG GET pattern* | All matching directives |
CONFIG GET * | Full configuration dump |
7. Persisting Runtime Configuration
| Command | Description |
CONFIG REWRITE | Write current runtime config back to redis.conf, preserving comments |
Warning: CONFIG SET is volatile. Without CONFIG REWRITE changes are lost on restart.
8. Setting Server Verbosity
| Level | Description |
debug | Verbose, dev only |
verbose | Rarely useful, but not noisy |
notice | Production default |
warning | Only critical messages |
nothing | Disable logging NEW |
CONFIG SET loglevel notice
9. Configuring Database Count
| Directive | Default | Notes |
databases | 16 | DBs indexed 0..N-1; not supported in Cluster mode |
10. Understanding Redis Data Model
| Type | Use Case | Big-O |
| String | Cache, counter, session | O(1) |
| List | Queue, timeline | O(1) push/pop |
| Hash | Object representation | O(1) per field |
| Set | Unique members, tagging | O(1) add/remove |
| Sorted Set | Leaderboard, priority queue | O(log N) |
| Bitmap | Flags, presence | O(1) per bit |
| HyperLogLog | Cardinality estimation | O(1) add |
| Stream | Event log, message bus | O(log N) add |
| Geo | Location queries | O(log N) |
| JSON NEW | Document store | O(path) |