DATABASES / SYSTEM CONCEPT BRIEF

Memcached

Memcached is a simple, high-performance, distributed in-memory cache.

BeginnerPhase 04 / Topic 7 of 16RequirementsTrade-offsFailure modes
01

Overview

Memcached is a simple, high-performance, distributed in-memory cache. It stores opaque byte values under string keys with TTLs and evicts least recently used items when memory is full. It has no persistence, no replication, and no rich data types; clients spread keys across servers with consistent hashing.

That simplicity makes it very fast and easy to scale horizontally with multithreading on each node. Choose Memcached when you need a pure cache for large volumes of simple values; choose Redis when you need data structures, persistence, or replication.

Sticky notes on a board

Quick to write, quick to read, and when the board fills up the oldest notes are thrown away. If the board falls over, the notes are gone, and that is fine because the real information is in the files.

02

When to use it

  • Pure caching of database query results, rendered fragments, or objects.
  • Very large cache clusters where simplicity matters.
  • Multithreaded performance on large machines.
  • Data you can always rebuild from the source of truth.
03

Where it shows up in interviews

Large-scale read caching

Recognize it when: cache in front of a huge database fleet.

  • Design Facebook's caching layer
  • Design a read-heavy social feed
04

Where it is used in real software

Facebook

Operated one of the largest Memcached deployments, described in 'Scaling Memcache at Facebook', with leases and regional pools.

Wikipedia and YouTube

Have used Memcached to cache rendered pages and database results.

Amazon ElastiCache

Offers managed Memcached alongside Redis.

05

Key terms

Slab allocator
Memory divided into size classes to limit fragmentation.
LRU eviction
Remove least recently used items when full.
Client-side sharding
Clients hash keys to choose a server.
CAS
Check-and-set with a version token for optimistic updates.
06

How it works, step by step

  1. 1
    Client hashes the key

    Consistent hashing picks one server.

  2. 2
    get(key)

    Returns the value or a miss.

  3. 3
    On miss, load from the DB

    Then set(key, value, ttl).

  4. 4
    On write, delete the key

    Invalidate rather than update.

  5. 5
    Server evicts under memory pressure

    LRU within slab classes.

07

Memcached vs Redis

Both are in-memory caches

Step 1 / 5
AspectMemcachedRedis
Data typesStrings (bytes)Strings, hashes, lists, sets, sorted sets, streams
PersistenceNoneRDB / AOF
ReplicationNone built inPrimary-replica, Sentinel, Cluster
ThreadsMultithreadedSingle-threaded commands (threaded I/O)
Max value size1 MB default512 MB

NOWAspect: Data types | Memcached: Strings (bytes) | Redis: Strings, hashes, lists, sets, sorted sets, streams

For a plain cache either works; Redis is more common today because the extra features are often useful.

08

Implementation

import Memcached from "memcached"; // Client hashes keys across serversconst cache = new Memcached(["cache-1:11211", "cache-2:11211", "cache-3:11211"]); function getUser(id: string): Promise<User> {  return new Promise((resolve, reject) => {    cache.get(`user:${id}`, async (err, data) => {      if (err) return reject(err);      if (data) return resolve(JSON.parse(data));      const user = await db.users.findById(id);      cache.set(`user:${id}`, JSON.stringify(user), 300, () => resolve(user)); // 5 min TTL    });  });}
09

Complexity and performance

get / setO(1)

Sub-millisecond.

Node failure impact~1/N of keys miss

With consistent hashing.

10

Trade-offs

Simplicity vs features

Memcached is easy to operate but lacks data structures, persistence, and replication.

Cold cache after failure

Lost nodes mean misses until the cache warms up; protect the database with request coalescing.

11

Variants and related techniques

mcrouter

Facebook's proxy for routing, replication, and failover across Memcached pools.

12

Common mistakes

  • Storing data that cannot be rebuilt.

    Fix: Memcached is not durable.

  • Thundering herd on popular key expiry.

    Fix: Use leases or request coalescing and TTL jitter.

13

Interview questions

When would you pick Memcached over Redis?

For a large, simple, multithreaded cache of opaque values where persistence and data structures are unnecessary, and simplicity at scale matters.

What happens when a Memcached node dies?

Its keys become misses and are reloaded from the database; with consistent hashing only that node's share of keys moves.

14

Practice problems

ProblemDifficultyWhat it trains
Compare Memcached and Redis for a session storeEasyDurability needs.
Design a caching layer for 1M reads/sHardPools, hashing, herd protection.