Redis Deep Dive
The Swiss Army knife of in-memory data stores. Far more than just a cache.
1What is Redis?
Redis (Remote Dictionary Server): An in-memory data structure store used as a database, cache, message broker, and queue. Single-threaded, incredibly fast (100K+ ops/sec).
2Data Structures
Strings
Basic key-value. Store text, numbers, JSON, binary.
SET user:1 'john'Lists
Ordered collection. Use for queues, timelines.
LPUSH queue:emails 'msg1'Sets
Unique unordered items. Tags, unique visitors.
SADD tags:post:1 'redis' 'cache'Sorted Sets
Unique items with scores. Leaderboards, rankings.
ZADD leaderboard 100 'alice'Hashes
Field-value pairs. Store objects.
HSET user:1 name 'john' age 30Streams
Append-only log. Event sourcing, messaging.
XADD events * type 'click'3Common Use Cases
Caching
Cache database queries, API responses, sessions
Session Store
Store user sessions with automatic expiry
Rate Limiting
Track request counts per user/IP with TTL
Leaderboards
Sorted sets for real-time rankings
Pub/Sub
Real-time messaging between services
Distributed Locks
SETNX for mutual exclusion
Queues
Lists as simple job queues
Real-time Analytics
HyperLogLog for unique counts
4Persistence Options
RDB (Snapshotting)
Point-in-time snapshots at intervals. Fast restart, but may lose recent data.
AOF (Append-Only File)
Log every write operation. Better durability, slower startup.
RDB + AOF
Combine both for durability and fast restarts.
5Clustering & HA
Redis Sentinel
Monitoring, failover, and notification. For high availability.
Redis Cluster
Automatic sharding across nodes. Horizontal scaling.
Replication
Master-replica for read scaling and failover.
Managed Options
AWS ElastiCache, Redis Cloud, Azure Cache
6Key Takeaways
?Quiz
1. Best Redis data structure for a real-time leaderboard?
2. Redis is single-threaded. How is it still so fast?