Nuxt & Universal SSR (50 Qs)•Part 3: Advanced Internals, Performance & Production (Senior / Staff)
How do you implement Distributed Cache Invalidation with Nitro Cache?
senior LevelMost AskedCode ExampleFormsPerformanceSystem Design
In Simple Words: When scaling Nuxt horizontally across multiple containers or serverless edge instances, local in-memory caches become out of sync. By configuring Nitro's cached event handlers with a shared Redis driver, cached responses are invalidated across the entire cluster using tags or explicit keys:
typescript
// server/api/products.get.ts
export default defineCachedEventHandler(async (event) => {
return await db.product.findMany();
}, {
maxAge: 60 * 60, // 1 hour
name: 'products-cache',
getKey: (event) => 'all-products'
});
// server/api/invalidate.post.ts
export default defineEventHandler(async () => {
const cache = useStorage('cache');
await cache.removeItem('nitro:handlers:_:products-cache:all-products.json');
return { success: true };
});