Caching with Redis
Caching with Redis
Caching is one of the most impactful performance techniques available to a backend engineer. Instead of recomputing or re-fetching data on every request, a cache stores the result and returns it instantly for subsequent calls. NestJS ships with a first-class CacheModule built on top of cache-manager, and with Redis as the backing store you get a distributed, persistent cache that survives restarts and scales across multiple application instances.
Installing dependencies
cache-manager-ioredis-yet is the modern Redis store adapter for cache-manager v5+. It uses ioredis under the hood, which is the recommended Redis client for Node.js in production.
Registering CacheModule with Redis
Import CacheModule in your AppModule (or any feature module). Use CacheModule.registerAsync() to pull Redis connection details from ConfigService:
CacheModule. For large apps this is almost always what you want.
The CacheInterceptor — zero-boilerplate route caching
NestJS provides CacheInterceptor which automatically caches the entire response of a controller method. Apply it at the controller or method level with @UseInterceptors, or bind it globally via the APP_INTERCEPTOR provider:
@CacheKey pins the cache entry to a fixed string key regardless of URL parameters. @CacheTTL overrides the module-level default for that handler only.
Manual cache operations via CACHE_MANAGER
For fine-grained control — writing, reading, or deleting individual keys — inject the cache manager token directly:
Cache invalidation strategies
- TTL-based expiry — the simplest strategy; the cache expires automatically after N seconds. Suitable when slightly stale data is acceptable (e.g., product listings).
- Event-driven invalidation — call
cache.del(key)explicitly when the underlying data changes (update/delete). Ensures the next read fetches fresh data immediately. - Namespace / tag-based invalidation — prefix related keys (e.g.,
products:*) and iterate to delete a group. Useful for cache-busting an entire resource family at once. - Write-through — update the cache and the database in the same operation so the cache is never stale; slightly more complex but guarantees consistency.
orders:${userId}).
keyPrefix in the store config (e.g., 'myapp:') so your keys are namespaced away from other applications or services that share the same Redis instance.
Summary
CacheModule with a Redis store gives NestJS apps a distributed, persistent cache. CacheInterceptor handles automatic route-level caching; @CacheKey and @CacheTTL tune individual endpoints. For manual operations inject CACHE_MANAGER and call get, set, and del. Always choose an invalidation strategy — TTL, event-driven, or write-through — appropriate to your consistency requirements, and keep user-specific data behind user-scoped keys.