What is Redis?
Redis is an in-memory data store used as a cache, session store, message broker, and more. It is extremely fast because all data lives in RAM, with optional persistence to disk.
Installation
sudo apt update
sudo apt install -y redis-serverConfiguration
Edit /etc/redis/redis.conf:
# Bind to localhost only (security)
bind 127.0.0.1 ::1
# Set a password
requirepass your_redis_password
# Set max memory (e.g., 256 MB)
maxmemory 256mb
maxmemory-policy allkeys-lrusudo systemctl restart redis-serverBasic Operations
# Connect
redis-cli -a your_redis_password
# String operations
SET mykey "hello"
GET mykey
SET session:abc123 "user_data" EX 3600 # Expires in 1 hour
# Check memory usage
INFO memory
# Monitor commands in real time
MONITORCommon Use Cases
- Page caching — cache rendered HTML pages
- Session storage — faster than file-based sessions
- Rate limiting — track request counts per IP
- Queue — LPUSH/BRPOP for simple job queues
Memory Policies
allkeys-lru— evict least recently used keys (best for caching)volatile-lru— evict LRU keys that have an expiry setnoeviction— return errors when memory is full