The cache-aside pattern is a caching strategy where application code checks the cache before querying the primary data source. If the data is found (cache hit), it is returned immediately. If not (cache miss), the data is fetched from the source, stored in the cache, and then returned.

Implementation in NestJS

All ingested sources implement cache-aside manually:

const cached = await redisService.get(key);
if (cached) return JSON.parse(cached);
 
const data = await repository.find(query);
await redisService.set(key, JSON.stringify(data), ttl);
return data;

The CacheInterceptor automates this pattern for HTTP response caching — it intercepts @Get() requests, checks the cache, and populates it on misses without manual code.

TTL considerations

Typical TTL values by use case:

  • Reset tokens: 10 minutes
  • Product/product listing data: 30 minutes
  • Stable reference data: 1 day

Setting appropriate TTL balances freshness against cache hit rate. Too short defeats caching; too long risks stale data.