Library / Case Studies

Optimizing Memcached Efficiency at Quora

As Quora has continued to grow, we've continued to invest in the scalability of our infrastructure. Caching is a key component of our architecture, and we use Memcached as our primary store to cache results from slower, persistent databases. While data stores like MySQL, HBase, and Redis offer a...

By Quora engineers · Quora · Added 2026-10-12

Open original source

All content belongs to Quora engineers / Quora. We only link to it and give credit. Report a problem

Learn this case study

About 37 min left. Your work is saved as you go.

What you are about to study

Quora explains a real system. You should know the basics: caching, queues and replication.

Ideas you will meet

Distributed Systems
Consensus, time, failure and the fundamentals of scale-out.

What we already know about this

Our own lessons on these ideas. Read them here before you start, so it makes sense.

  • Core Concepts: How Things Actually Work · 8 minWhy Redis is fast, and Redis versus MemcachedIn-memory storage, a single-threaded event loop and rich data structures, plus how persistence and replication work.Read here

    Why it is fast

    1. Everything lives in memory. No disk seek on the read path.
    2. Simple, efficient data structures (hash tables, skip lists, compact encodings) with predictable costs.
    3. A single-threaded event loop for command execution: no locks between threads, no context switching, and each command runs atomically. I/O multiplexing lets one thread serve thousands of connections.
    4. A lean protocol and in-process memory management.

    (Newer versions use extra threads for network I/O, but command execution stays ordered.)

    Memory modules in the slots of a computer motherboard: in-memory data is read in nanosecondsImage: Mixabest · Public domain · Source · Wikimedia Commons: Dual-channel DDR memory use 6026

    How the pieces fit

    Figure SDA-CORE-09-1 · Redis: event loop, memory, persistence and replication · © forcsguy.com

    More than a cache

    Redis stores typed values:

    TypeUse
    Stringcached values, counters, flags
    Hashobjects with fields (a user profile)
    Listqueues, recent items
    Setunique members, tags, set maths
    Sorted setleaderboards, priority queues, time-ordered data
    Othersstreams, bitmaps, HyperLogLog, geospatial

    Because operations run on the server, you can increment, push, pop or intersect without a read-modify-write race.

    Figure SDA-CORE-09-2 · Redis data types and what they are for · © forcsguy.com

    Durability options

    Memory is volatile, so Redis offers:

    • Snapshots (RDB): periodic point-in-time dumps. Compact, but recent writes can be lost.
    • Append-only file (AOF): logs every write; replays on restart. Safer, bigger and a little slower.
    • Or none at all when it is purely a cache.

    Scaling and availability

    • Replication: a primary copies to replicas (asynchronously), serving reads and enabling failover.
    • Sentinel / managed failover promotes a replica when the primary dies.
    • Cluster: splits keys across nodes by hash slot, for data larger than one machine.

    Redis versus Memcached

    RedisMemcached
    Data typesmanystrings only
    Persistenceoptionalnone
    Replicationyesnot built in
    Threadingone for commandsmulti-threaded
    Best forcache plus data structures, queues, countersa very simple, large, pure cache

    For most new projects Redis is the more flexible choice. Memcached remains fine for a straightforward cache that must use many CPU cores.

    Pitfalls

    • Memory is finite: set a maxmemory and an eviction policy (for example LRU).
    • Avoid slow commands on huge collections.
    • A very hot key still concentrates load on one node.

    More on this site

    Open this lesson with its quiz and exercise

Go deeper on this

A path through videos, articles, other case studies and code on the same ideas as Optimizing Memcached Efficiency at Quora.

  1. Warm upGet the vocabulary first.

  2. WatchHow others solve the same problem, on video.

    Videos belong to their creators and play from YouTube. More on YouTube (opens in a new tab)

  3. ReadEngineers explain it in their own words.

  4. See it in another companyThe same ideas, a different system.

  5. Read the codeSee how it looks when it is real.

Keep learning

Loading community…