You need a cache in front of your database. Every tutorial says Redis, a colleague swears by Memcached, and Facebook famously ran one of the largest Memcached deployments ever. Which should you pick?
Both are in-memory key-value stores that answer in well under a millisecond. The difference is scope: Memcached does one thing, Redis does many.
Memcached: a pure cache
- Values are opaque blobs (up to 1 MB by default, configurable with
-I). There's no structure: youget,set,delete,incrandcaswhole values. - Multi-threaded from the start. One instance uses every core, and throughput scales on big machines.
- Memory-efficient for small objects. The slab allocator avoids fragmentation, and per-item overhead is low.
- No persistence, no replication. A restart empties the cache. Distribution is done by the client with consistent hashing across independent servers. Losing a node means losing its share of the cache, and nothing else.
- LRU eviction is always on, so the cache never runs out of memory; it evicts.
- Extstore can push colder values to flash while keeping keys in RAM, for very large caches.
Redis (and Valkey): a data-structure server
- Rich data types: hashes, lists, sets, sorted sets, streams, bitmaps, HyperLogLog and geo, operated on on the server (
HINCRBY,ZADD,LPUSH). - Persistence (RDB snapshots, AOF), replication, automatic failover (Sentinel) and Cluster sharding.
- Pub/Sub, Lua scripting, transactions, TTLs on every key, and keyspace notifications.
- Command execution is single-threaded per instance (I/O can be threaded). You scale with more shards rather than more cores.
Choose Memcached when
- you're caching rendered fragments, serialized objects or query results looked up by key, and nothing else;
