Cache: Redis & Memcached

Quick Reference: Write Policy | Replacement Policy | Step 2: In-Memory Databases


Quick Reference

FeatureRedisMemcached
Data TypesStrings, lists, sets, hashes, sorted setsKey-value only
PersistenceOptional (RDB, AOF)None (volatile)
ReplicationMaster-slaveNone
Pub/SubYesNo
Lua ScriptingYesNo
Performance~100K ops/sec~200K ops/sec
Use CaseComplex caching needsSimple 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

  1. Complex Caching: Session storage, user profiles
  2. Real-time Features: Leaderboards, counters
  3. Pub/Sub: Real-time notifications
  4. Distributed Locks: Coordination in distributed systems

Memcached Use Cases

  1. Simple Caching: HTML fragments, API responses
  2. Session Storage: Simple session data
  3. Database Query Caching: Cache expensive queries

Best Practices

  1. Choose Right Tool: Redis for complex needs, Memcached for simple
  2. Set TTLs: Always set expiration times
  3. Monitor Memory: Track memory usage and eviction
  4. 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