Cachingmedium

Redis vs Memcached

Both are in-memory key-value stores used for caching. Redis supports rich data structures, persistence, pub/sub, and clustering. Memcached is simpler, multi-threaded, and optimized for pure caching with higher throughput per core.

Memory anchor

Redis = a Swiss Army knife that also happens to be blazing fast (data structures, pub/sub, scripting). Memcached = a single-purpose hammer -- it only does one thing (string caching) but swings with both hands (multi-threaded).

Expected depth

Choose Redis when you need: rich data types (lists, sets, sorted sets, hashes), pub/sub messaging, Lua scripting, persistence (RDB snapshots, AOF logging), distributed data structures (distributed locks, rate limiters, leaderboards). Choose Memcached when you need: maximum throughput for simple string key-value caching, multi-threading (Redis is single-threaded per shard), or a very simple operational footprint.

Deep — senior internals

Redis's single-threaded event loop processes commands atomically — this enables operations like INCR (atomic increment) and Lua scripts to be race-condition free. Redis 6.0 introduced I/O threading for network operations while keeping command processing single-threaded. Redis Cluster shards data across nodes using 16,384 hash slots; each node owns a subset of slots. Client libraries route requests to the correct node based on CRC16(key) mod 16384. Redis persistence tradeoffs: AOF (Append Only File) logs every write for durability (can replay on restart) at the cost of write amplification; RDB (periodic snapshots) is faster to restore but loses writes since the last snapshot. For rate limiting and distributed locks, Redis SETNX + EXPIRE is the foundation of the Redlock algorithm (though Redlock's safety in the face of clock skew is debated by Martin Kleppmann).

🎤Interview-ready answer

I default to Redis for almost all caching use cases because its rich data structures enable patterns beyond simple caching: sorted sets for leaderboards and rate limiting, lists for queues, pub/sub for real-time notifications. I'd use Memcached only if benchmarking showed it to be the bottleneck and the use case is purely string key-value with no need for persistence or complex structures. In practice, the operational simplicity of using Redis for both caching and lightweight messaging outweighs Memcached's marginal throughput advantage.

Common trap

Using Redis persistence (AOF) for a cache without understanding the write amplification. AOF rewrites every write to disk — on a cache-heavy write workload, this can saturate disk I/O. For pure cache use cases, disable persistence entirely (appendonly no, save '').

Related concepts