# lazy-layers-cache > lazy-layers-cache is an open-source L1/L2 cache for Node.js and Cloudflare Workers: an in-process L1 in front of an optional shared L2 (Redis, Cloudflare KV or a Durable Object cache tier), read through with `getOrSet`, with stampede protection inside one process and, with a Redis lease, across processes, and invalidation over an event bus. MongoDB can coordinate locks, counters and invalidation, but it is not a shared L2. - Current release: 8.0.0. MIT license. TypeScript, ESM and CommonJS. One runtime dependency (`msgpackr`). Redis (`ioredis`), RabbitMQ (`amqplib`), NATS and native codecs are optional peer dependencies. - Runs on Node.js 20 or 22+ (22+ for NATS) and on Cloudflare Workers through `lazy-layers-cache/cloudflare`. - Install: `npm install lazy-layers-cache`. Read with `getOrSet(key, loader)`. Invalidate with `invalidate(key)`, `invalidateByPattern('tenant:42:*')` or `invalidateTags(['store:7'])`. - Stampede protection: concurrent callers for one key share one loader call in a process. With Redis L2 a per-key lease makes one instance load while the others wait and are woken over the event bus. A miss costs 2 Redis round trips. - Invalidation between instances over Redis Pub/Sub, RabbitMQ, NATS Core, NATS JetStream, Durable Object RPC or MongoDB change streams. A loaded value can be broadcast to warm peers' L1. - Fail-safe stale serving, stale-while-revalidate, early refresh, negative caching, circuit breakers and loader timeouts. - L1 eviction is S3-FIFO by default (LRU, SIEVE, FIFO available). L1 is limited by entries and by bytes, and gives memory back when the heap or the process nears its limit. - Prometheus metrics and a live dashboard protected by a generated token. - Benchmarks, measured on an Apple M5 with Node 24.21 on 2026-11-12 (all in BENCHMARKS.md): a warm read-through hit costs 180 ns in reference mode and 1,610 ns in encoded mode, the default, against 6,490 ns in 0.7.0. 10,000 concurrent callers on one cold key with a 20 ms loader ran it once and were all served in 30.8 ms. On Zipf traffic the in-memory store hit 78.23% with S3-FIFO, against 76.64% for quick-lru and 73.59% for lru-cache. 1,000,000 concurrent callers on one cold key across 10 processes sharing Redis caused one loader call, and the other 999,999 had its value within 206.4 ms (inflight.maxWaiters raised from its 10,000 default). One API process served 1,000,000 HTTP requests at 47,058 per second (4,394 without a cache), with 16 µs of server CPU per request, 286.5 MB peak RSS and 90% fewer database queries (2026-12-03, 0.04% of requests failed on a write/load race tracked in issue #28). ## Agent skill - Install for Claude Code, Cursor, Codex and other agents: `npx skills add Amon20044/LazyLayers`. Source: [skills/lazy-layers-cache/SKILL.md](https://github.com/Amon20044/LazyLayers/blob/main/skills/lazy-layers-cache/SKILL.md) ## Docs - [Introduction](https://lazy-layers-cache.vercel.app/docs): what the cache is and which page answers which task - [Quickstart](https://lazy-layers-cache.vercel.app/docs/quickstart): an in-memory cache, then Redis L2, `getOrSet` and invalidation - [Installation](https://lazy-layers-cache.vercel.app/docs/installation): Node.js versions, ESM, CommonJS, TypeScript and optional peer dependencies - [Prevent cache stampedes](https://lazy-layers-cache.vercel.app/docs/guides/stampede-protection): in-flight deduplication and the Redis lease - [L1/L2 cache design](https://lazy-layers-cache.vercel.app/docs/concepts/layers): an in-memory L1 with Redis L2, sizing and TTLs - [Invalidation](https://lazy-layers-cache.vercel.app/docs/concepts/invalidation): by key, pattern or tag, across instances - [Eviction and value modes](https://lazy-layers-cache.vercel.app/docs/concepts/eviction): S3-FIFO, LRU, SIEVE, memory limits and pressure - [Batches, namespaces and freshness](https://lazy-layers-cache.vercel.app/docs/guides/batches-and-freshness): `getOrSetMany`, `namespace`, `earlyRefresh`, `staleWhileRevalidate`, `ttlJitter` - [Failure handling](https://lazy-layers-cache.vercel.app/docs/guides/failure-handling): fail-open, circuit breakers, stale fallback, timeouts - [Event buses](https://lazy-layers-cache.vercel.app/docs/guides/event-buses): Redis Pub/Sub, RabbitMQ, NATS Core and JetStream, and their delivery guarantees - [Observability](https://lazy-layers-cache.vercel.app/docs/guides/observability): events, Prometheus metrics and the dashboard - [Load testing and sizing](https://lazy-layers-cache.vercel.app/docs/guides/load-testing): 1,000,000 HTTP requests with and without the cache, CPU and memory, and instance sizing - [Cloudflare KV](https://lazy-layers-cache.vercel.app/docs/setups/cloudflare-kv): Workers and Node.js with Hono, Queue invalidation - [Without Redis](https://lazy-layers-cache.vercel.app/docs/setups/coordination): locks, counters, rate limits and invalidation on Durable Objects or MongoDB - [Production setup](https://lazy-layers-cache.vercel.app/docs/setups/production): `setupCache`, Redis ACLs, resolution rules and shutdown - [API reference](https://lazy-layers-cache.vercel.app/docs/reference/api): every method and export - [Configuration reference](https://lazy-layers-cache.vercel.app/docs/reference/configuration): every option and its default - [FAQ](https://lazy-layers-cache.vercel.app/docs/faq): short answers to common questions ## Comparisons - [Compared with other caches](https://lazy-layers-cache.vercel.app/docs/comparison): bentocache, cache-manager, cacheable, cachified, async-cache-dedupe, layered-loader and lru-cache, with features read from each README - [BENCHMARKS.md](https://github.com/Amon20044/LazyLayers/blob/main/BENCHMARKS.md): every number, how it was measured, and where another library is the better choice ## Optional - [Full documentation as one text file](https://lazy-layers-cache.vercel.app/llms-full.txt) - [Changelog](https://github.com/Amon20044/LazyLayers/blob/main/CHANGELOG.md) - [Migration guide from 0.7](https://github.com/Amon20044/LazyLayers/blob/main/MIGRATION.md) - [Source code on GitHub](https://github.com/Amon20044/LazyLayers) - [npm package](https://www.npmjs.com/package/lazy-layers-cache) - [Landing page](https://lazy-layers-cache.vercel.app/)