Skip to content

Memcached + mcrouter — production sharding and replication

Route memcached traffic through mcrouter for consistent hashing, pool replication, and rolling cache reconfiguration without restarting clients.

Memcached on its own is a single-node key/value store; it does not shard, it does not replicate, and it does not failover. The standard answer to those gaps is mcrouter — a memcached protocol router originally built by Meta that sits between clients and a pool of memcached nodes. This article is the operational view: how mcrouter shards, how it replicates, what its hot-spare and migration patterns look like, and what we wire into observability before declaring it production.

How to verify

Confirm what your clients currently connect to and whether a router is already in the path. A direct connection to memcached looks the same as a connection to mcrouter from the client side, so check the listener process.

ss -ltnp | grep -E ':(11211|5000)\b'
echo -e 'version\r\nquit\r\n' | nc 127.0.0.1 11211
ps -fC mcrouter
ps -fC memcached
echo -e 'stats\r\nquit\r\n' | nc 127.0.0.1 5000 | head -20

If stats returns STAT version lines, you are talking to memcached directly; if it returns STAT mcrouter_<...>, you are through mcrouter.

What’s happening

Mcrouter speaks the memcached protocol on its client side and routes each request to one or more backend memcached instances based on a configuration tree. The two patterns we use are:

  • Sharded pool — one logical key namespace partitioned across N memcached nodes via consistent hashing. A failed node loses its share of the keyspace but the rest keeps serving. Adding a node reshuffles ~1/N of the keys.
  • Replicated pool — writes go to all replicas, reads pick one (usually the local replica). Useful when failure of one cache region must not produce a cold-start storm.

Mcrouter trees compose these — a pool of pools, with routing rules per prefix. You can ship a config that says “writes to the session: prefix go to the replicated pool, everything else goes to the sharded pool” and reload without dropping client connections.

The router exposes its own stats endpoint (port 5000 by default) reporting per-backend latency, error counts, and queue depth — the signals you need to spot a single misbehaving node before clients notice.

The procedure

  1. Install memcached and mcrouter. Memcached is in Ubuntu; mcrouter is built from source or installed from the upstream package.

    apt update && apt install -y memcached
    systemctl edit memcached    # set -l 0.0.0.0, -m 4096, -t 4
    systemctl restart memcached
    apt install -y libmcrouter-dev mcrouter  # or build from github.com/facebook/mcrouter
    mcrouter --version
  2. Write the routing config. A minimal sharded pool with two replicas per shard.

    {
      "pools": {
        "shard_pool": {
          "servers": [
            "cache-1.internal:11211",
            "cache-2.internal:11211",
            "cache-3.internal:11211"
          ],
          "protocol": "ascii"
        }
      },
      "route": {
        "type": "PrefixPolicyRoute",
        "operation_policies": {
          "set": "AllSyncRoute|Pool|shard_pool",
          "delete": "AllSyncRoute|Pool|shard_pool"
        },
        "default_policy": "PoolRoute|shard_pool"
      }
    }
  3. Start mcrouter, point clients at it. Clients connect to mcrouter on a local port; mcrouter does the fan-out.

    mcrouter --config-file=/etc/mcrouter/config.json \
             --port=5000 \
             --num-proxies=4 \
             --stats-root=/var/spool/mcrouter \
             -L /var/log/mcrouter.log &
    echo -e 'set foo 0 0 5\r\nhello\r\nquit\r\n' | nc 127.0.0.1 5000
    echo -e 'get foo\r\nquit\r\n' | nc 127.0.0.1 5000
  4. Add a hot-spare pool with WarmUpRoute. When the primary cache is cold (after a deploy), serve reads from a warm secondary while writes go to both.

    {
      "route": {
        "type": "WarmUpRoute",
        "cold": "PoolRoute|primary",
        "warm": "PoolRoute|secondary"
      }
    }
  5. Reload without dropping connections. Mcrouter watches the config file and reloads on change.

    mv /etc/mcrouter/config.json /etc/mcrouter/config.json.bak
    cp /etc/mcrouter/config.new.json /etc/mcrouter/config.json
    echo -e 'stats reload_config\r\n' | nc 127.0.0.1 5000
    curl -s http://127.0.0.1:5000/stats | jq .  # if HTTP stats enabled

Common pitfalls

  • Putting mcrouter on each client host vs. running a few central routers — local routers are the right answer for >50 clients (network hops add up); central routers are simpler at small scale. Mix and you regret it.
  • Sharding via the application’s own hash and “letting mcrouter route by IP” — you have two layers of sharding fighting each other.
  • Forgetting that WarmUpRoute warms from the cold pool by reading from warm — the warm pool needs the keys you care about. Cold-warming an empty warm pool just delays the same miss storm.
  • Running memcached without -m and watching it consume the entire host’s RAM; always pin memory budget at startup.
  • Letting mcrouter’s stats endpoint reach the internet — the data is detailed enough to plan an attack.

We deploy memcached + mcrouter for clients running session-style caches and read-through caches in front of relational databases. The cache is one tier of a layered design we tune together — see our notes on the L1/L2 cache pattern. Stack Harbor’s role under managed operations is to own that whole layered stack, not just the box.