Introducing LazyLayers 8.0. Faster reads, fewer Redis round trips, and more control over memory.

Explore what’s new

LazyLayers Distributed cachingUpdated

More servers. Less repeated work.

As your application grows, every instance gets its own local cache. LazyLayers connects those instances through shared storage and an event bus.

Three app instances ask a Redis drum for the lease on one key. The first gets it, loads once from the origin and stores the value. The other two are then woken with that value. LEASE REDIS LEASE ORIGIN · LOADED ONCE

Coordinate refreshes

One server loads. The rest wait.

With Redis L2, a per-key lease lets one instance run the loader while the others wait. They are woken over the event bus when the load finishes, and polling only covers a lost notification. The lease renews itself while a slow loader runs.

Stampede protection in depth
cache.ts Redis
const cache = new LazyLayersCache({
  l2: new RedisStore(redis),
  eventBus: new RedisEventBus(redis, 'cache.invalidate'),
})

// 10,000 callers, one loader call
await cache.getOrSet(`user:${id}`, () => db.users.findById(id))

Share invalidations

Every server keeps its own memory. One bus keeps it current.

Without a bus, other servers serve their old copy until it expires. With one, they hear about the write and drop or replace it. Delivery depends on the bus you choose.

Three app instances with no bus. The first has the new v2 value. The other two keep serving the stale v1 value from their own memory. WRITES v2 STILL SERVES v1 STILL SERVES v1 NO BUSstale until the TTL ends
Three app instances hang from a teal event bus. The first writes v2. The change travels along the bus and drops into the other two, and all three show v2. WRITES v2 APPLIES v2 APPLIES v2 EVENT BUSdel · pattern · set
  • delDrop one key on every server.
  • patternDrop every key matching tenant:42:*.
  • setSend the loaded value, so peers skip the database.
  • Redis Pub/Subat most once
  • NATS Core · JetStreamat most once · durable
  • RabbitMQdurable queues
  • Durable Object RPCno broker
  • MongoDB change streamsgaps reconciled
Read the consistency model

Handle temporary failures

Built for bad days.

Dependencies fail. Here is what each failure does to a request, and the path LazyLayers takes instead.

A read heads for the Redis drum, which is faded with a red cross, and stops. It goes straight to the origin instead, which shows a check and sends the value back. REDIS DOWN ORIGIN ANSWERS

Redis goes down

Requests keep moving

A circuit breaker skips the unhealthy L2 and getOrSet falls through to your loader. Your origin still has to be up.

The load to the faded origin times out. The app instance answers the caller from its amber stale value and shows a check. TIMEOUT LOADER TIMED OUT SERVES THE LAST GOOD VALUE

The loader fails or stalls

Serve the last good value

With fail-safe on, a loader error or timeout returns the retained stale value, 30 seconds past expiry by default, instead of an error.

An event stops at a red broken pipe and waits in an amber queue. When the pipe is joined again, the event reaches the other instance, which shows a check. QUEUED, RETRIED BUS DISCONNECTED

The bus disconnects

Retry brief outages

A bounded queue retries publishes through short event-bus interruptions. Long outages and restarts can still drop events.

Two red v2 events are stopped at a teal gate. A lime v3 event passes to the instance, which shows a check. v2 v2 v3 GENERATION + EVENT ID CHECK

Events arrive late or twice

Drop stale and duplicate events

Generation checks discard old invalidations and event IDs discard replays. This is per key, not a global ordering guarantee.

Failure handling in depth

FAQ

Questions about many servers

Read all FAQs

What is a cache stampede?

A cache stampede, or thundering herd, is many concurrent requests missing the same key at once, for example when a popular key expires or after a deploy, and all sending the same query to the database. The extra load slows the origin, which widens the window and lets more callers in.

Does in-flight deduplication work across several servers?

Not on its own. It is per process, so four servers each make one loader call. With Redis L2, getOrSet also takes a per-key lease, so one instance loads the key and the others wait. With an event bus they are woken when the load finishes. Without one they poll every pollMs.

Is the distributed lease an exactly-once guarantee?

No. It is stampede protection. Redis failover, process pauses and loaders that ignore their abort signal can still allow overlapping origin work, so keep loaders read-only and pass the supplied AbortSignal to your database client.

Should I invalidate before or after the database write?

After it commits. Invalidating first lets a concurrent loader read the old row and cache it again. Invalidation is separate from the database transaction, so an application that must retry a failed invalidation needs durable delivery of its own.

Is there a shared L2 cache with MongoDB?

No. With MongoDB each instance keeps its own L1, and change streams carry invalidations between them. The Durable Object backend does provide a shared cache tier.