Skip to content
intermediate Phase 96 · Release Engineering

Cache Warming Strategies

Cache warming strategies for Magento including preloading pages, cache hit optimization, and automated warming pipelines

30m
0 problems
Topic Progress 0%

Cache Warming Fundamentals

Why Cache Warming

After deployment or cache flush, the first requests to each page are slow because caches are cold. Cache warming pre-populates caches before users visit.

Without warming:
  Request 1: 5000ms (cold)
  Request 2: 2000ms
  Request 3: 500ms (warm)

With warming:
  Preload: 30000ms (background)
  Request 1: 500ms (warm)
  Request 2: 500ms (warm)

Magento Cache Types

# List all cache types
bin/magento cache:status

# Enable all caches
bin/magento cache:enable

# Clean cache (invalidates entries)
bin/magento cache:clean

# Flush cache (removes all entries)
bin/magento cache:flush

Page Cache Warming Script

#!/bin/bash
# warm-cache.sh

BASE_URL="https://www.example.com"
PRODUCT_IDS=(1 2 3 4 5 10 15 20 25 30)
CATEGORY_IDS=(3 5 7 10 15)

echo "Warming page cache..."

# Homepage
echo "Warming homepage..."
curl -s -o /dev/null "$BASE_URL/"

# Category pages
echo "Warming category pages..."
for id in "${CATEGORY_IDS[@]}"; do
    curl -s -o /dev/null "$BASE_URL/catalog/category/view/id/$id"
done

# Product pages
echo "Warming product pages..."
for id in "${PRODUCT_IDS[@]}"; do
    curl -s -o /dev/null "$BASE_URL/catalog/product/view/id/$id"
done

# CMS pages
echo "Warming CMS pages..."
curl -s -o /dev/null "$BASE_URL/about-us"
curl -s -o /dev/null "$BASE_URL/contact"

echo "Cache warming complete!"

Key Takeaway

Cache warming pre-populates caches after deployment or flush. Use scripts to preload popular pages like homepage, categories, and products.

Automated Cache Warming

Post-Deployment Warming

# GitHub Actions post-deploy
warming:
  needs: deploy-production
  runs-on: ubuntu-latest
  steps:
    - name: Wait for Deployment
      run: sleep 30
    
    - name: Warm Cache
      run: |
        # Homepage
curl -s -o /dev/null https://www.example.com/
        
        # Top categories
        for id in 3 5 7 10; do
          curl -s -o /dev/null "https://www.example.com/catalog/category/view/id/$id"
        done
        
        # Top products
        for id in 1 2 3 4 5; do
          curl -s -o /dev/null "https://www.example.com/catalog/product/view/id/$id"
        done

Cron-Based Warming

<!-- app/code/Vendor/CacheWarmup/etc/crontab.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <job name="cache_warmup" instance="Vendor\CacheWarmup\Cron\Warmup" method="execute">
            <schedule>*/5 * * * *</schedule>
        </job>
    </group>
</config>
<?php
namespace Vendor\CacheWarmup\Cron;

class Warmup
{
    private $httpClient;
    private $urlProvider;
    
    public function execute()
    {
        $urls = $this->urlProvider->getUrlsToWarm();
        
        foreach ($urls as $url) {
            $this->httpClient->request('GET', $url);
        }
    }
}

Cache Warming Service

<?php
namespace Vendor\CacheWarmup\Service;

class CacheWarmer
{
    private $productRepository;
    private $categoryFactory;
    private $pageRepository;
    
    public function warmAll(): void
    {
        $this->warmHomepage();
        $this->warmCategories();
        $this->warmProducts();
        $this->warmCmsPages();
    }
    
    private function warmHomepage(): void
    {
        $this->httpClient->request('GET', $this->storeManager->getStore()->getBaseUrl());
    }
    
    private function warmCategories(): void
    {
        $categories = $this->categoryFactory->create()
            ->addFieldToSelect('url_key')
            ->addFieldToFilter('is_active', 1)
            ->load();
            
        foreach ($categories as $category) {
            $this->httpClient->request('GET', $category->getUrl());
        }
    }
    
    private function warmProducts(): void
    {
        $products = $this->productRepository->getList(
            $this->searchCriteria->create()
        )->getItems();
        
        foreach ($products as $product) {
            $this->httpClient->request('GET', $product->getUrl());
        }
    }
}

Key Takeaway

Automate cache warming with post-deployment scripts, cron jobs, or services. Warm homepage, categories, products, and CMS pages.

Cache Hit Optimization

Cache Hit Ratio Monitoring

# Check FPC hit ratio
bin/magento cache:status | grep page_cache

# Varnish hit ratio
varnishstat -f MAIN.cache_hit
varnishstat -f MAIN.cache_miss

# Redis hit ratio
redis-cli info stats | grep keyspace_hits
redis-cli info stats | grep keyspace_misses

Improving Hit Ratio

# Enable full page cache
bin/magento config:set system/full_page_cache/caching_application 2

# Configure TTL for different pages
bin/magento config:set dev/caching/ttl_configuration/catalog_category 86400
bin/magento config:set dev/caching/ttl_configuration/catalog_product 86400

Cache Tag Optimization

// Use specific cache tags instead of broad ones
$cache->save($data, $key, ['catalog_product_123']);

// Avoid clearing entire cache
$cache->clean(); // Bad - clears everything
$cache->remove('key', ['tag']); // Good - removes specific entry

Varnish Configuration

# Increase TTL for static content
sub vcl_backend_response {
    if (beresp.url ~ "\.(css|js|jpg|png)$") {
        set beresp.ttl = 7d;
    }
    if (beresp.http.Content-Type ~ "text/html") {
        set beresp.ttl = 15m;
    }
}

# Enable grace for stale content
sub vcl_backend_response {
    set beresp.grace = 24h;
}

Key Takeaway

Monitor cache hit ratios with Varnish and Redis stats. Improve with proper TTL settings, specific cache tags, and grace periods for stale content.

Quiz

1. Why is cache warming important after deployment?

Question 1 options

2. Which pages should be warmed first?

Question 2 options

3. What is cache hit ratio?

Question 3 options

4. What does Varnish grace period do?

Question 4 options

5. How often should cache warming cron run?

Question 5 options

Flashcards

Question

What is cache warming?

Answer

Pre-populating caches with content before users visit to avoid cold cache performance impact

Question

Which pages to warm?

Answer

Homepage, top categories, popular products, and CMS pages

Question

How to check Varnish hit ratio?

Answer

varnishstat -f MAIN.cache_hit and MAIN.cache_miss

Question

What is cache hit ratio?

Answer

Percentage of requests served from cache vs total requests

Question

Why use specific cache tags?

Answer

Avoid clearing entire cache when invalidating specific entries

Question

What is Varnish grace period?

Answer

Time window to serve stale content while backend fetches fresh content

Revision Notes

Key Takeaways

  • 1. Cache warming pre-populates caches after deployment or flush
  • 2. Warm most visited pages first: homepage, categories, products
  • 3. Automate warming with cron jobs or post-deployment scripts
  • 4. Monitor cache hit ratios with Varnish and Redis stats
  • 5. Use specific cache tags to avoid broad cache clearing

Interview Tips

  • Explain cache warming strategy and implementation
  • Discuss cache hit ratio optimization techniques
  • Describe Varnish grace period configuration
  • Compare different cache invalidation approaches

Cheat Sheet

Cache Warming

Pages to Warm:
Homepage, categories, products, CMS

Methods:

  • Post-deployment scripts
  • Cron jobs (every 5-15 min)
  • CacheWarmer service

Monitoring:

  • varnishstat for Varnish hits
  • redis-cli info stats for Redis

Optimization:

  • Specific cache tags
  • Grace periods
  • Proper TTL settings