L1/L2 cache system design
Design a hybrid Node.js cache with in-memory L1 and Redis L2: trade-offs, sizing, TTLs, and when each layer is the right fit.
To cache with Redis and an in-memory L1 in Node.js, keep a bounded store inside each process in front of Redis and read through both. lazy-layers-cache's getOrSet checks L1, then Redis, then your loader, and writes the result to both; setupCache({ namespace, redis: { required: true } }) builds that pair together with Redis Pub/Sub, so a delete on one server reaches the others.
Two layers suit most services. To stack more (a hot and a warm memory tier, Redis, Cloudflare KV or a store of your own), see multi-tier layers.
A hybrid L1/L2 cache system puts the same value in two places for different reasons: L1 answers fast local reads, while Redis L2 answers shared reads across a fleet. Pick one cache layer and you have picked a failure mode. The two layers exist because "fast" and "shared" are different questions, and answering them with one piece of infrastructure means answering one of them badly.
One layer, two ways to be unhappy
Memory only. Reads are as fast as a map lookup, and that is the end of the good news. Every server holds its own copy of everything, so a fleet of forty servers holds forty copies and pays for forty cold starts. A restart throws the whole thing away. Each server only ever benefits from its own traffic, so your hit rate falls as you add capacity, which is exactly backwards.
Shared store only. Now there is one copy, it survives restarts, and every server sees the same value. But the key you read four hundred times a second costs a network round trip and a decode every single time, and your read path now fails when Redis does.
Two layers is not a compromise between those. L1 answers "how fast is a repeat read on this server". L2 answers "what does the fleet agree on, and what survives a deploy". Neither answer degrades the other.
L1, the copy in this process
L1 is a bounded store inside your process heap. By default it holds Lazy Layers serializer buffers: a hit costs a map lookup plus a decode, but nothing crosses a socket, and encoding makes retained bytes measurable and prevents caller mutation from changing cached state. With valueMode: 'reference' it holds the objects themselves and a hit skips the decode.
It is created for you when you do not pass l1.
| Dial | Default | What it controls |
|---|---|---|
levels.L1.maxEntries | 1_000 | How many keys L1 holds before its eviction policy removes one |
levels.L1.policy | 's3fifo' | Which key leaves: s3fifo, lru, sieve, fifo, or your own |
levels.L1.valueMode | 'encoded' | Private copies (encoded) or shared objects (reference) |
levels.L1.ttlMs | falls back to ttlMs, then one hour | How long an entry lives regardless of use |
levels.L1.maxMemory | 20% | Shared process-local ceiling for encoded payloads and bounded metadata |
levels.L1.maxProcessMemory | effective memory | RSS the whole process should stay under; the cache shrinks as it nears it |
levels.L1.autoEvict.enabled | true | Whether memory is sampled every 5 s; writes always evict to fit, and always check the heap and RSS |
levels.L1.admission.enabled | true | Whether a new candidate competes with the entries it would displace when space is tight |
Eviction order comes from the policy. Admission is separate: a bounded access history decides whether a new key is worth more per byte than what it would push out. See eviction and value modes.
L1 lives in the process, so it is per server. Three servers means three L1 caches that know nothing about each other. Closing that gap is the job of the event bus, through the lazy fan-out and invalidation, not of L1 itself.
Its blast radius is small, and that is its other virtue. A wrong value in L1 affects one server, expires at its TTL, and dies with the process.
L2, the copy everybody shares
L2 is optional and shared. RedisStore is the implementation in the package, and it stores bytes rather than objects, so values are encoded on the way in and decoded on the way out. Serialization covers the format.
import { LazyLayersCache, RedisStore } from 'lazy-layers-cache'
import { redis } from './redis.js'
export const cache = new LazyLayersCache({
levels: {
// Short in memory, so a wrong value cannot linger on one server.
L1: { maxEntries: 10_000, ttlMs: 60 * 1000 },
// Long in the shared tier, because it is what absorbs the load.
L2: { ttlMs: 60 * 60 * 1000 },
},
l2: new RedisStore(redis, {
prefix: 'app:cache:',
deleteStrategy: 'unlink',
scanCount: 500,
}),
})L2 survives a restart, is visible to every server, and catches what L1 evicts. It costs a round trip, and it can fail. When it does, the failure is treated as a miss rather than an exception, so the read falls through to your loader and the request still completes. Resilience covers what that degradation looks like.
Its blast radius is the opposite of L1's. A wrong value in L2 is served to every server and outlives every restart until something deletes it. That is why writes invalidate L2 explicitly instead of waiting for its TTL to come around.
How a read walks the layers
The promotion step normally makes the second read cheap. A value found in L2 is written into L1 before it is returned when the local budget permits. During pressure it is returned without promotion, so Redis remains available without immediately refilling the hot set.
What each layer is for
| L1 | L2 | |
|---|---|---|
| Holds | encoded bytes | encoded bytes |
| Cost of a hit | a map lookup plus a decode | a round trip plus a decode |
| Scope | one process | the whole fleet |
| Survives a restart | no | yes |
| When it fails | it cannot, it can only evict or expire | treated as a miss, the read continues |
| A wrong value affects | one server, until its TTL | every server, until it is deleted |
Sizing L1 by bytes and entry count
maxMemory is the hard process-local accounting ceiling and defaults to 20% of effective host or container memory. maxEntries remains a second guard against a huge population of tiny keys.
Budget encoded payloads, key and metadata overhead, and fail-safe snapshots. Returned decoded objects and values still owned by application code are outside this ledger; maxProcessMemory, which defaults to effective memory, watches the process as a whole.
| Typical value | Rough size | 10,000 entries | Reasonable ceiling |
|---|---|---|---|
| Session or flag | ~200 B | ~2 MB | 50,000+ |
| User record | ~2 KB | ~20 MB | 10,000 |
| Rendered list page | ~18 KB | ~180 MB | 1,000 |
These values are planning examples. Runtime metrics expose the configured hard cap, adaptive target, accounted bytes, categories, pressure state, and admission outcomes.
ttlMs is a size dial too, and an easier one to reason about. A shorter L1 TTL means fewer entries are alive at any moment, which caps memory by turnover rather than by count.
Under Node clustering, every worker keeps its own L1 and process-local budget. Divide a host allowance across workers or set a container limit for each worker.
TTLs resolve from most specific to least
- per call
options.levels.L1.ttlMsoroptions.levels.L2.ttlMs - per call
options.ttlMs - constructor
levels.L1.ttlMsorlevels.L2.ttlMs - constructor
ttlMs - for L2, the store's own
ttlMs(defaultTtlMs) DEFAULT_CACHE_TTL_MS, which is one hour
The cache passes the TTL it resolved to the L2 store with each write, so the store's own ttlMs applies only when the cache sets none.
A short L1 TTL with a longer L2 TTL is the usual arrangement. Memory turns over quickly, the shared tier keeps absorbing the load, and no single server holds anything for long.
Running with one layer
Either layer can be removed by passing false.
| Configuration | Behaviour |
|---|---|
Neither l1 nor l2 set | L1 only. The default. |
l2 set | L1 and L2, with promotion. |
l1: false | L2 only. Every read crosses the network. |
l1: false, l2: false | No caching. Every getOrSet runs the loader. |
L1 only is the right shape for a single process with no peers, where there is nobody to share with and nothing to invalidate. l1: false is occasionally useful when many processes share one machine and holding the same objects several times over is the thing you are trying to avoid, at the cost of a round trip on every read.
Where to next
Lazy loading
How a value gets into these layers, and how one server's load warms every peer's L1.
Invalidation
What keeps three separate L1 caches from disagreeing with each other.
Single process setup
The L1 only configuration in full, with TTLs chosen and explained.
Questions
Why use an in-memory L1 if I already have Redis?
A Redis read costs a network round trip and a decode on every request, and fails when Redis does. An L1 hit stays in the process: in reference mode a warm getOrSet hit measured 180 ns in BENCHMARKS.md. L2 is what every instance shares and what survives a deploy.
How long should the L1 and L2 TTLs be?
Keep L1 short and L2 long. setupCache sets a 10 second L1 TTL; L2 falls back to the cache-wide ttlMs, then one hour. Since 8.0 the TTL the cache resolves is passed to the L2 store with every write.
How large can L1 grow?
L1 is bounded by maxEntries (1,000 by default) and by maxMemory, a byte budget shared by every built-in store in the process: 20% of effective memory by default, and never more than 75%. It also gives memory back when the heap or the process nears its limit.
Production setup
How setupCache resolves infrastructure, overload protection, health, tuning, and shutdown.
Multi-tier layers
Build a cache from any number of layers: memory tiers with their own eviction policies, Redis, Cloudflare KV and your own stores, with per-layer limits, promotion rules, extensions and diagnostics.