Caching
In-memory, distributed, and HTTP caching for web apps.
In-memory (single process)
- ★ lru-cache — battle-tested LRU; the default for in-process Node caches.
@isaacs/ttlcache— TTL-only sibling library.- quick-lru — small, fast LRU.
node-cache— older, simpler.
Multi-tier / multi-driver
- ★ unstorage (UnJS) — universal storage layer; drivers for memory, Redis, Cloudflare KV, S3, R2, Vercel KV, browser, fs. Default for "swap backends without changing app code." Used inside Nuxt / Nitro.
- ★ keyv — universal key-value abstraction; drivers for everything; smaller surface than unstorage.
- cache-manager — multi-tier (e.g. memory + Redis); older but solid.
Distributed (Redis-style)
- ★ Upstash Redis — serverless Redis with HTTP API; works on Workers / Lambda / edge. Generous free tier.
- DragonflyDB — drop-in Redis replacement; faster, multi-threaded.
- Redis itself — boring, durable, ubiquitous.
- KeyDB — multi-threaded Redis fork.
- Cloudflare KV — eventually consistent edge KV; good for rare-write / many-read configs.
- Cloudflare Cache API + Cache Reserve — HTTP cache from inside Workers.
- Vercel KV — wraps Upstash for Vercel.
HTTP caching (the underrated kind)
- ★
Cache-Control+ETag+ a CDN — solves 80% of caching needs without a library. - stale-while-revalidate —
Cache-Control: max-age=60, stale-while-revalidate=86400. Standard, wide support. Cache-Control: no-store, private, public, max-age=…— get the directives right; tools likehttp-cache-semanticscan help.s-maxage— only the CDN caches; browsers ignore.
Framework / data-layer caches
- Next.js
unstable_cache/fetchcache — automatic per-request memoization + revalidation. - TanStack Query — client-side data cache (see Data Fetching).
- SWR — client cache.
- Drizzle / Prisma: nothing built in; layer your own using unstorage / keyv.
CDN-level
- Cloudflare Cache Reserve + Cache API — push cache deep at the edge.
- Vercel Edge Cache + ISR — built into Next.js on Vercel.
- Fastly Compute@Edge — programmable cache.
Patterns to know
- Cache aside — read cache, miss → DB → set cache. Default.
- Write-through — every write updates cache. Avoids staleness, slows writes.
- Stale-while-revalidate — return stale, refresh in background. Best UX for non-critical data.
- Negative caching — cache 404s briefly to avoid hammering origin on missing keys.
- Single-flight / coalescing — when 1000 concurrent requests miss the cache, only one calls origin. Use
p-memoizeor@isaacs/abort-controllerpatterns.
Pick this if…
- In-process Node cache: lru-cache.
- Universal driver-swappable cache: unstorage or keyv.
- Distributed cache, edge-friendly: Upstash Redis.
- Distributed cache, traditional Node: Redis or DragonflyDB.
- Just want HTTP cache to work: Cache-Control + ETag + a CDN; don't reach for a library.
- All-Cloudflare: Cache API + KV + Cache Reserve.