CodePark/ questions / Choosing a cache layer
START PRACTICINGPRACTICE
Backend question · Redis & caching › Choosing a cache layer

Redis vs Memcached: how do they differ, and when might you still pick Memcached?

Memcached is a simple cache with only keys and string values. Redis is a full in-memory data store with many data types and features. Pick Memcached only for a very simple, high-volume cache.

Memcached:

  • It is a simple in-memory key-value cache.
  • Values are opaque strings or blobs. Opaque means Memcached does not look inside them.
  • An item can be 1 MB by default.
  • It uses multiple threads.
  • It evicts with LRU. LRU (Least Recently Used) means it removes the item nobody used for the longest time.
  • It has no persistence (saving data to disk) and no built-in replication (copying data to other servers).
  • Clients usually shard keys themselves. This means the client decides which server holds each key.

Redis:

  • It has rich data types: hashes, lists, sets, sorted sets and streams.
  • It has atomic operations on these types. Atomic means each operation runs fully, with no other command in the middle.
  • It has TTLs (time to live: a key deletes itself after a set time).
  • It has persistence and replication.
  • It has Sentinel (automatic failover) and Cluster (data split across many nodes).
  • It has Pub/Sub messages, transactions and Lua scripting.

Because of these features, Redis is useful beyond caching. Teams use it for sessions, queues, rate limiters, leaderboards and locks.

Memcached can still make sense for a pure, very simple cache that holds large volumes of small blobs. Its multithreaded design uses many CPU cores on one node. That fits when you need nothing else.

In short: for most backend teams, Redis is the default, because one tool covers many needs.

  • Memcached stores plain key-value strings, uses many threads and has no persistence.
  • Redis has data structures, persistence, replication and Cluster.
  • Redis also adds Pub/Sub, transactions and Lua scripting.
  • Memcached fits a simple, high-throughput, pure cache.
  • Redis is the usual default choice.