Installing Redis
You can run Redis in Docker for local dev, Homebrew on macOS, or managed services (ElastiCache, Azure Cache, Redis Cloud) in production.
Introduction
You can run Redis in Docker for local dev, Homebrew on macOS, or managed services (ElastiCache, Azure Cache, Redis Cloud) in production. The install path matters less than the configuration you apply on day one.
Default redis.conf allows unbounded memory growth, no password on many setups, and persistence settings tuned for laptops — not multi-GB production datasets.
This lesson covers a safe baseline: maxmemory, eviction policy, ACL, and kernel tuning before traffic hits the instance.
Understanding the topic
Key concepts
- redis-server reads redis.conf at startup; CONFIG SET changes runtime (not always durable).
- Docker is ideal for dev; bind mounts let you inject custom conf.
- Managed Redis offloads patching, failover, and backups.
- maxmemory + maxmemory-policy prevent OOM kills at the OS level.
- requirepass/ACL restrict who can run FLUSHALL and CONFIG.
- vm.overcommit_memory=1 on Linux reduces fork failures during BGSAVE.
flowchart LRDev[Docker local] --> Test[Integration tests]Staging[Compose cluster] --> CI[Pipeline]Prod[ElastiCache HA] --> App[Spring Boot pods]
Step-by-step explanation
- Package manager or container pulls the Redis binary.
- Configuration file sets port, memory limits, persistence, ACL.
- Server binds to interface (127.0.0.1 dev; private subnet prod).
- Clients connect on 6379 (or TLS wrapper port in managed services).
- Health checks use PING; metrics from INFO exported to Prometheus.
Syntax reference
Common commands
- Always set maxmemory in any shared environment.
- Use redis:7-alpine for smaller images in CI.
- Map volumes for RDB/AOF only when you need local persistence.
# Docker with memory limitdocker run -d --name redis \-p 6379:6379 \redis:7-alpine redis-server \--maxmemory 256mb \--maxmemory-policy allkeys-lru# Verifyredis-cli ping
Informative example
Production-style docker-compose snippet with persistence and password:
# docker-compose.yml excerptservices:redis:image: redis:7-alpinecommand: >redis-server--requirepass ${REDIS_PASSWORD}--maxmemory 512mb--maxmemory-policy volatile-lru--appendonly yesports: ["6379:6379"]
In real production prefer managed Redis with TLS, multi-AZ, and automated backups. Compose is for staging parity.
Real-world use
Real-world use cases
- Local dev stack with Docker Compose alongside PostgreSQL.
- CI pipeline spinning ephemeral Redis for integration tests.
- Staging cluster mirroring prod eviction and persistence settings.
- ElastiCache replication group for production microservices.
- On-prem Redis with Sentinel for regulated industries.
Best practices
- Set maxmemory to ~75–80% of instance RAM before go-live.
- Use ACL users per application with command restrictions.
- Enable TLS in transit for any cross-network access.
- Set vm.overcommit_memory=1 and disable THP on Linux.
- Pin Redis version in Docker tags — avoid :latest in prod.
- Document restore procedure from RDB/AOF or cloud snapshot.
Common mistakes
- Exposing 6379 to the public internet without auth.
- No maxmemory — Redis grows until Linux OOM killer strikes.
- Using default persistence on dev and assuming prod matches.
- Skipping kernel tuning then blaming Redis for fork failures.
Advanced interview questions
Q1BeginnerHow do you run Redis locally?
Q2BeginnerWhy set maxmemory?
Q3IntermediateDocker vs managed Redis in production?
Q4IntermediateWhat is vm.overcommit_memory?
Q5AdvancedDesign Redis deployment for a new microservice?
Summary
Install is easy; safe configuration is the hard part.