Skip to the guide

Redis and Memcached

IntermediateConcept3 pages

These are the two in-memory stores behind most caches. Interviewers expect you to pick one with a reason, and to know what Redis promises when it's used as more than a cache.

The lockers and the league desk

A sports centre keeps things for members in two places. Along one wall are lockers in three sizes, used by many people at once and never looked inside. When every small locker is full, the small one unused longest is emptied, even if big ones stand free. At the front, one quick person at the league desk serves one member at a time. She keeps the league tables, the tally of games, the waiting list for courts and who's in today. Every second she copies the changes into a book in case of a power cut. A spare desk across the hall copies them a moment later, ready to take over.

  • the members → the services that call the cache
  • the lockers → Memcached: many threads, plain values it never reads
  • three locker sizes → its slab allocator: memory cut into fixed-size chunks
  • one person at the desk → Redis running commands on one main thread
  • tables, tallies, waiting list, sign-in list → Redis's data structures: sorted sets, counters, lists, sets
  • the book → persistence to disk; the spare desk → a replica