Dev Skills

Redis vs Memcached: differences and which to choose

Both are in-memory caches. Memcached does one thing simply; Redis is a data-structure server that also caches.

By devskills editorial team · Last updated

1

What problem does the Redis vs Memcached choice solve?

"Put Redis in front of it" has become a reflex, and interviewers sometimes ask why not Memcached. The honest answer is that for a plain look-aside cache of strings, both work, and the choice comes down to what else you need.

Memcached stores opaque values by key, across many threads, and nothing more. Redis stores typed data structures, can persist and replicate them, and runs scripts against them. That extra power is why Redis gets used for rate limiters, queues and leaderboards, not just caching.

2

What the Redis vs Memcached choice costs you

What it costs you
Redis's features are also its operational surface: persistence settings, replication, failover, cluster slots and memory used by data structures all need understanding and monitoring, and a Redis that has become a database is no longer something you can flush. Memcached's simplicity costs you everything it does not do: no persistence, no built-in replication, no data types, so a node restart empties it and anything beyond get and set is your code's job.
3

When to reach for the Redis vs Memcached choice, and when not to

Use it when

  • Memcached: a pure cache of serialised objects or HTML fragments, sharded by the client, where a lost node only means misses.
  • Redis: anything that needs data structures or atomic operations on the server: counters, rate limits, sorted leaderboards, sets, streams.
  • Redis: when cached data should survive a restart or fail over to a replica.

Do not, when

  • Do not choose Redis for its persistence and then treat it as the system of record without understanding what its persistence settings can lose.
  • Do not choose Memcached when you will soon need anything beyond get, set, delete and increment; you will end up adding Redis as well.
4

How the Redis vs Memcached choice actually works

A
Memcached
  • —Keys and opaque values (1 MB item limit by default, configurable).
  • —Multithreaded: one big node uses all its cores.
  • —No persistence and no built-in replication; clients shard keys across nodes.
  • —Evicts least-recently-used items when full, by default. A pure cache by design.
B
Redis
  • —Strings, hashes, lists, sets, sorted sets, streams, bitmaps, HyperLogLog, geo.
  • —Commands execute on a main thread; optional I/O threads help with network work.
  • —Persistence (RDB snapshots, AOF log), replicas, Sentinel failover, Redis Cluster sharding.
  • —Eviction policy is configurable; the default is noeviction, which rejects writes when memory is full.
Which one: If all you need is a look-aside cache of blobs, either works and Memcached is the simpler thing to run. As soon as you need a data structure, an atomic server-side operation, or a cache that survives a restart, Redis.
The same cache role, two very different tools.

Data model. Memcached maps a key to a blob of bytes; your application serialises and deserialises. Redis understands types, so it can increment a field inside a hash, add to a sorted set and fetch the top ten, or pop from a list, all atomically on the server without reading the value back.

Threading. Memcached is multithreaded, so a single large node scales with its cores. Redis executes commands one at a time on a main thread, which is what makes each command atomic and makes Lua scripts and MULTI/EXEC transactions simple to reason about. Since Redis 6, optional I/O threads take over socket reads and writes. To use many cores for commands, you run more Redis processes, usually as a cluster.

Persistence and replication. Memcached has neither built in; a restarted node is empty, and high availability means the client tolerates misses. Redis offers RDB snapshots, an append-only file (AOF) with a configurable fsync policy, asynchronous replicas, Sentinel for automatic failover and Redis Cluster for sharding across 16,384 hash slots. Asynchronous replication means a failover can lose the last writes acknowledged by the old primary, which matters if Redis holds anything you cannot rebuild.

Eviction. Memcached evicts least-recently-used items when memory is full, by default (the -M flag makes it return errors instead); evicting is its job. Redis lets you choose: allkeys-lru, allkeys-lfu, the volatile-* policies that only evict keys with a TTL, or noeviction, the default, which returns errors on writes when memory is full. For a cache, set an eviction policy explicitly; for anything else, monitor memory.

Licensing and Valkey. Memcached is BSD-licensed. Redis was BSD-licensed until March 2024, when Redis Ltd. moved new versions to a source-available dual licence (RSALv2 and SSPLv1). The Linux Foundation then started Valkey, a fork of Redis 7.2.4 under the BSD licence, which several cloud providers now offer. Redis 8 added the AGPLv3 as a third licence option. For most teams using a managed service this changes little, but it matters if you embed or redistribute it, so check the licence of the exact version you run.

When Memcached still wins. A very large, simple cache of rendered pages or serialised objects, where multithreading lets a few big nodes replace many Redis processes, and where losing a node only costs misses. Some of the largest published caching deployments, such as the one described in Facebook's memcache paper, are built this way.

MemcachedRedis
Data modelKey to opaque bytesKey to typed structures (strings, hashes, lists, sets, sorted sets, streams and more)
Max value size1 MB by default (configurable)512 MB per string
ThreadsMultithreadedCommands on a main thread; optional I/O threads
PersistenceNone built inRDB snapshots and/or AOF log
Replication and failoverNone built in; client-side shardingAsync replicas, Sentinel, Redis Cluster
EvictionLRU by defaultConfigurable; default noeviction
Server-side logicGet, set, add, cas, incr/decrLua scripts, functions, MULTI/EXEC transactions
Pub/sub, queuesNoPub/sub, lists as queues, streams with consumer groups
LicenceBSDRSALv2/SSPLv1, plus AGPLv3 from Redis 8; Valkey fork is BSD
Feature comparison. Check the documentation of the version you run; both projects keep evolving.
redis-cli — things Memcached cannot do on the server
# leaderboard: a sorted set keeps scores ordered for you
ZADD game:42 1500 asha 1320 ben 1710 chen
ZREVRANGE game:42 0 2 WITHSCORES # top three, highest first
# fixed-window rate limit: atomic increment plus expiry
INCR rl:user:7:202610261405
EXPIRE rl:user:7:202610261405 60
# a field inside an object, without reading it back
HINCRBY cart:99 sku-123 1
With Memcached each of these is a read, a change in your code and a write back, with a `cas` token to avoid lost updates.
Common trap
  • Saying Redis is "multithreaded now" without qualification. I/O threads exist; commands still execute one at a time.
  • Leaving Redis on the default noeviction policy and using it as a cache, then getting write errors when memory fills.
  • Treating Redis replication as durable. It is asynchronous, so a failover can drop acknowledged writes.
Say this
"Redis, because besides caching I need atomic counters for rate limiting and a sorted set for the leaderboard. Eviction set to allkeys-lru for the cache keys, and I treat everything in it as rebuildable, because replication is asynchronous. If this were only a page cache I would be happy with Memcached."
5

The follow-up an interviewer will ask about the Redis vs Memcached choice

"Why Redis and not Memcached for this cache?"

What a strong answer sounds like

For the cache alone, either would do, and I would say so. Both are in-memory, sub-millisecond, and support the get, set and TTL I need for cache-aside. I am choosing Redis because the same design also needs a rate limiter and a leaderboard: an atomic increment with an expiry per user per minute, and a sorted set for the top scores. With Memcached I would implement both in application code with read, modify and compare-and-set loops, or run Redis anyway. The costs I accept: Redis executes commands on one main thread per process, so I scale it by sharding rather than with bigger machines, and its replication is asynchronous, so I treat everything in it as rebuildable. I would also set an explicit eviction policy, because the default refuses writes when memory fills, which is the wrong behaviour for a cache.

6

Redis vs Memcached: common questions

Is Redis faster than Memcached?

For simple get and set, both answer in well under a millisecond inside a data centre, and the network usually dominates. Memcached can use more cores on one node; Redis is scaled out with more processes. Pick on features, then benchmark your own workload.

What is Valkey?

An open-source (BSD) fork of Redis 7.2.4, created under the Linux Foundation in 2024 after Redis changed its licence. It is wire-compatible with Redis for the commands that existed at the fork, and several cloud providers offer it as a managed service.

Can Memcached persist data?

Not as a database would. It is designed as a cache, and a restarted node normally starts empty. Treat any data in it as something you can reload from the source of truth.

Should I use Redis as my primary database?

Only if you have configured persistence and replication for the loss you can accept, and your data fits in memory. Many teams use it for data that is valuable but rebuildable (sessions, rate limits, leaderboards) and keep the system of record elsewhere.

7

Your turn

60-second self-check

Your team caches rendered product pages (50 KB each, 200 GB total) and nothing else. A colleague insists on Redis Cluster with AOF persistence. What would you question?

Stuck? Take one hint at a time0 of 2 used
0 / 25 characters
8

Learn it from the source