Module 1 - Data Storage

Redis vs Memcached

Two popular in-memory caching solutions. Which one should you choose?

1Quick Comparison

Simple Analogy
Memcached: A simple, blazing-fast key-value store. Like a hash table in memory. Does one thing extremely well.

Redis: A Swiss Army knife. Key-value plus lists, sets, sorted sets, pub/sub, persistence. More features, slightly more overhead.

2Feature Comparison

FeatureRedisMemcached
Data TypesStrings, lists, sets, hashes, streamsStrings only
PersistenceRDB, AOFNone (pure cache)
ReplicationBuilt-inNone
Pub/SubYesNo
ClusteringRedis ClusterClient-side sharding
Multi-threadingSingle (mostly)Multi-threaded
Memory EfficiencyGoodBetter for simple strings

3When to Use Each

Choose Redis

  • ✓ Need data structures (lists, sets, sorted sets)
  • ✓ Need persistence/durability
  • ✓ Need pub/sub messaging
  • ✓ Need replication/HA
  • ✓ Rate limiting, leaderboards, sessions

Choose Memcached

  • ✓ Simple key-value caching only
  • ✓ Need multi-threaded performance
  • ✓ Maximum memory efficiency for strings
  • ✓ Already have Memcached expertise
  • ✓ Pure cache, no persistence needed

4Performance Considerations

Memcached: Multi-threaded

Can use multiple CPU cores. Better for high-throughput simple caching on multi-core machines.

Redis: Single-threaded (mostly)

One main thread for commands. Still incredibly fast (100K+ ops/sec). I/O threads added in Redis 6.

Memory Overhead

Redis has more overhead per key due to data structure flexibility. Memcached is leaner for pure strings.

5Key Takeaways

1Redis is feature-rich: data structures, persistence, pub/sub, replication
2Memcached is simpler: pure cache, multi-threaded, memory efficient
3For simple caching only, Memcached may be slightly faster
4For anything beyond simple caching, Redis is the better choice
5Most projects choose Redis for its versatility and ecosystem

?Quiz

1. You need to cache database queries with automatic expiry. Which works?

2. You need a real-time leaderboard with rankings. Which should you use?