Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Caching

System Design HLD-05.06

Distributed caches: Redis and Memcached

Shared in-memory caches: Memcached's simplicity, Redis data structures, persistence and replication, Redis Cluster hash slots, and when to choose which.

Intermediate 14 min Last reviewed

The big idea

An in-process cache lives inside one server, so with twenty servers you get twenty separate caches, each warming up on its own and each with its own copy of the data. A distributed cacheA cache that runs as its own service, shared by all application servers over the network, usually spread across several nodes. runs as a separate service that every application server shares: one copy of each value, warmed once, holding far more data than any single server's memory. Redis and Memcached are the two classic choices.

Both keep data in memory and answer in well under a millisecond inside a data center. They differ in ambition. Memcached is a deliberately simple, very fast cache of strings. Redis is a data structure server: besides strings it offers lists, sets, sorted sets, hashes, streams and atomic operations on them, plus optional persistence and built-in clustering. That makes Redis useful far beyond caching, for leaderboards, rate limiters, queues, sessions and locks.

Think of Memcached as a huge, fast coat check: hand over a coat, get a ticket, swap them back. Redis is a coat check that can also keep things sorted, count them and keep a waiting list.

How it works

Step 1: Memcached

Memcached stores keys and opaque values in memory with a TTL and evicts with an LRU policy when full. It is multi-threaded, so one node can use many cores. It has no persistence and no built-in replication: if a node restarts, its data is gone, which is acceptable for a pure cache. To spread data across nodes, the clients hash each key to choose a node, usually with consistent hashing ( Consistent hashing ), so adding a node moves only a fraction of keys.

Step 2: Redis data structures

SET session:8f2c "{...}" EX 1800          # string with a 30 minute TTL
INCR page:home:views                       # atomic counter
LPUSH queue:emails "job-1"                 # list as a simple queue
SADD post:9:likes user:42                  # set of unique members
ZADD leaderboard 1520 "ada"                # sorted set: score-ordered
ZREVRANGE leaderboard 0 9 WITHSCORES       # top 10 in one call
HSET user:42 name "Ada" plan "pro"         # hash: fields of one object

Because each command runs atomically on the server, many coordination problems become one-liners: a rate limiter with INCR plus a TTL, a leaderboard with a sorted set, a lock with SET key value NX PX 30000. Redis executes commands on a single main thread, which makes every command atomic but means one slow command, such as fetching a huge key, blocks everything else on that node.

Step 3: Persistence and replication

Redis can persist data in two ways: RDB snapshots write the whole dataset to disk periodically, compact but losing recent writes on a crash; AOF (append-only file) logs every write and replays it on restart, more durable at some cost. Each primary can have replicas that copy its data asynchronously, and a failover system promotes a replica if the primary dies. Asynchronous replication means a failover can lose the last few writes, so Redis is usually not the only home for data you cannot lose.

Step 4: Redis Cluster and hash slots

Redis ClusterRedis's built-in sharding: the key space is split into 16,384 hash slots, each owned by one primary node with its replicas. splits the key space into 16,384 hash slotsOne of 16,384 buckets in Redis Cluster; a key's slot is CRC16 of the key modulo 16,384.. Each primary node owns a range of slots, and a key's slot is CRC16(key) mod 16384:

Redis Cluster routing a key to its hash slot and node A client computes the slot of the key user:1000 as 1649. Slots 0 to 5460 belong to node A, 5461 to 10922 to node B, and 10923 to 16383 to node C, each with a replica. The request goes to node A. Client CRC16(key) % 16384 user:1000 -> slot 1649 Node A slots 0-5460 Node B slots 5461-10922 Node C slots 10923-16383 replica A replica B replica C Figure 1. The client hashes the key to slot 1649, which node A owns, and sends the command straight there. Moving slots between nodes is how a cluster grows or rebalances; each primary has a replica for failover.
hash-slots.pyruns with python3 (standard library)
def crc16(data: bytes) -> int:
    """CRC16-CCITT (XModem), the checksum Redis Cluster uses for key slots."""
    crc = 0
    for byte in data:
        crc ^= byte << 8
        for _ in range(8):
            crc = ((crc << 1) ^ 0x1021) if crc & 0x8000 else crc << 1
            crc &= 0xFFFF
    return crc

def key_slot(key: str) -> int:
    # Hash tags: if the key contains {...}, only that part is hashed,
    # so related keys can be forced onto the same slot (and node).
    start = key.find("{")
    if start != -1:
        end = key.find("}", start + 1)
        if end > start + 1:
            key = key[start + 1:end]
    return crc16(key.encode()) % 16384

NODES = [(0, 5460, "node A"), (5461, 10922, "node B"), (10923, 16383, "node C")]

def node_for(slot):
    return next(name for lo, hi, name in NODES if lo <= slot <= hi)

for key in ["foo", "somekey", "user:1000", "user:1001", "{user:1000}:cart", "{user:1000}:orders"]:
    slot = key_slot(key)
    print(f"{key:20} slot {slot:5}  -> {node_for(slot)}")

Output

foo                  slot 12182  -> node C
somekey              slot 11058  -> node C
user:1000            slot  1649  -> node A
user:1001            slot  5712  -> node B
{user:1000}:cart     slot  1649  -> node A
{user:1000}:orders   slot  1649  -> node A

The slots for foo and somekey match the values in the Redis documentation. Note the last two keys: with a hash tagA part of a Redis key in curly braces; only that part is hashed, so keys sharing a tag land in the same slot. in braces, only user:1000 is hashed, so the cart and orders keys land in the same slot as each other. Multi-key operations and transactions in Redis Cluster only work on keys in the same slot, so hash tags are how you group related keys deliberately.

Going deeper

Big keys and hot keys

Because a Redis node is single-threaded per command, one huge value (a list with millions of items) or one extremely popular key can stall a node. Keep values small, split large collections, avoid commands that scan everything (KEYS *) in production, and handle hot keys as in Thundering herd, cache stampede and hot keys . Stock exchanges make the same trade-off on purpose, one thread per shard with everything in memory and no locks, as shown in single-threaded order matching engine design .

Cache or database?

Redis with AOF and replication is durable enough for some workloads, and teams do use it as a primary store for sessions, counters and queues. But memory is expensive, failover can drop recent writes, and querying is limited to what the data structures offer. Treat it as a database only when you have chosen those trade-offs deliberately.

Trade-offs

OptionWins whenLoses when
Memcached Large, simple string caches; multi-threaded throughput per node; nothing to persist Rich data structures, persistence and built-in replication
Redis Caches plus data structures: sessions, counters, leaderboards, rate limits, queues, locks Very large plain caches where its features go unused; per-command single thread
In-process cache The hottest tiny data and hot keys, at memory speed Sharing across servers and consistency

Common mistakes

  • Running KEYS * or fetching enormous values in production, blocking a Redis node.
  • Treating Redis as durable storage without configuring persistence and accepting async replication's data loss window.
  • Using multi-key commands across slots in Redis Cluster and getting cross-slot errors.
  • Modulo hashing keys across Memcached nodes, so adding one node invalidates most of the cache.

Check yourself

  1. Which is a good fit for a Redis sorted set?

    Show answer

    B. A real-time leaderboard ordered by score Sorted sets keep members ordered by score and return the top N in one command.

  2. How many hash slots does Redis Cluster use?

    Show answer

    C. 16,384 Keys map to one of 16,384 slots via CRC16 modulo 16,384.

  3. Why use a hash tag like {user:1000}:cart?

    Show answer

    B. To force related keys into the same slot so multi-key operations work Only the tagged part is hashed, so keys sharing a tag share a slot and node.

  4. What happens to a Memcached node's data when it restarts?

    Show answer

    B. It is lost; the cache refills from the database Memcached has no persistence; it is purely a cache.

Recap

  • A distributed cache is a shared, separate in-memory service for all app servers.
  • Memcached is a simple, multi-threaded string cache with client-side sharding.
  • Redis adds data structures, atomic commands, persistence (RDB, AOF) and replication.
  • Redis Cluster maps keys to 16,384 hash slots; hash tags keep related keys together.
  • Avoid big keys and blocking commands, and choose durability deliberately.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy

Next up