System Design · step by stepDesign a Distributed Cache
Step 1 / 9
RUN IT YOURSELF

LRU eviction, in Python & TypeScript

A cache is bounded, so when it fills up it evicts the least-recently-used entry. Here is an O(1) LRU cache in both languages, running live. Switch tabs, read the comments, edit, and hit Run.

HOW TO READ THE CODE — 4 IDEAS
  1. The cache holds at most capacity entries, ordered from oldest to newest.
  2. Every get/put moves the touched key to the most-recent end (steps 1–2).
  3. When it overflows, drop the entry at the oldest end (step 3).
  4. An ordered map (Python OrderedDict / JS Map) makes all of this O(1).
CPython · WebAssembly
built to be cached, not memorized — make the calls, drop a node, run the gauntlet.
Finished this one? 0 / 28 System Designs done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More System Designs