
Caching Strategies That Actually Work in Production
The Cache Taxonomy Not all caches are the same. Choosing the wrong one is worse than no cache at all. 1. In-Process Memory Cache Fastest possible. Zero network hops. Lives in your Node.js process. import NodeCache from ' node-cache ' ; const cache = new NodeCache ({ stdTTL : 300 , // 5 minutes default checkperiod : 60 , // cleanup every 60s maxKeys : 1000 , // prevent unbounded growth }); async function getUser ( userId : string ): Promise < User > { const cacheKey = `user: ${ userId } ` ; const cached = cache . get < User > ( cacheKey ); if ( cached ) return cached ; const user = await db . users . findUnique ({ where : { id : userId } }); cache . set ( cacheKey , user ); return user ; } Good for: Config data, reference data, per-instance computation. Bad for: Multi-instance deployments (each instance has different state), large datasets. 2. Redis Cache Shared across all instances. Survives deploys (usually). import { createClient } from ' redis ' ; const redis = createClient ({ url :
Continue reading on Dev.to
Opens in a new tab


