Virat Kohli273M followers
profile:viratpage · 2 KBlikes:virat1,204,337top10built 9:55- 8 cores
on disknothing
- misses only
Virat Kohli273M followers
profile:viratpage · 2 KBlikes:virat1,204,337top10built 9:55on disknothingVirat Kohli273M followers
profile:viratpage · 2 KBtrending · virat1,204,337top 10kept in orderon diska copyInstagram the night RCB win: millions open Virat’s profile, the Explore tab ranks tonight’s posts, then the cache restarts. Illustrative counts.
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.
| Difference | Memcached | Redis |
|---|---|---|
| What it stores | Plain values: a key and its bytes | Values with types: counters, lists, ranked sets |
| Work on the data | Add one to a number; anything else in the app | Count, rank and list inside the cache |
| Threads | Every core of the machine | Commands on one thread; more machines to grow |
| After a restart | Empty: every read goes to the database | Reloads its copy from disk |
| Pick it for | A big, plain cache of pages and objects | Counters, leaderboards, queues, sessions |
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.
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.
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.
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.