CodePark/ questions / Choosing a cache layer
START PRACTICINGPRACTICE
Backend question · Redis & caching › Choosing a cache layer

A NestJS API runs as 4 instances behind a load balancer, each caching user profiles in an in-memory Map. After a user edits their profile, they sometimes see the old version. Why?

Each instance has its own cache, and the update only invalidated the copy on the instance that handled it

Each instance has its own cache. The update only invalidated (removed) the copy on the instance that handled the request. A process-local cache lives inside one process, so other instances can't see it. The other three instances keep serving their stale (old) copies until their entries expire.

Two fixes:

• Use one shared Redis cache for all instances.

• Broadcast the invalidation to all instances with Redis Pub/Sub, so each one removes its copy.


"The load balancer caches responses" is wrong: a load balancer only spreads requests across instances. The stale data comes from each instance's own Map.