Installing and Configuring Redis

1. Installing Redis Server

PlatformCommandNotes
macOSbrew install redisInstalls Redis 7.4+ stable
Ubuntu/Debiansudo apt install redis-serverAdd official PPA for latest version
RHEL/CentOSsudo dnf install redisEnable EPEL repository first
Dockerdocker run -d -p 6379:6379 redis:7.4-alpineRecommended for development
From Sourcemake && make installAlways latest features NEW
Redis Stackdocker run -p 6379:6379 redis/redis-stackIncludes JSON, Search, Bloom, TimeSeries

2. Starting Redis Server

CommandDescription
redis-serverStart with default config on port 6379
redis-server /etc/redis/redis.confStart with custom config file
redis-server --port 6380 --daemonize yesOverride config via CLI flags
systemctl start redisStart via systemd on Linux
brew services start redisStart as background service on macOS

3. Connecting with Redis CLI

FlagPurposeExample
-hHostredis-cli -h 192.168.1.10
-pPortredis-cli -p 6380
-aPasswordredis-cli -a secret
-nDatabase indexredis-cli -n 2
--tlsTLS connectionredis-cli --tls --cert client.crt
-xRead last arg from stdinecho "data" | redis-cli -x SET key
--scanIterate keysredis-cli --scan --pattern "user:*"

4. Configuring redis.conf File

DirectiveDefaultPurpose
bind127.0.0.1 -::1Listen interfaces
port6379TCP listening port
protected-modeyesBlock external connections without auth
requirepassAuthentication password
maxmemory0Memory limit (e.g. 2gb)
maxmemory-policynoevictionEviction algorithm
save3600 1 300 100RDB snapshot trigger
appendonlynoEnable AOF persistence
databases16Number 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
CommandEffect
CONFIG SET key valueApply immediately (volatile)
CONFIG RESETSTATReset INFO counters

6. Getting Configuration Values

Example: Inspect runtime configuration

CONFIG GET maxmemory
CONFIG GET max*
CONFIG GET *timeout*
CONFIG GET save
PatternReturns
CONFIG GET nameSingle key/value pair
CONFIG GET pattern*All matching directives
CONFIG GET *Full configuration dump

7. Persisting Runtime Configuration

CommandDescription
CONFIG REWRITEWrite 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

LevelDescription
debugVerbose, dev only
verboseRarely useful, but not noisy
noticeProduction default
warningOnly critical messages
nothingDisable logging NEW
CONFIG SET loglevel notice

9. Configuring Database Count

DirectiveDefaultNotes
databases16DBs indexed 0..N-1; not supported in Cluster mode

10. Understanding Redis Data Model

TypeUse CaseBig-O
StringCache, counter, sessionO(1)
ListQueue, timelineO(1) push/pop
HashObject representationO(1) per field
SetUnique members, taggingO(1) add/remove
Sorted SetLeaderboard, priority queueO(log N)
BitmapFlags, presenceO(1) per bit
HyperLogLogCardinality estimationO(1) add
StreamEvent log, message busO(log N) add
GeoLocation queriesO(log N)
JSON NEWDocument storeO(path)