Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 37 ยท Patterns and Comparisons

Memcached vs Redis

Compare Memcached and Redis on data model, threading, persistence, replication, eviction and operations, then use a decision helper and a data-transfer experiment to choose.

Memcached and Redis are the two names that come up whenever someone needs an in-memory cache. They overlap on the simplest use (store a value under a key with an expiry) and then diverge sharply. Memcached stays deliberately small: a fast, multithreaded cache for opaque values. Redis is a data structure server: strings, hashes, lists, sets, sorted sets, streams and more, with persistence and replication.

This lesson compares them honestly, points out where each one wins, and gives you a decision helper. Neither server runs inside the live editors; the code here are stand-ins that illustrate the data-model differences. Redis code appears in read-only blocks and is labelled as such. Features change over versions, so verify specifics in the documentation of the version you deploy.

The comparison

Aspect Memcached Redis
Data model Key to opaque byte string Strings, hashes, lists, sets, sorted sets, streams, bitmaps, HyperLogLog, geo
Max value size 1 MB by default (configurable) Up to 512 MB
Threading Multithreaded; scales across CPU cores in one process Command execution is single-threaded; I/O threads can help; scale out with more instances
Persistence None: a restart empties the cache Optional snapshots (RDB) and append-only log (AOF)
Replication / HA None built in; use clients, proxies or replicated key names Replicas, Sentinel for failover, Cluster for sharding
Sharding Always client-side or via proxy Client-side, proxy, or native Redis Cluster
Eviction Segmented LRU per slab class Configurable: LRU/LFU approximations, random, TTL-based, or no eviction
Atomic operations incr/decr, cas, add Many commands plus transactions and server-side scripting
Other features Very small command set Pub/sub, streams, scripting, modules, geospatial, rate-limit helpers
Operations Few knobs, very predictable More features, more to configure and to secure

Redis also has forks and compatible servers (for example Valkey), which share most of these properties.

Where Memcached shines

  • Pure caching of simple values. HTML fragments, serialized objects and query results: get, set, expire.
  • Using many CPU cores on one box. Threads share the hash table, so a big server serves many connections without running several processes.
  • Predictable memory behaviour and low operational surface. There is almost nothing to tune besides memory, connections and threads. Nothing to persist or replicate means no persistence-related latency spikes.
  • Throw-away semantics. If the node vanishes you lose only a cache, by design.

Where Redis shines

  • When the structure is the point. Leaderboards (sorted sets), queues (lists, streams), unique counters (HyperLogLog), sessions stored as hashes, rate limiting with atomic scripts, pub/sub.
  • Data you cannot afford to lose on restart, with snapshots or an append-only log, or that needs replication for availability.
  • Partial updates. Changing one field of a large object without rewriting it all.
  • Very large values beyond Memcached's default limit.

A data-model experiment: leaderboards

In Memcached, a leaderboard has to be stored as one serialized blob. Every score update means: read the whole blob, change one entry, write the whole blob back, and guard it with CAS (see Consistency Models and CAS). In Redis, a sorted set updates a single member in place. The experiment below measures how many bytes cross the network per update in the blob design as the leaderboard grows.

Python Blob update cost grows with the object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The cost grows linearly with the board, while a native sorted-set command stays small. The read-only Redis equivalent, shown for comparison, uses two commands (this is real Redis syntax; it is not runnable here):

TEXT
ZADD leaderboard 4200 player7
ZREVRANGE leaderboard 0 9 WITHSCORES

When the logic is just "cache a rendered page", this advantage is irrelevant; when you maintain structured, frequently changing data in memory, it is decisive.

A simple decision helper

The helper below turns a few requirements into a suggestion. It is a teaching aid and a starting point for discussion, not a rule: many sites run both, using Memcached for a high-volume page and object cache and Redis for queues, counters and sessions.

Python Which one fits these requirements?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Practical migration notes

  • Keep the interface thin. Wrap the cache behind two or three functions (get, set, delete) so you can switch products or run both.
  • Test failure behaviour. A cache that returns "miss" on error is safe; a Redis used as a database needs backups and monitoring of replication.
  • Do not store the only copy in a cache, with either product, unless you have deliberately configured persistence and recovery.
  • Both need network protection. Neither should be reachable from the internet; see Cache Anti-Patterns and Security.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement about persistence is correct?

    Redis offers RDB snapshots and an append-only log; Memcached is purely in-memory.
  2. 2. Which workload is a natural fit for Redis rather than Memcached?

    Sorted sets update members in place and give rankings, which would be clumsy as a Memcached blob.
  3. 3. What is a strength of Memcached's threading model?

    Memcached is multithreaded, so one server process can use all cores.
  4. 4. Why wrap your cache in a thin interface in your application?

    A small abstraction keeps the choice reversible and testable.

Summary

  • Memcached is a small, multithreaded cache of opaque values with no persistence or built-in replication.
  • Redis is a data structure server with persistence, replication, clustering and many data types.
  • Pick Memcached for simple, high-volume caching and Redis when structures, durability or messaging matter; many systems use both.
  • Blob-style storage in Memcached makes partial updates expensive.
  • Keep the cache interface thin and never make the cache the only copy of important data.

Next: Multi-Layer Caching and CDNs