Redis vs Memcached (Caching Focus)¶
1. Decision¶
Choose Memcached as a pure caching solution for high-throughput, multi-threaded, simple key-value string workloads; choose Redis when caching requires complex data structures (Lists, Sets, Hashes, Sorted Sets), high availability failover (Sentinel), replication, persistence, or atomic Lua scripts.
2. 30-Second Answer¶
When evaluating Redis vs Memcached strictly as application cache layers, both deliver sub-millisecond in-memory read/write performance. Memcached excels at high-concurrency multi-threaded string caching on large multi-core servers, utilizing fixed-size Slab Allocation to eliminate OS memory fragmentation. Redis operates on a single-threaded event loop per process, but provides advanced caching features: server-side data structure manipulation (HSET, ZADD), atomic operations, key eviction policies (LRU, LFU), persistence (RDB/AOF), primary-replica replication, and automated high availability (Redis Sentinel/Cluster). For simple string caching at scale, Memcached is lighter; for rich dynamic application state caching, Redis is superior.
3. Decision Matrix¶
| Option | Best For | Avoid When | Latency | Scalability | Consistency | Operational Complexity |
|---|---|---|---|---|---|---|
| Memcached | Simple key-value string/blob caching, multi-threaded CPU scale-up, static HTML fragments | Complex data structure caching, data persistence, automated failover requirements | Sub-millisecond (<1ms) | Vertical multi-threaded scale-up; Horizontal via client-side consistent hashing | Ephemeral; No built-in replication across nodes | Very Low |
| Redis | Rich data structures, session state, rate limiting, leaderboards, HA replication, Sentinel | Pure simple string caching on multi-core single instances without multi-process clustering | Sub-millisecond (<1ms) | Vertical RAM; Horizontal via Redis Cluster (Hash Slots) | Primary-Replica async replication; Configurable persistence | Medium |
4. Selection Rules¶
| Requirement | Best Choice | Rationale / Example |
|---|---|---|
| Multi-Core Single Machine CPU Saturation | Memcached | Multi-threaded memory architecture processes >1M QPS per node natively |
| Cache Invalidation by Hash Fields | Redis | Use Redis Hashes (HSET/HDEL) to invalidate individual fields without clearing entire object |
| Slab Allocation Memory Efficiency | Memcached | Fixed-size memory slabs prevent OS memory fragmentation under high key churn |
| Sorted Leaderboards & Top-K Caching | Redis | Sorted Sets (ZADD, ZREVRANGE) execute rank caching in \(O(\log N)\) complexity |
| Automated Cache Node Failover (HA) | Redis | Redis Sentinel promotes replicas to primary automatically upon node death |
| Atomic Cache Increments with Expiration | Redis | INCR + EXPIRE combined in single atomic operation or Lua script |
5. Tradeoffs¶
- Multi-Threaded Hardware Utilization vs Single-Threaded Event Loop
- Gain (Memcached): Scales linearly across 32+ CPU cores on a single large cache instance.
- Lose (Memcached): Lacks rich server-side computation or data structures.
- Data Structure Versatility vs Pure String Caching
- Gain (Redis): Update nested cached object attributes (
HSET) without re-serializing entire JSON string. - Lose (Redis): Consumes higher memory overhead per key due to
redisObjectmetadata headers. - Slab Allocator Determinism vs
jemallocDynamic Allocation - Gain (Memcached): Zero OS memory fragmentation during continuous write/delete churn.
- Lose (Memcached): Wastes memory if cached payload sizes don't align closely with slab class sizes.
- Replication & Persistence Capabilities vs Ephemeral Memory Only
- Gain (Redis): Replicas prevent cold-cache database crashes during primary node restarts.
- Lose (Redis): Managing replication lag, split-brain Sentinel elections, and cluster hash slot rebalancing.
- Client Routing Simplicity vs Server-Managed Hash Slots
- Gain (Memcached): Client libraries (e.g., Ketama consistent hashing) route requests without server cluster gossip.
- Lose (Memcached): Client configuration drift causes cache key routing mismatches.
6. Interview Counter Questions¶
- How do eviction policies differ between Memcached (Slab LRU) and Redis (Global LRU / LFU)?
- How does Redis version 6.0+ multi-threaded I/O impact the performance gap with Memcached?
- Why does updating a nested JSON string in Memcached cause high network and CPU serialization overhead?
- How do you implement Cache Warming in Redis vs Memcached after a cluster reboot?
- What is the impact of Redis single-threaded blocking commands (
KEYS *,FLUSHALL) in a production cache cluster? - How does Memcached handle memory allocation via Slab Classes and what is Slab Calcification?
- Why is Redis preferred over Memcached for distributed rate limiting and sliding window counters?
- How does Redis Sentinel detect node failure and perform automatic master election?
- When would you deploy a proxy layer (Twemproxy / Mcrouter / Envoy) in front of Redis or Memcached?
- How do you handle cache memory fragmentation in Redis without restarting the process?
7. Gold Standard Answers¶
- Memcached applies LRU eviction strictly within individual slab classes; a full slab class evicts keys even if other slab classes have free memory. Redis evaluates global LRU/LFU approximations across the entire dataset.
- Redis 6.0 offloads network I/O socket reading/writing to background worker threads while keeping command execution strictly single-threaded, narrowing the raw throughput gap with Memcached while preserving atomicity.
- In Memcached, updating 1 field requires fetching the full JSON string over the network, deserializing it, mutating the field, re-serializing, and writing back. In Redis,
HSET my_key field valueupdates the attribute directly in memory. - Redis reloads memory state rapidly from RDB snapshots or AOF logs on boot. Memcached starts completely cold (empty RAM); warming requires executing synthetic background application queries before opening public traffic.
- Blocking \(O(N)\) operations stop the single-threaded event loop, queueing incoming client requests until socket timeouts trigger cascading application outages. Always use
SCANandUNLINKinstead. - Memcached allocates memory into fixed slab sizes (e.g., 64B, 128B). Slab Calcification occurs when slabs become locked to specific size classes based on historical traffic; if traffic patterns shift to different payload sizes, memory becomes underutilized.
- Redis supports native atomic data primitives (
INCR,ZADD) and Lua scripts. Memcached only supports basic stringincr, making complex sliding window rate limiters difficult to implement without race conditions. - Sentinel instances monitor primary nodes via PING heartbeats. If a quorum of Sentinels reports
SDOWN(subjective down), it escalates toODOWN(objective down), triggering a Raft-like election to promote the best replica to primary. - Deploy Twemproxy or Mcrouter to manage connection pooling, request multiplexing, and consistent hashing sharding across hundreds of cache nodes, saving backend application connection overhead.
- Redis includes an active defragmentation engine (
activedefrag yes) that allocates new memory pages for live keys usingjemallocand frees fragmented pages transparently in the background without restarting.
8. Common Mistakes¶
- Running
KEYS *or synchronousDELon large keys in production Redis, freezing the entire cache layer. - Using Memcached for objects requiring frequent partial updates, wasting serialization CPU and network bandwidth.
- Relying on Memcached for critical session state without a persistent database backup or replication layer.
- Over-allocating memory past physical RAM limits, triggering OS swap thrashing and destroying cache latencies.
- Ignoring Redis memory fragmentation ratio (
mem_fragmentation_ratio > 1.5), leading to out-of-memory crashes.
9. Keywords¶
Redis, Memcached, Eviction Policy, LRU, LFU, Slab Allocation, Slab Calcification, Active Defragmentation, Redis Sentinel, Twemproxy, Mcrouter, Redis 6.0 Multi-Threaded I/O, UNLINK, HSET.
10. Cheat Sheet¶
- Core Metric: Pure multi-threaded string caching = Memcached; Rich data structures & HA caching = Redis.
- Memory Management: Memcached = Slab allocation (no OS fragmentation, but risk of slab calcification); Redis =
jemalloc+ Active Defragmentation. - Data Mutation: Redis allows field-level updates (
HSET); Memcached requires full string re-serialization. - Failover & HA: Redis Sentinel/Cluster provides auto-failover and replication; Memcached relies on client-side sharding or proxies (Mcrouter).
- Eviction: Memcached = Per-slab LRU; Redis = Configurable global LRU, LFU, or random eviction.
- Command Safety: Never run \(O(N)\) blocking commands (
KEYS *,DEL) in Redis; useSCANandUNLINK.