NestJS Caching & Redis
Installation
SKILL.md
Caching & Redis Standards
Caching Strategy
- Layering: Use Multi-Level Caching for high-traffic read endpoints.
- L1 (Local): In-Memory (Node.js heap). Ultra-fast, no network. Ideal for config/static data. Use
lru-cache. - L2 (Distributed): Redis. Shared across pods.
- L1 (Local): In-Memory (Node.js heap). Ultra-fast, no network. Ideal for config/static data. Use
- Pattern: Implement Stale-While-Revalidate where possible to avoid latency spikes during cache misses.
NestJS Implementation
- Library: Use
cache-managerwithcache-manager-redis-yet(Recommended overcache-manager-redis-storefor better V4 support and stability). - Interceptors: Use
@UseInterceptors(CacheInterceptor)for simple GET responses.- Warning: By default, this uses the URL as the key. Ensure consistent query param ordering or custom key generators.
- Decorators: Standardize custom cache keys.