| name | dockergento-varnish-controller |
| description | Manages Varnish cache activation in Dockergento using hm varnish-on and hm varnish-off commands. Use when testing full-page cache behavior, optimizing performance, or simulating production caching. |
| allowed-tools | Read Grep Glob Bash Edit Write |
| model | sonnet |
Dockergento Varnish Controller
Controls Varnish full-page cache in Hiberus Dockergento environments using hm varnish-on and hm varnish-off commands for testing production-like caching behavior.
When to Use
- Testing full-page cache (FPC) functionality
- Simulating production caching behavior
- Performance testing with cache layers
- Debugging cache invalidation issues
- Validating ESI (Edge Side Includes) blocks
- Testing cache purge mechanisms
- Cache-aware development workflow
- Before deploying caching changes
Requirements
Execution Workflow
-
Assess caching need
- Identify if Varnish testing required
- Check current cache state
- Review Magento Varnish configuration
- Plan testing scenario
-
Enable Varnish
- Run
hm varnish-on
- Wait for routing update
- Verify Varnish is active
- Warm up cache if needed
-
Test caching behavior
- Access pages and check cache headers
- Test cache invalidation
- Verify ESI blocks work
- Check cache hit ratios
-
Disable Varnish
- Run
hm varnish-off
- Return to direct PHP-FPM access
- Verify uncached behavior
-
Analyze results
- Review cache logs
- Check performance metrics
- Document findings
Command Reference
Basic Commands
hm varnish-on
hm varnish-off
curl -I http://localhost | grep -i "X-Varnish\|X-Cache"
hm bash -c varnish varnishd -V
hm bash -c varnish varnishstat -1
Verify Varnish Status
curl -I http://localhost | grep -E "X-Varnish|X-Cache|Age"
curl -I http://localhost 2>&1 | grep "X-Magento-Cache-Debug"
hm bash -c varnish varnishadm backend.list
Implementation Patterns
Pattern 1: Automated Cache Testing Session
#!/bin/bash
set -e
URL="${1:-http://localhost}"
echo "Starting Varnish cache testing session..."
echo "Enabling Varnish..."
hm varnish-on
sleep 3
RESPONSE=$(curl -sI "${URL}")
if ! echo "${RESPONSE}" | grep -q "X-Varnish"; then
echo "ERROR: Varnish not detected"
exit 1
fi
echo "✓ Varnish enabled successfully"
echo ""
echo "Test 1: Initial request (expect MISS)..."
HEADERS=$(curl -sI "${URL}")
echo "${HEADERS}" | grep -E "X-Cache|X-Varnish|Age"
echo ""
echo "Test 2: Second request (expect HIT)..."
sleep 1
HEADERS=$(curl -sI "${URL}")
echo "${HEADERS}" | grep -E "X-Cache|X-Varnish|Age"
echo ""
echo "Test 3: Cache invalidation..."
hm bash php bin/magento cache:flush full_page
sleep 1
echo "Test 4: After flush (expect MISS)..."
HEADERS=$(curl -sI "${URL}")
echo "${HEADERS}" | grep -E "X-Cache|X-Varnish|Age"
echo ""
echo "Varnish Statistics:"
hm bash -c varnish varnishstat -1 | grep -E "cache_hit|cache_miss|n_object"
echo ""
echo "Press Enter to disable Varnish and finish..."
read
echo "Disabling Varnish..."
hm varnish-off
echo "Cache testing completed"
Pattern 2: Performance Comparison
#!/bin/bash
set -e
URL="${1:-http://localhost}"
REQUESTS=100
echo "Performance comparison: With/Without Varnish"
echo "URL: ${URL}"
echo "Requests: ${REQUESTS}"
echo ""
echo "Testing WITHOUT Varnish..."
hm varnish-off
sleep 3
START=$(date +%s%N)
for i in $(seq 1 ${REQUESTS}); do
curl -s "${URL}" > /dev/null
done
END=$(date +%s%N)
DURATION_NO_VARNISH=$(( (END - START) / 1000000 ))
AVG_NO_VARNISH=$((DURATION_NO_VARNISH / REQUESTS))
echo " Total time: ${DURATION_NO_VARNISH}ms"
echo " Average per request: ${AVG_NO_VARNISH}ms"
echo ""
echo "Testing WITH Varnish..."
hm varnish-on
sleep 3
curl -s "${URL}" > /dev/null
sleep 1
START=$(date +%s%N)
for i in $(seq 1 ${REQUESTS}); do
curl -s "${URL}" > /dev/null
done
END=$(date +%s%N)
DURATION_WITH_VARNISH=$(( (END - START) / 1000000 ))
AVG_WITH_VARNISH=$((DURATION_WITH_VARNISH / REQUESTS))
echo " Total time: ${DURATION_WITH_VARNISH}ms"
echo " Average per request: ${AVG_WITH_VARNISH}ms"
echo ""
IMPROVEMENT=$((DURATION_NO_VARNISH - DURATION_WITH_VARNISH))
SPEEDUP=$((DURATION_NO_VARNISH * 100 / DURATION_WITH_VARNISH))
echo "Results:"
echo " Time saved: ${IMPROVEMENT}ms"
echo " Speedup: ${SPEEDUP}%"
echo " Requests/sec without Varnish: $((REQUESTS * 1000 / DURATION_NO_VARNISH))"
echo " Requests/sec with Varnish: $((REQUESTS * 1000 / DURATION_WITH_VARNISH))"
hm varnish-off
Pattern 3: Cache Warmup Script
#!/bin/bash
set -e
BASE_URL="${1:-http://localhost}"
echo "Warming up Varnish cache..."
hm varnish-on
sleep 3
echo "Clearing cache..."
hm bash php bin/magento cache:flush full_page
echo "Warming up homepage..."
curl -s "${BASE_URL}/" > /dev/null
echo "Warming up category pages..."
CATEGORIES=$(hm bash php bin/magento catalog:category:list | grep -oE "http[s]?://[^\s]+")
for url in ${CATEGORIES}; do
echo " ${url}"
curl -s "${url}" > /dev/null
sleep 0.5
done
echo "Warming up product pages..."
PRODUCTS=$(hm bash php bin/magento catalog:product:list --limit 20 | grep -oE "http[s]?://[^\s]+")
for url in ${PRODUCTS}; do
echo " ${url}"
curl -s "${url}" > /dev/null
sleep 0.5
done
echo ""
echo "Cache warmup completed"
echo "Varnish Statistics:"
hm bash -c varnish varnishstat -1 | grep -E "cache_hit|cache_miss|n_object"
Pattern 4: Cache Header Inspector
#!/bin/bash
set -e
URL="${1}"
if [ -z "${URL}" ]; then
echo "Usage: $0 <url>"
exit 1
fi
echo "Inspecting cache headers for: ${URL}"
echo ""
hm varnish-on
sleep 2
echo "=== First Request (Cache MISS) ==="
HEADERS=$(curl -sI "${URL}")
echo "${HEADERS}" | grep -E "HTTP|X-Varnish|X-Cache|Age|X-Magento-Cache|Cache-Control"
echo ""
sleep 2
echo "=== Second Request (Cache HIT) ==="
HEADERS=$(curl -sI "${URL}")
echo "${HEADERS}" | grep -E "HTTP|X-Varnish|X-Cache|Age|X-Magento-Cache|Cache-Control"
echo ""
CACHE_STATUS=$(echo "${HEADERS}" | grep "X-Cache:" | awk '{print $2}')
AGE=$(echo "${HEADERS}" | grep "Age:" | awk '{print $2}')
VARNISH=$(echo "${HEADERS}" | grep "X-Varnish:" | awk '{print $2}')
echo "Summary:"
echo " Cache Status: ${CACHE_STATUS:-Unknown}"
echo " Age: ${AGE:-0} seconds"
echo " X-Varnish: ${VARNISH:-N/A}"
if [ "${CACHE_STATUS}" = "HIT" ]; then
echo " ✓ Page is cached by Varnish"
else
echo " ✗ Page is not cached (check Cache-Control headers)"
fi
Pattern 5: ESI Block Testing
#!/bin/bash
set -e
URL="${1:-http://localhost}"
echo "Testing ESI (Edge Side Includes) blocks..."
hm varnish-on
sleep 3
ESI_ENABLED=$(hm bash php bin/magento config:show system/full_page_cache/esi_enabled)
if [ "${ESI_ENABLED}" != "1" ]; then
echo "WARNING: ESI not enabled in Magento"
echo "Enable with: bin/magento config:set system/full_page_cache/esi_enabled 1"
fi
echo "Fetching page and checking for ESI tags..."
CONTENT=$(curl -s "${URL}")
if echo "${CONTENT}" | grep -q "<esi:include"; then
echo "✓ ESI tags found in response"
echo "${CONTENT}" | grep -o "<esi:include[^>]*>" | head -5
else
echo "✗ No ESI tags found"
fi
echo ""
echo "Varnish ESI Statistics:"
hm bash -c varnish varnishstat -1 | grep -i esi
hm varnish-off
Magento Configuration
Check Varnish Configuration
hm bash php bin/magento config:show system/full_page_cache/caching_application
hm bash php bin/magento config:show system/full_page_cache/varnish/backend_host
hm bash php bin/magento config:show system/full_page_cache/varnish/backend_port
hm bash php bin/magento varnish:vcl:generate > varnish.vcl
Enable Varnish in Magento
hm bash php bin/magento config:set system/full_page_cache/caching_application 2
hm bash php bin/magento config:set system/full_page_cache/varnish/backend_host nginx
hm bash php bin/magento config:set system/full_page_cache/varnish/backend_port 80
hm bash php bin/magento config:set system/full_page_cache/varnish/access_list localhost
hm bash php bin/magento config:set system/full_page_cache/esi_enabled 1
hm bash php bin/magento cache:flush config
Cache Debugging
Check Cache Hit Ratio
#!/bin/bash
hm bash -c varnish varnishstat -1 | awk '
/cache_hit/ { hit=$2 }
/cache_miss/ { miss=$2 }
END {
total = hit + miss
if (total > 0) {
ratio = (hit / total) * 100
printf "Cache Hits: %d\n", hit
printf "Cache Misses: %d\n", miss
printf "Hit Ratio: %.2f%%\n", ratio
} else {
print "No cache statistics available"
}
}'
Monitor Varnish Log
hm bash -c varnish varnishlog
hm bash -c varnish varnishlog -q "ReqURL ~ '/catalog/product/view/'"
hm bash -c varnish varnishlog -q "VCL_call eq HIT"
hm bash -c varnish varnishlog -q "VCL_call eq MISS"
Purge Varnish Cache
curl -X PURGE http://localhost/specific-page
hm bash php bin/magento cache:flush full_page
hm bash -c varnish varnishadm "ban req.url ~ /"
Testing Scenarios
Test 1: Homepage Caching
hm varnish-on
curl -sI http://localhost | grep "X-Cache"
curl -sI http://localhost | grep "X-Cache"
hm varnish-off
Test 2: Category Page Caching
hm varnish-on
CATEGORY_URL="http://localhost/women/tops-women.html"
curl -sI "${CATEGORY_URL}" | grep "X-Cache"
curl -sI "${CATEGORY_URL}" | grep "X-Cache"
hm varnish-off
Test 3: Cache Invalidation
hm varnish-on
curl -s http://localhost > /dev/null
curl -sI http://localhost | grep "X-Cache: HIT"
hm bash php bin/magento cache:flush full_page
curl -sI http://localhost | grep "X-Cache: MISS"
hm varnish-off
Test 4: ESI Block Caching
hm varnish-on
hm bash php bin/magento config:set system/full_page_cache/esi_enabled 1
hm bash php bin/magento cache:flush config
curl -s http://localhost | grep "esi:include"
hm varnish-off
Troubleshooting
Varnish Not Caching Pages
hm bash php bin/magento config:show system/full_page_cache/caching_application
hm bash php bin/magento config:set system/full_page_cache/caching_application 2
curl -sI http://localhost | grep "Cache-Control"
curl -sI http://localhost | grep -E "no-cache|no-store|private"
Cache Not Invalidating
hm bash php bin/magento config:show system/full_page_cache/varnish/access_list
curl -X PURGE http://localhost/
hm bash -c varnish varnishadm ban.list
hm bash -c varnish varnishadm "ban req.url ~ /"
ESI Not Working
hm bash php bin/magento config:show system/full_page_cache/esi_enabled
hm bash php bin/magento config:set system/full_page_cache/esi_enabled 1
hm bash -c varnish cat /etc/varnish/default.vcl | grep esi
hm bash php bin/magento varnish:vcl:generate --export-version=6 > varnish6.vcl
Best Practices
- Keep Varnish disabled during active development (cache confusion)
- Enable for testing cache-related features
- Use cache warmup scripts after enabling
- Monitor cache hit ratios (target >90%)
- Test cache invalidation thoroughly
- Configure appropriate TTL values
- Use ESI for dynamic blocks (cart, customer menu)
- Purge cache selectively (avoid full purges)
- Test before production deployment
- Document caching behavior for team
Common Pitfalls
❌ Don't
- Don't leave Varnish on during development (stale cache)
- Don't deploy Varnish changes without testing
- Don't ignore cache headers (Cache-Control)
- Don't use GET parameters in cached URLs
- Don't cache personalized content (use ESI)
- Don't forget to configure purge ACL
✅ Do
- Do disable Varnish for development
- Do test cache invalidation scenarios
- Do use appropriate Cache-Control headers
- Do implement proper ESI blocks
- Do monitor cache statistics
- Do configure grace mode for resilience
Integration Examples
Development Workflow
#!/bin/bash
echo "Development mode: Varnish disabled"
hm varnish-off
echo "Testing cache behavior..."
hm varnish-on
./scripts/test_varnish_cache.sh
hm varnish-off
CI/CD Integration
test-cache:
script:
- hm varnish-on
- php bin/magento cache:flush
- ./tests/cache_test.sh
- hm varnish-off
Quality Bar
- Varnish enables/disables without errors
- Magento detects Varnish correctly
- Cache headers present in responses
- Cache hit ratio > 90% for static pages
- Cache invalidation works correctly
- ESI blocks function properly
- No cache confusion during development
- Performance improvement measurable
- Cache purge ACL configured correctly
- Team understands when to enable/disable