# Serverless (https://lazy-layers-cache.vercel.app/docs/setups/serverless)

> L2 caching in short-lived Node.js processes and functions.



Serverless platforms give you a fresh process for every request or a short-lived container that disappears after handling a batch. L1, the in-process cache, lives and dies with that process. By the time the next request arrives, the cache is gone.

That does not make LazyLayers useless. It makes it a different shape. You keep L2, you keep in-flight dedupe, and you keep the event bus. You skip L1, or you treat it as a single-request optimisation rather than a persistent layer.

## What changes [#what-changes]

<CardGroup cols="2">
  <Card title="L1 is gone or ephemeral" icon="bolt">
    No process survives long enough for L1 to warm across requests. Each cold start is truly cold for L1.
  </Card>

  <Card title="L2 is your real cache" icon="database">
    Redis or Cloudflare KV outlives each invocation. A miss checks L2, then your origin. See the [Cloudflare KV setup](/docs/setups/cloudflare-kv) for Workers and Node.js.
  </Card>

  <Card title="In-flight dedupe still works" icon="layer-group">
    Concurrent callers within one invocation share one loader call. That is free, and it matters when a single request fans out to many parallel reads.
  </Card>

  <Card title="The bus connects instances" icon="tower-broadcast">
    A bus lets active instances invalidate their local L1 copies promptly when those copies matter. Deleting a key from a shared Redis L2 does not need a bus.
  </Card>
</CardGroup>

## The setup [#the-setup]

You write two files, not four. There is no `src/cache/bus.js` unless you need cross-instance invalidation, and there is no L1 configuration because there is no persistent process to hold it.

<Steps>
  <Step title="Connect to Redis">
    One connection, one file. The same Redis client that backs L2 also backs the event bus if you use one.

    <CodeGroup>
      ```js title="src/cache/redis.js"
      import Redis from 'ioredis'

      export const redis = new Redis(process.env.REDIS_URL, {
        maxRetriesPerRequest: 2,
        enableReadyCheck: true,
        enableOfflineQueue: true,
      })

      redis.on('error', (err) => {
        console.error('[redis]', err.message)
      })
      ```

      ```ts title="src/cache/redis.ts"
      import Redis from 'ioredis'

      export const redis = new Redis(process.env.REDIS_URL!, {
        maxRetriesPerRequest: 2,
        enableReadyCheck: true,
        enableOfflineQueue: true,
      })

      redis.on('error', (err: Error) => {
        console.error('[redis]', err.message)
      })
      ```
    </CodeGroup>
  </Step>

  <Step title="Build the cache">
    L1 is off. L2 is the only persistent layer. The TTLs are longer because L2 is invalidated directly rather than by expiry.

    <CodeGroup>
      ```js title="src/cache/index.js"
      import { LazyLayersCache, RedisStore } from 'lazy-layers-cache'
      import { redis } from './redis.js'

      export const cache = new LazyLayersCache({
        // No L1. Every read goes to L2 or the origin.
        l1: false,

        // Fallback lifetime for anything that does not set its own.
        ttlMs: 5 * 60 * 1000,

        levels: {
          // Long, because L2 is the only tier and is invalidated directly.
          L2: { ttlMs: 60 * 60 * 1000 },
        },

        l2: new RedisStore(redis, {
          prefix: 'app:cache:',
          deleteStrategy: 'unlink',
          scanCount: 500,
        }),

        // Optional. A bus is useful if other instances keep local L1 copies.
        // eventBus: bus,

        timeouts: { softMs: 800, hardMs: 5000 },
        failSafe: { staleTtlMs: 30 * 1000 },
        negativeCache: { ttlMs: 10 * 1000, maxEntries: 5_000 },
      })
      ```

      ```ts title="src/cache/index.ts"
      import { LazyLayersCache, RedisStore, type LazyLayersCacheOptions } from 'lazy-layers-cache'
      import type { User } from '../types/user.js'
      import { redis } from './redis.js'

      const options: LazyLayersCacheOptions<string, User> = {
        // No L1. Every read goes to L2 or the origin.
        l1: false,

        // Fallback lifetime for anything that does not set its own.
        ttlMs: 5 * 60 * 1000,

        levels: {
          // Long, because L2 is the only tier and is invalidated directly.
          L2: { ttlMs: 60 * 60 * 1000 },
        },

        l2: new RedisStore<User>(redis, {
          prefix: 'app:cache:',
          deleteStrategy: 'unlink',
          scanCount: 500,
        }),

        timeouts: { softMs: 800, hardMs: 5000 },
        failSafe: { staleTtlMs: 30 * 1000 },
        negativeCache: { ttlMs: 10 * 1000, maxEntries: 5_000 },
      }

      export const cache = new LazyLayersCache<string, User>(options)
      ```
    </CodeGroup>
  </Step>

  <Step title="Read and invalidate">
    The same `getOrSet` and `delete` API. A Redis L2 deletion is visible to other Redis clients because they share the same server. If other instances also keep local L1 copies, those copies need an event bus or a short TTL.

    <CodeGroup>
      ```js title="src/users/repository.js"
      import { cache } from '../cache/index.js'
      import { db } from '../db.js'

      export async function getUser(id) {
        return cache.getOrSet(`user:${id}`, async ({ signal } = {}) => {
          const row = await db.users.findById(id, { signal })
          return row ?? undefined
        })
      }

      export async function updateUser(id, patch) {
        const updated = await db.users.update(id, patch)

        await cache.delete(`user:${id}`)

        return updated
      }
      ```

      ```ts title="src/users/repository.ts"
      import { cache } from '../cache/index.js'
      import type { User } from '../types/user.js'
      import { db } from '../db.js'

      export async function getUser(id: string): Promise<User | undefined> {
        return cache.getOrSet(`user:${id}`, async ({ signal } = {}) => {
          const row = await db.users.findById(id, { signal })
          return row ?? undefined
        })
      }

      export async function updateUser(id: string, patch: Partial<User>): Promise<User> {
        const updated = await db.users.update(id, patch)

        await cache.delete(`user:${id}`)

        return updated
      }
      ```
    </CodeGroup>
  </Step>
</Steps>

## Adding cross-instance invalidation [#adding-cross-instance-invalidation]

When serverless instances keep local L1 copies, a Redis L2 deletion does not immediately clear those in-process copies. Add a bus if those copies need prompt peer invalidation. With L1 disabled as above, deleting the shared Redis key is sufficient for the next read from Redis.

<CodeGroup>
  ```js title="src/cache/bus.js"
  import { RedisEventBus } from 'lazy-layers-cache'
  import { redis } from './redis.js'

  export const bus = new RedisEventBus(redis, 'lazylayers.invalidate', {
    retryQueue: { maxSize: 1000 },
    handlerConcurrency: 16,
  })

  await bus.connect()
  ```

  ```ts title="src/cache/bus.ts"
  import { RedisEventBus } from 'lazy-layers-cache'
  import { redis } from './redis.js'

  export const bus = new RedisEventBus(redis, 'lazylayers.invalidate', {
    retryQueue: { maxSize: 1000 },
    handlerConcurrency: 16,
  })

  await bus.connect()
  ```
</CodeGroup>

Then wire it into the cache:

<CodeGroup>
  ```js title="src/cache/index.js"
  import { LazyLayersCache, RedisStore } from 'lazy-layers-cache'
  import { redis } from './redis.js'
  import { bus } from './bus.js'

  export const cache = new LazyLayersCache({
    l1: false,

    ttlMs: 5 * 60 * 1000,

    levels: {
      L2: { ttlMs: 60 * 60 * 1000 },
    },

    l2: new RedisStore(redis, {
      prefix: 'app:cache:',
      deleteStrategy: 'unlink',
      scanCount: 500,
    }),

    eventBus: bus,
    source: process.env.FUNCTION_NAME ?? `fn-${process.pid}`,

    timeouts: { softMs: 800, hardMs: 5000 },
    failSafe: { staleTtlMs: 30 * 1000 },
    negativeCache: { ttlMs: 10 * 1000, maxEntries: 5_000 },
  })
  ```

  ```ts title="src/cache/index.ts"
  import { LazyLayersCache, RedisStore, type LazyLayersCacheOptions } from 'lazy-layers-cache'
  import type { User } from '../types/user.js'
  import { redis } from './redis.js'
  import { bus } from './bus.js'

  const options: LazyLayersCacheOptions<string, User> = {
    l1: false,

    ttlMs: 5 * 60 * 1000,

    levels: {
      L2: { ttlMs: 60 * 60 * 1000 },
    },

    l2: new RedisStore<User>(redis, {
      prefix: 'app:cache:',
      deleteStrategy: 'unlink',
      scanCount: 500,
    }),

    eventBus: bus,
    source: process.env.FUNCTION_NAME ?? `fn-${process.pid}`,

    timeouts: { softMs: 800, hardMs: 5000 },
    failSafe: { staleTtlMs: 30 * 1000 },
    negativeCache: { ttlMs: 10 * 1000, maxEntries: 5_000 },
  }

  export const cache = new LazyLayersCache<string, User>(options)
  ```
</CodeGroup>

## Platform notes [#platform-notes]

### Vercel [#vercel]

Vercel serverless functions are short-lived containers. Each invocation is a separate process with its own heap. L1 would only help within a single invocation, which is too brief to matter.

Set `source` from `process.env.VERCEL_FUNCTION_NAME` or `process.env.AWS_LAMBDA_FUNCTION_NAME` if you are on the AWS-backed runtime.

Redis connections are not reused across invocations on Vercel unless you use a connection pool external to the function. Every cold start opens a new one. This is normal and the library handles it.

### Cloudflare Workers [#cloudflare-workers]

Use the Workers-safe `lazy-layers-cache/cloudflare` entrypoint with a KV binding. The Node.js entrypoint imports native compression packages and cannot be bundled into a Worker. See [Cloudflare KV](/docs/setups/cloudflare-kv) for a Hono Worker and a Node.js REST setup.

L1 is not meaningful in Workers. Each request is isolated, and there is no shared heap.

### AWS Lambda [#aws-lambda]

Lambda functions are containers that may be reused across invocations. L1 **can** persist between warm invocations, but it is unreliable as a cache because Lambda can freeze and resume on a different instance, or evict the container at any time. Treat L1 as a bonus for warm starts, not a guarantee.

Set `source` from `process.env.AWS_LAMBDA_FUNCTION_NAME`. If you use provisioned concurrency, L1 becomes more predictable, but L2 is still the tier you design around.

### Netlify Functions [#netlify-functions]

Same model as Vercel. Short-lived, no persistent process. L2 only, with an optional bus for cross-instance invalidation.

## What you give up [#what-you-give-up]

<AccordionGroup>
  <Accordion title="L1 is not a real layer">
    Without a persistent process, L1 only helps within a single invocation. That is in-flight dedupe, not a cache. The real cache is L2.
  </Accordion>

  <Accordion title="Deletes do not travel without a bus">
    A `delete` on one invocation drops the key from L2 on that Redis connection. Other invocations see the stale entry until the L2 TTL expires. Add a bus if you need immediate cross-instance invalidation.
  </Accordion>

  <Accordion title="Cold starts hit L2 directly">
    Every cold start skips L1 entirely. The first read of every key pays a Redis round trip. With a bus, warm invocations may have the value in L2 from a previous load, but there is no local cache to absorb it.
  </Accordion>

  <Accordion title="Connection overhead">
    Each cold start opens a new Redis connection. On platforms that reuse containers (warm Lambda), the connection may survive, but do not design around it.
  </Accordion>
</AccordionGroup>

## When to move to serverful [#when-to-move-to-serverful]

Move to a serverful setup when any of these is true.

<Steps>
  <Step title="L1 hit rate matters">
    If your read pattern is the same keys repeatedly and you need sub-millisecond reads, a persistent L1 on a long-running process gives you that. Serverless cannot.
  </Step>

  <Step title="You need tight staleness control without a bus">
    On a single server, L1 TTL is your staleness window. On serverless, L2 TTL is, and it is longer because L2 is your only tier.
  </Step>

  <Step title="Connection limits are a problem">
    Many concurrent invocations each opening their own Redis connection can hit limits. A serverful process multiplexes requests over a single connection pool.
  </Step>
</Steps>

## Where to next [#where-to-next]

<CardGroup cols="2">
  <Card title="Single process" icon="server" href="/docs/setups/single-process">
    The smallest serverful setup. L1 only, no Redis, no bus.
  </Card>

  <Card title="Multiple instances" icon="server" href="/docs/setups/multi-instance">
    Add L2 and the bus to a serverful setup, and see what the staleness window becomes.
  </Card>

  <Card title="Production checklist" icon="clipboard-check" href="/docs/setups/production">
    Instance sizing, clustering, graceful shutdown, and the transport tabs in full.
  </Card>
</CardGroup>
