Cache: Redis & Memcached
Quick Reference: Write Policy | Replacement Policy | Step 2: In-Memory Databases
Quick Reference
| Feature | Redis | Memcached |
|---|---|---|
| Data Types | Strings, lists, sets, hashes, sorted sets | Key-value only |
| Persistence | Optional (RDB, AOF) | None (volatile) |
| Replication | Master-slave | None |
| Pub/Sub | Yes | No |
| Lua Scripting | Yes | No |
| Performance | ~100K ops/sec | ~200K ops/sec |
| Use Case | Complex caching needs | Simple caching |
Clear Definition
Redis and Memcached are in-memory caching systems that store data in RAM for extremely fast access. Redis is feature-rich with persistence options, while Memcached is simple and focused on pure caching performance.
š” Key Insight: Use Redis for complex caching needs (data structures, persistence). Use Memcached for simple, high-performance caching.
Core Concepts
Redis Features
- Data Structures: Rich data types beyond key-value
- Persistence: Optional RDB snapshots and AOF logging
- Replication: Master-slave replication for high availability
- Pub/Sub: Publish-subscribe messaging
- Transactions: Multi-command transactions
- Lua Scripting: Server-side scripting
Memcached Features
- Simplicity: Key-value store only
- Performance: Extremely fast, low overhead
- Volatility: No persistence, data lost on restart
- Distributed: Client-side sharding
Use Cases
Redis Use Cases
- Complex Caching: Session storage, user profiles
- Real-time Features: Leaderboards, counters
- Pub/Sub: Real-time notifications
- Distributed Locks: Coordination in distributed systems
Memcached Use Cases
- Simple Caching: HTML fragments, API responses
- Session Storage: Simple session data
- Database Query Caching: Cache expensive queries
Best Practices
- Choose Right Tool: Redis for complex needs, Memcached for simple
- Set TTLs: Always set expiration times
- Monitor Memory: Track memory usage and eviction
- Handle Failures: Have fallback to database
Interview Tips
šÆ Interview Focus: Know when to use Redis vs Memcached, cache patterns.
Quick Reference Summary
Redis: Feature-rich caching with persistence, replication, and data structures. Use for complex caching needs.
Memcached: Simple, fast key-value cache. Use for high-performance, simple caching.
Next Topic: Write Policy ā
Back to: Step 4 Overview | Main Index