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

Explore what’s new

LazyLayers Intelligent evictionUpdated

Memory that knows what to let go.

Give the L1 a byte budget and pick an eviction policy. When the heap or the container gets close to its limit, the cache gives memory back on its own: stale fail-safe copies first, then whatever your policy values least.

An L1 cache drawn as a tray of slots beside a glass column of process memory. Writes drop into free slots and the column rises. When the tray is full, the policy evicts its coldest key to make room. When process memory crosses the 85% heap line, amber stale fail-safe copies leave first, then the policy evicts one more fresh entry, and the column falls back. Captions number each step. 85% heap 1 · WRITES FILL THE BUDGETeach entry is charged its real size 2 · BUDGET FULLthe policy evicts its coldest key 3 · MEMORY PRESSUREthe V8 heap passed 85% of its limit 4 · STALE COPIES GO FIRSTfail-safe snapshots are the cheapest bytes 5 · THEN THE POLICY’S PICKthe least valuable fresh entry 6 · MEMORY GIVEN BACKroom returns once memory is normal L1 · maxMemoryany policy: S3-FIFO · LFU · SIEVE · LRU · FIFO PROCESS MEMORYheap · RSS · container

Limits

Entries and bytes.

Values are charged their real size: the exact encoded bytes by default, or a model of V8’s object layout in reference mode, within 3% of measured heap use on the shapes we test.

Eviction in the docs
cache.ts Node.js
const cache = new LazyLayersCache({
  levels: {
    L1: {
      maxEntries: 50_000,
      maxMemory: '256MB',      // or '10%' of effective memory
      policy: 's3fifo',        // 'lru' | 'sieve' | 'fifo'
      valueMode: 'encoded',    // 'reference' skips the copy
    },
  },
})

Eviction policies

Four policies. S3-FIFO by default.

On skewed traffic (Zipf α = 1.0, 100,000 keys, cache at 10%), S3-FIFO kept 78.23% hits, against 76.64% for quick-lru and 73.59% for lru-cache.

Default

S3-FIFO

New keys wait in a small queue. Only keys read again move to the main queue, and recently evicted keys are remembered as ghosts. Best mean miss ratio in our policy matrix.

Long-term popularity

LFU

Counts reads per key and evicts the least used, with aging so keys that were popular once do not stay forever. Constant time per operation.

Simple, scan-resistant

SIEVE

One queue and a moving hand that skips recently used entries. 78.0% hits on the skewed workload.

Familiar

LRU

Evicts the least recently used entry. Good when recent use predicts the next read.

Cheapest

FIFO

Evicts in insertion order, with no bookkeeping on reads.

Memory pressure

Gives memory back before it runs out.

A shared budget samples memory every five seconds and on writes. With nothing configured, a process nearing its container limit makes the cache shrink.

  • V8 heap

    Pressure at 85% of the heap limit, critical at 95%.

  • Process memory

    Pressure at 90% of maxProcessMemory, which defaults to the container limit or host memory, so Buffers outside the heap count too.

  • Your cap

    maxMemory above 75% of effective memory is lowered to 75%, with a warning.

  • Shrinking

    Under sustained pressure the target drops to 90% of what the caches hold per sample, 70% when critical. Stale fail-safe copies go first.

On Cloudflare Workers the isolate L1 is a separate, smaller store bounded by entry count. See Cloudflare limits.

FAQ

Questions about eviction

Read all FAQs

Why is S3-FIFO the default instead of LRU?

On the policy matrix in BENCHMARKS.md it had the lowest mean miss ratio of the built-in policies, missed up to 14.18 points less than LRU, and was never more than 4.45 points behind it. Choose lru when the whole popular set is replaced at once and fits in the cache.

What hit ratio does it reach on skewed traffic?

On Zipf α = 1.0 traffic over 100,000 keys with room for 10% of them, the in-memory store hit 78.23% with S3-FIFO, against 76.64% for quick-lru, which holds up to twice its configured size, and 73.59% for lru-cache.

Should I use encoded or reference mode?

Encoded, the default, stores bytes and decodes a private copy on every hit, so callers cannot change each other’s values. A warm L1 get() measured 1,141 ns. Reference mode stores the object and returns it, 120 ns in the same benchmark. Use it when callers treat values as read-only.

How much memory does each entry use?

In BENCHMARKS.md, 100,000 small entries retained 76.1 bytes each in encoded mode with LRU and 98.4 with S3-FIFO. Reference mode retained 74.5 and 96.8, and lru-cache 57.1. The difference is the expiry time, charged bytes and payload size each LazyLayers entry keeps, and S3-FIFO’s queue state.