Compare

Redis vs Memcached

MemcachedPlain values, every core
virat.kohli

Virat Kohli273M followers

FollowMessage
App servercache first
Memcachedevery core
  • profile:viratpage · 2 KB
  • likes:virat1,204,337
  • top10built 9:55
  • 8 cores
  • on disknothing
PostgreSQLquiet
  • misses only
RedisCounters, lists, a copy on disk
virat.kohli

Virat Kohli273M followers

FollowMessage
App servercache first
Redis1 thread
  • profile:viratpage · 2 KB
  • trending · virat1,204,337
  • top 10kept in order
  • 1 thread
  • on diska copy
PostgreSQLquiet
  • misses only

Instagram the night RCB win: millions open Virat’s profile, the Explore tab ranks tonight’s posts, then the cache restarts. Illustrative counts.

Redis or Memcached?

If the cache only holds saved pages and objects, Memcached is simple and uses every core. If it has to count, rank, or survive a restart, pick Redis.

DifferenceMemcachedRedis
What it storesPlain values: a key and its bytesValues with types: counters, lists, ranked sets
Work on the dataAdd one to a number; anything else in the appCount, rank and list inside the cache
ThreadsEvery core of the machineCommands on one thread; more machines to grow
After a restartEmpty: every read goes to the databaseReloads its copy from disk
Pick it forA big, plain cache of pages and objectsCounters, leaderboards, queues, sessions

Redis vs Memcached, explained step by step

1. Virat’s profile

One Instagram app, with a cache in front of its database. On the left, Memcached: plain values, kept in memory. On the right, Redis: counters and ranked lists, with a copy on disk. Let’s put both through the night RCB win.

First, RCB win, and millions open Virat’s profile. The app asks the cache before the database.

Memcached has his page saved under one key, and hands it back in a millisecond. The database never hears about it.

It spreads the work over every core, so one machine serves millions of these.

Now the same profile with Redis. Same key, same saved page, same millisecond.

Redis works through commands on one thread, but for a plain saved page the two are equal. The difference starts when the cache has to do more.

2. Trending tonight

Next, the Explore tab shows tonight’s top posts. Every like lands in the cache.

Memcached can add one to a number, so each post keeps its own like count.

But it can’t sort. To find the top ten, a job reads every post’s count and sorts them itself, so the list is rebuilt only every five minutes.

Rajat sees Virat’s post at number three, though it’s already the most liked.

With Redis, every like is one command on a ranked list. Redis adds one and moves the post into place itself.

The top ten is one more command, already in order. Rajat sees Virat’s post at number one, live.

3. The cache restarts

Finally, the cache machine restarts for an update. Memcached kept everything in memory only, so it comes back empty.

Every profile open now misses and falls through to the database. Millions of reads at once, and it slows to a crawl.

Rajat’s profile just keeps loading.

Redis saved a copy of its data to disk. It restarts, loads that copy back, and the cache is full again.

Rajat’s profile opens from the cache, and the database barely notices.

In short

So which should you pick? If the cache only holds saved pages and objects, Memcached is simple and uses every core. If it has to count, rank, or survive a restart, pick Redis.