Caching

Lesson 12 of 14 · 22 min

x
86%

Multi-Level Caching & Cache Warming

Production systems rarely use a single cache layer. Multi-level caching stacks an L1 in-process cache (microseconds) in front of an L2 distributed cache like Redis (milliseconds) in front of the database (tens of milliseconds). Each level catches what the one below misses. Cache warming preloads the cache before traffic arrives — critical after a deploy, cache flush, or cold start. A warming script loads the top 1000 products, popular articles, or configuration into Redis before flipping traffic. Without warming, the first wave of users after a deploy experiences a storm of cache misses. Database-level caching is often overlooked. PostgreSQL's shared_buffers (buffer pool) caches table pages in RAM. MySQL's InnoDB buffer pool does the same. ORM-level second-level caches (Hibernate, EF Core) cache entity objects in-process. These are automatic — but tuning shared_buffers and understanding when the buffer pool is thrashing is part of cache performance engineering.

Before
Cold start — every request is a cache miss
// After deploy or redis FLUSHALL
// First 10,000 users all hit the database
app.get('/products/featured', async (req, res) => {
  const products = await db.query(
    'SELECT * FROM products WHERE featured = true'
  );
  res.json(products);
});
After
Cache warming on startup
// Warm cache before accepting traffic
async function warmCache() {
  const featured = await db.query(
    'SELECT * FROM products WHERE featured = true'
  );
  await redis.setex('products:featured', 3600, JSON.stringify(featured));

  const topIds = await db.query(
    'SELECT id FROM products ORDER BY views DESC LIMIT 1000'
  );
  for (const { id } of topIds) {
    const product = await db.query('SELECT * FROM products WHERE id = $1', [id]);
    await redis.setex(`product:${id}`, 3600, JSON.stringify(product));
  }
  console.log('Cache warmed with', topIds.length, 'products');
}

await warmCache();
// Now start the server

Key Takeaway

Stack L1 in-process + L2 Redis + database buffer pool — warm the cache before traffic hits to avoid post-deploy miss storms.

PreviousNext Lesson