← Two Hard Things: caching bugs

Two Hard Things: caching bugs · Part 9

new Map() is a memory leak

· by Tony Tang

Memoize getUser with a Map and nothing ever leaves: the process runs out of memory, restarts empty, and the graph climbs again. A sawtooth on your memory dashboard is a cache with no limit. Fix: an LRU with a size limit and a TTL. Memory goes flat.

Comment below: What's the biggest Map in your codebase?

Two Hard Things: a series on the caching bugs you already ship, and how to fix them. "There are only two hard things in computer science: cache invalidation and naming things." (Phil Karlton) All code is real TypeScript on Node, typechecked before it went on screen, and every claim was reproduced against real Redis and Postgres.

More from this series