Redis vs Memcached
The differences between Redis and Memcached that change a design (data model, threading, persistence, clustering, memory) and how to justify choosing one for a workload.
The problem
In 2003 LiveJournal’s databases were struggling to generate pages, while its web servers had spare memory. Brad Fitzpatrick wrote Memcached to pool that memory into one large hash table reachable over the network. It did one thing, storing a blob under a key with an expiry, and it took the load off the databases.
The limits of a blob cache show up as soon as the cached thing has structure. Suppose you cache a user’s list of 5,000 follower ids and need to add one. With Memcached you fetch the whole value, deserialise it, append, serialise and write it back. Two servers doing this at once overwrite each other unless you add a check-and-set loop.
In 2009 Salvatore Sanfilippo released Redis, which he had built for a real-time analytics product. Its idea was to keep data structures at the server and send it operations. Appending to a list is one command, and the server applies it atomically.
Today both are mature, and “I will add Redis” is said in most design interviews. The follow-up is “why not Memcached?”, and a good answer rests on a handful of differences.
Data model
Memcached maps a key of up to 250 bytes to an opaque value, 1 MB at most by default, with an expiry. The commands are get, set, add, replace, delete, increment and decrement, a check-and-set, and multi-key get.
Redis values are typed: strings, hashes, lists, sets, sorted sets, streams, bitmaps, HyperLogLogs and geospatial indexes. Each type has server-side commands, such as ZADD and ZRANGE for a ranked set or HINCRBY for one field of a hash. Several commands can be made atomic with MULTI and EXEC or a Lua script.
If all you store is serialised objects, this difference is worth little. If you need counters, rankings, sets or queues, it is the whole decision.
Threading model
Memcached is multi-threaded. Worker threads (four by default) share one hash table with fine-grained locks, so one process uses many cores.
Redis executes commands on a single thread, one at a time. Two consequences follow. Every command is atomic without locks, which makes INCR and Lua scripts safe to build on. And one slow command, such as KEYS * or deleting a set with millions of members, blocks every other client until it finishes.
“Single-threaded” needs a qualifier. Redis has long used background threads for work such as file syncing and, since 4.0, freeing large values with UNLINK. Since 6.0 (2020) it can use I/O threads to read and parse requests and write replies in parallel. This is off by default in 6 and 7, and later releases of Redis and Valkey reworked it for more throughput. Command execution remains on the main thread in all of them. To use more cores for execution, run more shards.
Persistence
Memcached is a pure cache. After a crash it starts empty.
Redis offers two mechanisms, usable together:
- RDB writes a point-in-time snapshot. Redis forks, and the child writes the file while the parent keeps serving, relying on copy-on-write. Snapshots are compact and quick to load, but a crash loses everything since the last one, and the fork can use a lot of extra memory under heavy writes.
- AOF (append-only file) logs each write command, and is off until you enable it. The
appendfsyncsetting decides durability:alwayssyncs before replying and is slow,everysec(the default) can lose about a second, andnoleaves it to the operating system. Redis rewrites the file in the background to keep it small.
For a pure cache you can switch both off. Many teams keep RDB on so that a restarted node comes back warm, which avoids the cold start described in Cache stampede and thundering herd.
Replication and clustering
Memcached servers know nothing about each other. The client library hashes each key to a server, usually with consistent hashing, and a dead server means its share of keys miss. Proxies such as mcrouter move that routing out of the client.
Redis builds three layers into the server.
- Replication. A primary streams its writes to replicas asynchronously. Replicas can serve reads that may be slightly behind.
- Sentinel. A set of separate Sentinel processes watches a primary and its replicas. When a quorum agrees the primary is down, they promote a replica and tell clients where it is. This gives failover for a dataset that fits on one machine. It does not shard.
- Redis Cluster. The key space is divided into 16,384 hash slots, and a key’s slot is
CRC16(key) mod 16384. Each primary owns a set of slots and has its own replicas. Clients cache the slot map and talk to nodes directly.
Step 1 of 9
Redis Cluster divides the key space into 16,384 hash slots. Here three primaries each own a range of slots, and each has one replica that receives its writes asynchronously.
Read the steps as text
- Redis Cluster divides the key space into 16,384 hash slots. Here three primaries each own a range of slots, and each has one replica that receives its writes asynchronously.
- To find a key, the client computes the CRC16 checksum of the key name, modulo 16,384. For the key product:7 that gives slot 8390, which falls in the range owned by Primary B.
- The client keeps a copy of the slot map, so it sends the command straight to Primary B. There is no proxy and no extra hop.
- Later an operator rebalances the cluster and moves slot 8390 to Primary C. The client's map is now out of date, so it still sends the command to Primary B.
- Primary B does not forward the request. It replies with a MOVED error that names the slot and its new owner. The client updates its map and retries against Primary C.
- Now Primary B crashes. The other nodes notice through gossip that it has stopped answering. Its replica asks the remaining primaries for their votes, and with a majority it promotes itself to primary.
- Clients are redirected to the new primary. Replication is asynchronous, so any write that B acknowledged but had not yet sent to its replica is lost. Redis Cluster chooses speed and availability over never losing a write.
- One more rule follows from slots. A command that touches several keys only works if all of them hash to the same slot. A hash tag arranges this, because only the part of the key inside curly braces is hashed.
- So the client does the routing, a moved slot costs one redirect, and a failover can cost the last few writes. That is acceptable for a cache, and something to design around for anything else.
Two limits matter in a design. Multi-key commands and transactions need all their keys in one slot, which you arrange with hash tags: only the part of the key inside braces is hashed, so {user:42}:cart and {user:42}:profile share a slot. And because replication is asynchronous, a failover under Sentinel or Cluster can lose writes the old primary had acknowledged. The WAIT command reduces that risk but does not make Redis strongly consistent. Replication covers the general trade-off.
Memory efficiency
Memcached uses a slab allocator. Memory is divided into pages assigned to classes of fixed chunk sizes, and an item goes in the smallest chunk that fits. Fragmentation stays low and usage is predictable, at the cost of some wasted space inside chunks. Per-item overhead is about 50 bytes.
Redis uses a general-purpose allocator and keeps more metadata per key, so a large number of small string values usually costs more memory than in Memcached. In the other direction, Redis stores small hashes, lists and sets in compact encodings, so packing many small fields into one hash can be very efficient.
Beyond caching
Because of its data structures, Redis is used for much more than caching: rate limiters, distributed locks, session stores, leaderboards on sorted sets, queues and streams, publish and subscribe, and approximate unique counts. Memcached does none of this, and some teams see that as a virtue. It cannot be misused as a database, and it has no command that can stall the server.
Licensing and forks
Memcached has always been under a permissive BSD licence. Redis was too until March 2024, when Redis Ltd moved new versions, starting with 7.4, to a dual source-available licence (RSALv2 or SSPLv1) that restricts offering Redis as a managed service. Within weeks the Linux Foundation announced Valkey, a fork of Redis 7.2 that keeps the BSD licence and is backed by several cloud providers. In 2025, Redis 8 added the AGPLv3 as a further licence option.
When to choose each
| You need | Choose |
|---|---|
| A plain key to blob cache, as large and simple as possible | Memcached |
| To use many cores in one process for get and set | Memcached |
| Counters, sets, rankings, queues or atomic multi-step updates | Redis or Valkey |
| Replication, automatic failover or a warm restart | Redis or Valkey |
| One system for cache, rate limiting and locks | Redis or Valkey |
Do not choose Memcached if values exceed its item size limit, if the cache must survive a restart, or if you will end up rebuilding lists and counters in the client. Do not choose Redis as the only home for data you cannot lose unless you have worked through its durability settings and failover behaviour, and do not choose it for a dataset that will not fit in memory.
In the interview
When you name a cache, add one clause of justification. “Redis, because the feed is a sorted set and I want atomic updates” or “Memcached would do here, since it is only serialised blobs” both show a decision was made.
Be ready for three follow-ups. How does it scale past one node (Cluster with hash slots, or client-side hashing for Memcached)? What happens when a node fails (replica promotion, possible loss of recent writes, or a cold shard)? And what happens on restart (RDB or AOF, or an empty cache and a warm-up plan)? Add the threading caveat if performance comes up: commands run serially, so keep them small.
Key takeaways
- Memcached stores opaque values by key and nothing else. Redis stores data structures and runs atomic operations on them at the server.
- Memcached is multi-threaded. Redis executes commands on one thread, with optional I/O threads since 6.0, so each command is atomic and one slow command blocks everyone.
- Redis can persist with RDB snapshots, an append-only file or both. Memcached is designed to lose its contents on a restart.
- Redis has built-in replication, Sentinel for failover and Redis Cluster for sharding over 16,384 hash slots. Memcached servers are independent and the client does the sharding.
- Redis replication is asynchronous, so a failover can lose acknowledged writes. Persistence does not make it a system of record on its own.
- Redis changed licence in 2024, which produced the Valkey fork. Check current terms before a commercial decision.
- Pick Memcached for a plain, large, multi-core blob cache. Pick Redis or Valkey when you need structures, atomic operations, failover or a warm restart.
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
The one-line difference between Memcached and Redis.
Memcached is a multi-threaded cache of opaque values by key. Redis is a data structure server with atomic server-side operations, persistence, replication and clustering.
How does Redis use threads?
Commands execute one at a time on a single main thread. Background threads handle tasks such as fsync and lazy freeing, and since 6.0 optional I/O threads handle socket reads and writes.
RDB versus AOF.
RDB is a periodic point-in-time snapshot written by a forked child. It is compact and fast to load but loses everything since the last snapshot. AOF logs each write and with everysec loses about a second.
What is Redis Sentinel for?
High availability without sharding. Sentinel processes monitor a primary and its replicas, agree by quorum that the primary is down, promote a replica and tell clients the new address.
How does Redis Cluster place a key?
slot = CRC16(key) mod 16384. Each primary owns a set of slots. Clients cache the slot map and are corrected with MOVED redirects. Hash tags force keys into the same slot.
How does Memcached scale beyond one server?
The servers do not know about each other. The client library, or a proxy such as mcrouter, hashes each key to a server, usually with consistent hashing.