Running and Securing Redis
Running and Securing Redis
docker run -d --name cache \
-v redisData:/data \
-p 6379:6379 \
redis:7-alpine \
redis-server --requirepass strongpass --appendonly yes
protected-mode exists precisely for this shape of deployment: with no password set, Redis refuses connections from outside the local interface. Exposing 6379 without --requirepass gives anyone a writable key-value store holding your sessions — the most common container compromise pattern. Health checks must authenticate: redis-cli -a strongpass ping returns PONG only when auth succeeded.
Persistence Choices
Persistence Choices
Redis offers two mechanisms, and combining them is normal:
| Mode | How | Tradeoff |
|---|---|---|
| RDB | fork-based snapshots every N seconds | fast restarts, loses the window since last snapshot |
| AOF | replays append-only write log (appendfsync everysec) | up to ~1s loss, larger files |
redis-server --appendonly yes --save 900 1 --save 60 100
Snapshots and the AOF live under /data, so the named volume from the first example is what makes either one survive a restart. For a pure cache you can set --save "" and skip AOF entirely — the data is rebuildable, and disk I/O on every write is wasted.
Caching Pitfalls and Interview Angles
Caching Pitfalls and Interview Angles
Unbounded memory. Without maxmemory, Redis grows until the container OOMs. Set a ceiling and an eviction policy:
redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru
allkeys-lru is right for a cache (any key may go); noeviction is right when Redis is the source of truth and writes must fail loudly instead of silently dropping data.
Sessions vs cache. Session stores need AOF or replication — evicting a logged-in user is a different failure than losing a hot query result.
Interview: defend the eviction policy against the workload, explain RDB/AOF durability in seconds of possible loss, and describe how you would run Redis for HA (replica + Sentinel or Cluster).
Quiz
1. Which eviction policy fits a pure cache?
2. What is protected-mode for?
3. With AOF and appendfsync everysec, worst-case loss after a crash is about:
Flashcards
Question
--requirepass
Click to reveal answer
Answer
Sets the password; without it an exposed 6379 is an open writable store.
Question
RDB vs AOF
Click to reveal answer
Answer
RDB = periodic snapshots (faster restart, more loss); AOF = write log (≤~1s loss with everysec).
Question
maxmemory-policy
Click to reveal answer
Answer
What happens at the ceiling: allkeys-lru for caches, noeviction when data is authoritative.
Question
/data volume
Click to reveal answer
Answer
Where RDB and AOF files live — mount it or persistence disappears with the container.
Revision Notes
Key Takeaways
- 1. Authenticate before publishing the port, ever
- 2. Persistence only matters if /data is a volume
- 3. Pick the eviction policy from the data's role, not habit
Interview Tips
- • Classify the workload first (cache vs primary store), then justify policy and persistence
- • Describe HA options and what fails during a Sentinel failover
Cheat Sheet
redis:7 --requirepass + -v /data; AOF everysec for durability, save "" for pure cache; maxmemory + allkeys-lru; healthcheck with auth.