LazyLayersv9.0.0

FAQ

Short answers about lazy-layers-cache: multi-layer caching, Redis, stampedes, invalidation across servers, Lua scripts, Cloudflare Workers, outages, eviction, memory, speed and upgrading to 9.0.

Short answers to the questions people ask most about lazy-layers-cache 9.0.0. Every number comes from BENCHMARKS.md, which says how each was measured. For a side-by-side look at other libraries, see the comparison.

Questions

What is lazy-layers-cache?

An open-source multi-layer cache for Node.js and Cloudflare Workers. You stack layers nearest first (process memory, more memory tiers, Redis, Cloudflare KV, a Durable Object tier or a store you write), read through all of them with one getOrSet call, and an event bus keeps every server's copy correct after a write. Concurrent callers for a missing key share one load, in a process and across a fleet. It is MIT-licensed, written in TypeScript, and has one runtime dependency, msgpackr.

What is a multi-layer cache, and why use more than two layers?

A multi-layer cache keeps data in several stores ordered from nearest to farthest. Two layers (memory, then Redis) suit most services. A third helps when the hot set is larger than you want to keep as live objects: a small memory() layer holds the hottest keys without decoding, a larger memory({ name: 'warm', maxMemory: '256MB' }) layer holds compact bytes, and Redis shares everything between servers. Each layer has its own eviction policy, byte limit, TTL cap, deadline and circuit breaker, so a slow layer is skipped instead of slowing every read.

Do I need Redis?

No. In one process, setupCache({ namespace, redis: false }) runs an in-memory L1 only. For several instances, use Redis as L2 with Redis Pub/Sub, Cloudflare KV, or lazy-layers-cache/coordination, which keeps locks, counters, rate limits and invalidation in Cloudflare Durable Objects or MongoDB.

How does it prevent cache stampedes?

Concurrent getOrSet callers for the same missing key share one loader call in the process, and with Redis L2 a per-key lease lets one instance load while the others wait to be woken. In BENCHMARKS.md, 10,000 concurrent callers on one cold key ran the loader once.

How do other instances learn that a value changed?

Over an event bus: Redis Pub/Sub, RabbitMQ, NATS Core, NATS JetStream, Durable Object RPC or MongoDB change streams. invalidate, invalidateByPattern and invalidateTags publish a delete that every peer applies to its own L1, and a freshly loaded value can be broadcast so peers do not query the database for it. setupCache caps that broadcast at 32 KiB.

Does it work on Cloudflare Workers?

Yes. lazy-layers-cache/cloudflare is a Worker-safe entry point with a Cloudflare KV store, and lazy-layers-cache/coordination adds Durable Object locks, counters, rate limits, a shared cache tier and invalidation between Worker instances.

What happens when Redis or the event bus fails?

The cache fails open: an unreachable layer becomes a miss and the loader runs, circuit breakers stop repeated calls to the failing dependency, and fail-safe serves a recent value when the loader fails. Invalidations the bus could not deliver are resent once it is back, and keys whose Redis write failed are skipped in Redis until they are repaired, so neither outage leaves an old value in place. In a recorded run, six servers rode out a 4 s Redis freeze with no errors.

Which eviction policy does it use?

S3-FIFO by default, with LRU, LFU, SIEVE, FIFO and custom policies available, chosen per layer. On the synthetic traces in BENCHMARKS.md it had the lowest mean miss ratio of the built-in policies, and on Zipf α = 1.0 traffic the in-memory store hit 78.23% with it, against 73.59% for lru-cache.

How much memory does it use?

L1 is limited by maxEntries (1,000 by default) and by maxMemory, a byte budget shared by every built-in store in the process: 20% of effective memory by default, never more than 75%. It gives memory back when the V8 heap passes 85% of its limit or RSS passes 90% of maxProcessMemory, which defaults to the container or host memory. A small encoded entry retained 76.1 bytes with LRU and 98.4 with S3-FIFO in BENCHMARKS.md.

How fast is it?

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, and a miss costs 2 Redis round trips. Inserting into a full in-memory cache is slower than in lru-cache or quick-lru, because every write measures the value and charges a memory budget.

Which Node.js versions and module formats are supported?

Node.js 22 or later. The package ships ESM, CommonJS and TypeScript declarations.

Does it expose metrics?

Yes. With the observability option it serves Prometheus metrics under the lazycache prefix and a live dashboard protected by a generated token. Every cache operation is also an event you can subscribe to with cache.on().

Does it use Redis Lua scripts?

Yes. Every Redis operation that must be atomic (a read with its TTL, a write that fences a load in flight, taking or releasing a lease) is a Lua script, called by hash. Since 9.0, reads, writes and deletes issued in the same tick run as one script per 64 keys, so a burst of 500 makes 8 script calls instead of 500. You can also register Lua of your own in the scripts option and run it on cache keys with cache.runScript; a script marked writes: true is fenced against loads in flight and drops its keys from every server's memory afterwards.

How do I upgrade from 8.0 to 9.0?

Most applications need no code changes. Run Node.js 22 or later, remove any invalidation you published yourself after set() (it now does that), and stop catching DistributedLockLostError around loads. The wire format is unchanged, so 8.0 and 9.0 servers can share Redis and an event bus during a rolling deploy. MIGRATION.md lists every smaller change.

On this page