Introducing LazyLayers 9.0. Multi-layer caching, intelligent batching and outage recovery.

Explore what’s new

LazyLayers CloudflareUpdated

Cache at the edge.

The same getOrSet in Workers. Workers KV holds the shared values. LazyLayers adds lazy loading, a per-isolate L1, local dedupe and invalidation over Queues.

Three amber Worker isolates, each with a small L1, joined by a rail to a Cloudflare KV drum. ISOLATES → KV

Hono or plain Workers

One cache per request.

Import from lazy-layers-cache/cloudflare: it loads no Redis, NATS or native code, so it bundles for Workers.

worker.ts Workers KV · Queue
import { CloudflareWorkerCache, CloudflareWorkerMemoryStore } from 'lazy-layers-cache/cloudflare'

// Reused across requests in this isolate.
const l1 = new CloudflareWorkerMemoryStore(1_000)

export default {
  async fetch(request, env) {
    const cache = new CloudflareWorkerCache(env.CACHE, {
      prefix: 'users-api:',
      l1,
      invalidationPublisher: queuePublisher(env),
    })
    const user = await cache.getOrSet('user:42', () => loadUser(env, 42))
    return Response.json(user)
  },
}

Who does what

Cloudflare stores. LazyLayers reads through.

ConcernCloudflare providesLazyLayers adds
Shared cacheWorkers KV stores and reads keys across locations.One L2 adapter for Worker bindings and Node.js REST, in the same MessagePack format as Redis.
Lazy loadingKV reads and writes. Your application owns the origin.getOrSet runs your loader on a miss and fills KV and the isolate’s L1 for the next read.
Thundering herdKV has no per-key loader lease.Callers in one isolate share one load. Misses on different isolates can still each load.
InvalidationQueues deliver messages to a consumer, with retries.invalidate and invalidateByPattern publish to the Queue, and the consumer deletes from KV.

Know the limits

What the edge does not give you

  • KV is eventually consistent.

    A write can take time to reach every location. Until then, reads elsewhere may see the old value.

  • No lease across isolates.

    Different isolates can each load the same missing key. For a lock every isolate shares, use the Durable Objects coordination entry.

  • The isolate L1 is small on purpose.

    An isolate has 128 MB of memory in total. The Worker L1 is bounded by entry count, 1,000 by default, and keeps values for 10 seconds by default, so size maxEntries to your values.

  • Queue retries do not reach every L1.

    The consumer clears KV. Other isolates drop their copies when their short L1 TTL ends.

Durable Objects

Coordination every isolate shares.

The coordination entry point keeps locks, counters, rate limits and invalidation in a Durable Object, and can serve as a cache tier. It is the answer when KV’s eventual consistency is not enough.

Read the coordination guide

FAQ

Questions about Workers and KV

Read all FAQs

Does Cloudflare KV give cross-instance stampede protection?

No. KV has no atomic lease, so getOrSet deduplicates loads only within one Worker cache object or one Node.js process. Use Redis L2, or Durable Objects through lazy-layers-cache/coordination, when one load per key across instances matters.

How fresh is data cached in Cloudflare KV?

KV is eventually consistent. A write or delete can stay invisible in another location for 60 seconds or more, so use this setup for data whose freshness budget allows that delay.

Does a Cloudflare Queue invalidate every Worker’s L1?

No. A Queue delivers each message to one consumer, which repeats the KV deletion and retries failures. Each isolate’s L1 ages out by its own TTL.

Does the coordination entry point work in Cloudflare Workers?

Yes. It loads no Redis, NATS, RabbitMQ or native compression code, so it bundles for Workers as it is, and every operation is atomic.