AlgoViz

Memcached

medium 30 min

Memcached is a small, fast, shared cache for plain values.

See it run

Fixed chunk sizes, eviction inside a class, clients that choose the server, and what a miss costs.

Operations

Store an item

How a 139-byte item finds its chunk.

Step 1 of 5

Four classes

pagesusedfreeevicted
96 B1400069220
120 B0000
152 B0000
192 B150004610

Two pages are in use, one for the smallest class and one for the largest shown. No class is full.

Step 1: Four classes

need = 48 + 8 + len(key) + 1 + len(value) + 2cls = smallest_class(need)if cls.has_free():      chunk = cls.take()elif pages_left():      cls.cut(new_page()); chunk = cls.take()else:                   chunk = cls.evict_oldest()chunk.write(key, value)
Cost
Constant
Watch out for
Waste per chunk
Exact sizes
no waste, then holes

Small values feel the overhead most. 56 bytes of bookkeeping around a 10-byte value is most of the memory.

In the real world

The same memcached doing real jobs. Pick one and step through it.

The home page entry expires while two servers ask for it.

Step 1 of 4

Both miss

App server 1App server 2Memcachedhome is goneMySQLidle

Two misses

The entry has just expired.

Step 1: Both miss

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Hash modulo the number of servers

    critical

    One more server turns most reads into misses.

    Wrong
    server = servers[hash(key) % len(servers)]
    Right
    server = rendezvous(key, servers)
  • A lock that always succeeds

    critical

    pymemcache does not wait for the answer by default, so add returns True for everyone.

    Wrong
    if client.add("lock:home", "1", expire=10): ...
    Right
    if client.add("lock:home", "1", expire=10,              noreply=False): ...
  • A TTL above 30 days

    major

    The server reads it as a date, which is long ago, so the item expires at once.

    Wrong
    client.set(k, v, expire=60 * 60 * 24 * 60)
    Right
    client.set(k, v, expire=60 * 60 * 24 * 30)
  • Counting with incr only

    minor

    incr on a missing key fails, and two clients that set 1 lose a count.

    Wrong
    if c.incr(k, 1) is None: c.set(k, "1")
    Right
    if c.incr(k, 1) is None and not c.add(k, "1"):    c.incr(k, 1)
  • Open to the network

    critical

    There is no password by default.

    Wrong
    memcached -l 0.0.0.0
    Right
    memcached -l 10.0.0.11   # private, behind a firewall

Interview tips

  • How to explain it

    • Plain values in memory, many threads.
    • Clients choose the server.
    • Fixed chunks, eviction per class.
  • Before production

    • Set -m, -c and a private address.
    • Consistent or rendezvous hashing.
    • TTL on every key.
    • Test the database with an empty cache.
  • Reach for it when

    • You only need get, set and delete.
    • Everything can be rebuilt.
    • You want to use many cores per machine.
  • Be careful when

    • You need structures or copies.
    • Pages need hundreds of keys.
    • Value sizes change over time.