Most teams throw a cache in front of their database and hope for the best. The wrong caching strategy gives you stale data, thundering herds, or memory that never gets evicted. Here is the decision framework for cache-aside, read-through, write-behind, and write-through, with working Node.js + Redis code for each.
Cache hits are fast, cache misses are slow, and every cache expiration is a synchronized pause for your users. Stale-while-revalidate serves the old data instantly while refreshing in the background, turning every cache operation into a near-zero-latency experience with real working Node.js + Redis code.
When a hot cache key expires, a thousand concurrent requests can hammer the same database query. The singleflight pattern collapses those into a single backend call. Here is a production-grade TypeScript implementation, the edge cases that break naive versions, and the one-line integration that makes it cache-aware.
A cached endpoint quietly serves 50k req/s for weeks, until the key expires and 4,000 simultaneous misses hit Postgres in the same millisecond. Here is the 40 lines of single-flight + probabilistic early refresh that turn cache expiration from a cliff into a soft handoff, with the load-test numbers that prove it.