Core Concepts
Understanding Redis as a data structure server.
In-Memory Store
Redis stores all data in RAM for sub-millisecond read/write latency. It supports optional persistence to disk via RDB snapshots (point-in-time) and AOF logs (append-only file).
Single-Threaded Event Loop
Redis processes commands sequentially on a single thread using an event-driven I/O model. This eliminates lock contention and makes every operation atomic by default.
Persistence Options
RDB: Periodic snapshots — fast restarts, possible data loss. AOF: Logs every write — durable, larger files. RDB+AOF: Combine both for maximum durability with fast recovery.
Pub/Sub Messaging
Built-in publish/subscribe messaging where publishers send messages to channels without knowing who listens. Ideal for real-time notifications, chat systems, and event broadcasting.
TTL & Expiration
Every key can have a Time-To-Live (TTL) in seconds or milliseconds. Redis automatically evicts expired keys — essential for caching, sessions, and rate limiting.
Cluster & Sentinel
Redis Cluster: Automatic sharding across multiple nodes for horizontal scaling. Sentinel: High availability with automatic failover for master-replica setups.
Data Types & Commands
Redis is a "data structure server" — choosing the right type is critical for performance.
Setup Guide
Get Redis running locally in under a minute.
Docker (Recommended)
macOS
Ubuntu/Debian
Usage Examples
Practical patterns for caching, sessions, Pub/Sub, and rate limiting.
Best Practices
Set maxmemory
Always configure maxmemory and an eviction policy (allkeys-lru is recommended for caching). Without this, Redis will consume all available RAM and crash.
Key Naming Convention
Use a consistent pattern: entity:id:field (e.g., user:42:profile). This makes keys self-documenting and enables pattern scanning with SCAN.
Always Set TTL
Use EXPIRE or SETEX for cache data. Without TTLs, memory fills up with stale data. Review and clean orphaned keys regularly.
Avoid Giant Keys
Large hashes, lists, or sets can block the single-threaded event loop. Keep collections under 10,000 members. Use SCAN instead of KEYS * in production.
Use Pipelining
Batch multiple commands into a single network round-trip using pipelines. This dramatically reduces latency overhead for bulk operations.
Monitor with INFO
Regularly check INFO memory, INFO stats, and SLOWLOG GET to identify memory issues, hit/miss ratios, and slow commands. Use RedisInsight for visual monitoring.
Scaling & Production
Redis Cluster
Distribute data across 16,384 hash slots spread over 3+ master nodes. Each master has 1+ replicas for failover. Supports automatic resharding — add nodes and migrate slots with redis-cli --cluster reshard.
Sentinel HA
For simpler setups, use Redis Sentinel (3+ sentinel processes) for automatic master failover. Sentinels monitor the master, elect a new one on failure, and redirect clients. Good for single-shard HA without full Cluster complexity.
Read Replicas
Scale reads by adding replicas. Configure clients with READONLY mode to route GET operations to replicas. Replication is asynchronous — eventual consistency. Use WAIT command for synchronous replication when needed.
Memory Management
Set maxmemory and choose an eviction policy: allkeys-lru (cache), volatile-ttl (expire soonest first), or noeviction (queue). Monitor fragmentation ratio — restart or use MEMORY PURGE if >1.5.
Client-Side Caching
Redis 6+ supports server-assisted client-side caching via CLIENT TRACKING. The server invalidates client-local caches when keys change, reducing round trips by 10–100× for hot keys.
Scaling Benchmarks
Single Redis instance: 100K–300K ops/sec (GET/SET). A 6-node cluster (3 masters + 3 replicas) handles 1M+ ops/sec. Use pipelining (PIPELINE) to batch commands and reduce RTT overhead by 10×.
Frequently Asked Questions
Redis vs Memcached — when to use which?→
Should I use RDB or AOF persistence?→
appendfsync everysec). For production, use both RDB + AOF together.How do I handle cache invalidation?→
DEL when the source data changes. For complex scenarios, use Pub/Sub to broadcast invalidation events.