Layercache
Production-ready caching for Node.js

Stack memory, Redis, disk, and Memcached behind one compact API with single-flight fetches, tag invalidation, stale serving, and operational metrics.

npm
yarn
pnpm
bun
deno
npm install layercache
LayercacheLayercache

how a read travels

100 concurrent requests. One database call.

read path · 1 of 4

L1 · Memory

~0.1 ms

An in-process LRU answers hot keys without leaving the Node.js process. Most reads stop here and never go deeper.

read path · 2 of 4

L2 · Redis

~1 ms

Shared across every instance. A miss takes a single-flight lease — one caller runs the fetcher while the rest wait for its result.

read path · 3 of 4

L3 · Disk

~5 ms

Survives restarts and serves stale fallback when the layers above are cold or a fetch fails.

read path · 4 of 4

Origin · your database

50+ ms

The layer you're protecting. Watch the stampede: every few seconds 14 concurrent requests fall — exactly one reaches the database.

then, on the way up

The result backfills every layer

One origin call refills disk, Redis, and memory on its way back — so the next read stops at L1 in a tenth of a millisecond.

0requests
100%hit rate
0origin calls

capabilities

Production concerns, already handled.

quick start

Two layers in nine lines.

import { CacheStack, MemoryLayer, RedisLayer } from 'layercache'
import Redis from 'ioredis'

const cache = new CacheStack([
  new MemoryLayer({ ttl: 60_000, maxSize: 1_000 }),       // L1: in-process
  new RedisLayer({ client: new Redis(), ttl: 3_600_000 }),  // L2: shared
])

// Read-through: fetcher runs once, all layers filled
const user = await cache.get('user:123', () => db.findUser(123))

Put a cache stack in front of it.

Apache-2.0 · 672 tests passing · Node.js ≥ 20