Redis Data Structures
Redis Data Structures
Redis provides multiple data structures beyond simple key-value strings.
Core Data Types
1. String:
SET user:123 '{"name":"John"}'
GET user:123
INCR counter
2. Hash:
HSET user:123 name 'John' age 30
HGET user:123 name
HGETALL user:123
3. List:
LPUSH queue task1 task2
RPOP queue
LRANGE queue 0 -1
4. Set:
SADD tags:post:1 'python' 'redis'
SMEMBERS tags:post:1
SINTER tags:post:1 tags:post:2
5. Sorted Set:
ZADD leaderboard 100 player1 200 player2
ZRANGE leaderboard 0 -1 WITHSCORES
ZRANGEBYSCORE leaderboard 150 250
Advanced Structures
6. HyperLogLog (Cardinality Estimation):
PFADD unique_users user1 user2
PFCOUNT unique_users
7. Bitmap:
SETBIT user:123:days 365 1
BITCOUNT user:123:days
8. Stream (Append-only log):
XADD mystream * field1 value1
XREAD COUNT 10 STREAMS mystream 0
9. Geospatial:
GEOADD locations 13.361389 38.115556 'Palermo'
GEODIST locations 'Palermo' 'Catania' km
Use Cases by Data Type
| Type | Use Case | Example |
|---|---|---|
| String | Simple caching, counters | Session tokens, page views |
| Hash | Object storage | User profiles, product details |
| List | Queues, recent items | Message queues, activity feeds |
| Set | Tags, unique items | User interests, unique visitors |
| Sorted Set | Leaderboards, rankings | Game scores, priority queues |
| Stream | Event sourcing | Audit logs, activity streams |
Redis Persistence
Redis Persistence
RDB (Redis Database Backup)
RDB Persistence:
Snapshot-based:
- Fork child process
- Write dataset to disk
- Point-in-time snapshots
Configuration:
# redis.conf
save 900 1 # Save if 1 key changed in 900 seconds
save 300 10 # Save if 10 keys changed in 300 seconds
save 60 10000 # Save if 10000 keys changed in 60 seconds
Pros:
- Compact single file
- Faster restart
- Good for backups
Cons:
- May lose data between snapshots
- Fork can cause latency spikes
AOF (Append-Only File)
AOF Persistence:
Log-based:
- Append every write operation
- Rewrite log periodically
- More durable than RDB
Configuration:
# redis.conf
appendonly yes
appendfsync everysec # fsync every second
appendonly always # fsync every write (slowest, safest)
aof-appendfsync no # let OS decide (fastest, least safe)
Pros:
- Better durability
- Easy to understand
- Can replay to exact state
Cons:
- Larger files
- Slower restart
- More CPU overhead
Hybrid Approach
Redis 4.0+ RDB + AOF:
- Use RDB for fast restarts
- Use AOF for durability
- AOF rewritten with RDB base
Configuration:
aof-use-rdb-preamble yes
Persistence Trade-offs
| Metric | RDB | AOF (everysec) | AOF (always) |
|---|---|---|---|
| Durability | Medium | High | Highest |
| Performance | Best | Good | Worst |
| File Size | Small | Large | Large |
| Restart Speed | Fast | Slow | Slow |
Redis vs Memcached
Redis vs Memcached
Feature Comparison
| Feature | Redis | Memcached |
|---|---|---|
| Data Structures | Strings, Hashes, Lists, Sets, Sorted Sets | Strings only |
| Persistence | RDB, AOF, Hybrid | None |
| Replication | Master-Slave, Sentinel | None built-in |
| Clustering | Built-in | Client-side |
| Lua Scripts | Yes | No |
| Transactions | Yes (MULTI/EXEC) | No |
| Pub/Sub | Yes | No |
| Memory Efficiency | Higher overhead | Lower overhead |
| Threading | Single-threaded | Multi-threaded |
| Max Value Size | 512 MB | 1 MB |
Performance
Benchmark Comparison (simple GET/SET):
Memcached: ~200,000 ops/sec per core
Redis: ~100,000 ops/sec single-threaded
Redis Cluster: Horizontal scaling
Memcached: Multi-threaded vertical scaling
When to Use Which
Use Redis when:
- Need complex data structures
- Require persistence
- Need pub/sub or transactions
- Want built-in replication/clustering
- Use cases: leaderboards, queues, session stores
**Use Memcached when:
- Simple key-value caching only
- Need maximum throughput
- Memory efficiency is critical
- Use cases: HTML fragments, database query results
Migration Considerations
# Redis can emulate Memcached
class RedisMemcachedCompat:
def __init__(self, redis_client):
self.redis = redis_client
def get(self, key):
return self.redis.get(key)
def set(self, key, value, ttl=0):
if ttl > 0:
self.redis.setex(key, ttl, value)
else:
self.redis.set(key, value)
def delete(self, key):
self.redis.delete(key)
Redis Ecosystem
Redis Modules:
- RediSearch: Full-text search
- RedisJSON: JSON support
- RedisGraph: Graph queries
- RedisTimeSeries: Time series data
- RedisBloom: Bloom filters
Redis Tools:
- Redis Sentinel: High availability
- Redis Cluster: Horizontal scaling
- RedisInsight: GUI monitoring
Practice Problems
Design a scalable Redis system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale Redis to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for Redis and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. Which Redis data structure is best for a leaderboard?
2. What does RDB persistence do in Redis?
3. What is a key advantage of Redis over Memcached?
4. Which Redis persistence option provides the best durability?
5. What is Redis Sentinel used for?
Flashcards
Question
Name Redis's 5 core data types
Click to reveal answer
Answer
1) String, 2) Hash, 3) List, 4) Set, 5) Sorted Set
Question
RDB vs AOF persistence?
Click to reveal answer
Answer
RDB: snapshots, faster restart, may lose data. AOF: log of writes, better durability, slower restart.
Question
Redis vs Memcached: Key difference?
Click to reveal answer
Answer
Redis: complex data structures, persistence, replication. Memcached: simple strings only, multi-threaded, faster for basic ops.
Question
What is Redis Sentinel?
Click to reveal answer
Answer
High availability solution providing monitoring, automatic failover, and configuration provider for Redis instances
Question
Which Redis structure for leaderboards?
Click to reveal answer
Answer
Sorted Set (ZADD, ZRANGE) - maintains elements ordered by score with O(log N) operations
Revision Notes
Key Takeaways
- 1.Redis supports 5+ data structures beyond simple strings
- 2.RDB for snapshots, AOF for durability - choose based on needs
- 3.Redis excels at complex data structures; Memcached for simple KV
- 4.Redis Sentinel for HA, Redis Cluster for horizontal scaling
- 5.Single-threaded but very fast; use pipelining for batch ops
Interview Tips
- •Know which data structure to use for each use case
- •Explain RDB vs AOF trade-offs clearly
- •Compare Redis vs Memcached - when to choose each
- •Mention Redis Cluster for scaling beyond single instance
Cheat Sheet
Cheat Sheet: Redis
Data Structures
- String: Simple KV, counters
- Hash: Object storage
- List: Queues, recent items
- Set: Tags, unique items
- Sorted Set: Leaderboards
Persistence
- RDB: Snapshots, fast restart
- AOF: Write log, better durability
- Hybrid: RDB base + AOF
Redis vs Memcached
- Redis: Rich structures, persistence, clustering
- Memcached: Simple, multi-threaded, faster for basic ops
Tools
- Sentinel: High availability
- Cluster: Horizontal scaling
- Modules: Search, JSON, Graph