Memcached or Redis
easy 25 minMemcached and Redis both keep data in memory and answer in well under a millisecond on the server.
Video lesson · Chapter 1 of 5
The same profile lookup hides different work
Theory
Read or listen first. About 2 min.
What it is
Memcached is a server that stores values under keys in memory. A value is a block of bytes: the server does not look inside it. You can get, set, delete, count up or down, and replace a value only if nobody changed it since you read it.
Redis is a data structure server. A value can be a string, a list, a set, a sorted set, a hash, a stream and more, and the server has commands that work on the inside of each one. It can also write data to disk, copy it to other servers and pass messages between clients.
Why it exists
Reading from memory saves the database from repeating the same work. Both products do that job well, so the first question is not which is faster. The first question is whether you need anything more than get and set.
Extra features cost something: more settings to understand, more memory use to plan, and data that somebody may start to treat as permanent. A smaller tool is easier to run, and a richer tool can save many trips over the network.
A short history
2003
Brad Fitzpatrick releases Memcached, first written for the website LiveJournal.
2009
Salvatore Sanfilippo releases the first version of Redis.
2015
Redis 3.0 is released in April with Redis Cluster, which spreads keys over several servers.
2024
Redis changes its licence, and the Linux Foundation announces Valkey, a fork of the last version under the BSD licence.
2025
From Redis 8, the AGPLv3 open source licence is one of the three licences a user can pick.
Words to know
- Round trip
- One request to the server and its answer. Over a network this usually costs more than the command itself.
- Data structure
- A value with a shape the server understands, such as a list or a sorted set.
- Slab
- A block of memory Memcached cuts into equal chunks, so it does not fragment.
- RDB
- A Redis snapshot of all data at one moment.
- AOF
- Append-only file: Redis logs every write so it can replay them.
- CAS
- Check and set: store a value only if it has not changed since you read it.
How it works
Step through the idea, or press Play.
List what the data must do. Only get and set, or rank, count, queue and expire parts of it
Understand it
Start here
A tray and a tool belt
Two helpers work at a craft fair. One carries a plain tray: put a thing on it, take a thing off. The other wears a tool belt with several tools, each made for one job.
A tray is not worse than a belt. If all you do is carry cups, the belt is in the way.
The first one
Memcached on its own
Memcached runs several worker threads, four by default. Each thread serves many connections, so one server can use several processor cores. Memory is cut into slabs of equal chunks, and old items leave by a least recently used rule kept per slab class, in several segments.
One Memcached machine
Many threads
Four by default. Useful when one machine has spare cores and many connections.
Whole values
Keys up to 250 bytes. Values are bytes up to 1 MB by default. No fields, no ranks.
Counters and CAS
It can count up and down in one step, and replace a value only if it is unchanged.
Empty after a normal restart
Data is in memory. A clean warm restart is possible from version 1.5.18 if you set it up, but a crash still empties it.
Memory a chunk wastes
The second one
Redis on its own
Redis runs commands one at a time on a main thread, so each single command is atomic. Threads for network input and output can be switched on, and they read requests and write replies while commands still run one after another.
A Redis primary with disk and a replica
Six structures and what they are for
| Type | Example command | Good for |
|---|---|---|
| String | SET user:123 Ana | Simple values, sessions |
| List | LPUSH tasks t1 | Recent items, simple queues |
| Set | SADD tags redis | Unique items, tags, followers |
| Sorted set | ZADD board 100 p1 | Leaderboards, priority queues |
| Hash | HSET user:123 age 30 | Objects such as profiles |
| Stream | XADD events * type click | Event logs with readers that keep a position |
Four more features
Pub/Sub
Clients publish and subscribe. A message is delivered at most once: a client that is away misses it.
Lua scripts
A small program runs on the server as one step. Good for a rate limit counter.
Transactions
MULTIandEXECrun queued commands together. There is no rollback of those that worked.Cluster
Keys are spread over servers by 16,384 hash slots.
Disk
Persistence: what survives
Redis persistence choices
| Choice | How it works | What a crash can lose |
|---|---|---|
| None | Memory only | Everything |
| RDB snapshot | Writes all data at chosen times. Restarts quickly | Writes since the last snapshot |
| AOF, sync each second | Logs every write. Slower to load | About one second of writes |
| RDB and AOF | Both together | As for AOF |
A restart, side by side
Compare
Feature by feature
| Feature | Memcached | Redis |
|---|---|---|
| Plain values | Yes | Yes |
| Lists, sets, sorted sets, hashes, streams | No | Yes |
| Expiry time per key | Yes | Yes |
| Counters in one step | Yes | Yes |
| Check and set | Yes | Yes, with WATCH or a script |
| Snapshots and a write log | No | Yes, optional |
| Copies on other servers | Not in the server itself | Yes, asynchronous |
| Sharing keys over servers | Client or proxy decides | Redis Cluster, or the client |
| Pub/Sub, scripts, transactions | No | Yes |
| Threads | Many worker threads | One for commands, optional input and output threads |
| Largest value | 1 MB by default | 512 MB for a string |
Memcached fits when
- You need simple key-value caching only
- You want high throughput for simple operations
- You have many cores to use on one machine
- Memory per key matters and values are small and plain
- Losing everything on a restart is fine
- You already run it well
Memcached does not fit when
- You need ranks, queues or fields inside a value
- Data must survive a restart
- You need messages between clients
- You need built-in replicas or a cluster
- Several keys must change as one step
- You need a script on the server
Speed
Count trips, not microseconds
For a plain get, both servers spend very little time, and the network is the larger part. Differences between them depend on value size, cores, connections and settings. So benchmark with your own traffic. What you can predict is the number of round trips.
Cost of adding one point to a player
Adding a score to a leaderboard
Who tends to come out ahead
| Work | Usually better | Why |
|---|---|---|
| Very many small plain gets on a big machine | Memcached | Threads share the work |
| Change part of a value | Redis | The value never leaves the server |
| Several keys as one step | Redis | A script or a transaction |
| Fetch 100 keys | Either | Both can answer many keys in one trip |
Memory
What fills up and what leaves
Memcached has a memory limit and removes old items when a class is full. Redis removes nothing by default: with a limit and the noeviction policy, writes fail when it is full. As a cache, set maxmemory and an eviction policy. See cache eviction and expiry times.
What happens when memory is full
Decide
A decision you can draw
Which one do I need?
Use cases
| Job | Pick | Note |
|---|---|---|
| Database query results | Memcached or Redis | Whole values, short life |
| HTML fragments and API responses | Memcached or Redis | Same |
| Sessions | Either | With Memcached a lost session means signing in again |
| Leaderboard | Redis | Sorted set |
| Simple job list | Redis | List, or a stream if jobs must not be lost |
| Rate limit | Redis | A counter and its expiry in one script |
| Live notifications | Redis | Pub/Sub, if missing one is fine |
| Real-time counts | Redis | Counters and sorted sets |
Pitfalls
Four things people get wrong
- 1
Treating a cache as the only copy
Even with the log, replication is asynchronous. Keep the source of truth in a database.
- 2
Believing one thread removes all races
One command is atomic. Two commands with your own code in between are not.
- 3
Counting a transaction as a rollback
If one queued command fails, the others still run.
- 4
Forgetting the eviction policy
A Redis cache without one refuses writes when full.
Remember
SIMPLE and RICH
| Memcached is SIMPLE | Redis is RICH |
|---|---|
| Stores plain values | Rich data types |
| In memory | In-server logic: scripts and atomic commands |
| Many threads | Clustering and copies built in |
| Performance for plain gets | Has a snapshot and a log |
| Light to run | |
| Efficient memory for plain values |
Summary
The whole topic in one minute.
Memcached does one job: keep bytes under a key, on many threads, with little to configure. Redis understands what is stored, so one command can do work that takes several trips with Memcached, and it can persist and replicate.
Pick Memcached for a pure cache of whole values that can all be lost. Pick Redis when you need a structure, an atomic step over several values, replicas, or data that should survive a restart. Measure with your own sizes before you argue about speed.
- Both hold data in memory, and a plain get is fast on either.
- Memcached: whole values, many threads, no built-in copies.
- Redis: structures, scripts, optional disk and replicas, commands one at a time.
- Count round trips: one Redis command can replace several steps.
- Set a memory limit and an eviction policy, and never keep the only copy in a cache.