Handling Traffic Spikes with Varnish Grace Mode Magento 2

Handling Traffic Spikes with Varnish Grace Mode Magento 2

A simple 300-second setting could save your store from losing thousands in revenue. Varnish Grace Mode Magento 2 keeps serving cached content while your backend recovers.

The article explores the working, considerations, and use cases of Varnish Grace Mode.

VARNISH GRACE MODE

A Simple 300 Second Setting

Could save your store from losing thousands in revenue. Varnish Grace Mode keeps serving cached content while your backend recovers.

$0
Revenue Saved
99.9%
Uptime Maintained
0
Requests Served

Traffic Spike Simulation

Watch how Grace Mode handles sudden traffic surges

Requests/sec Normal Spike Recovery
Backend: Healthy
Cache Hit Rate
98.5%
Grace Hits
0
Response Time
12ms

Key Takeaways

  • Grace mode helps serve cached pages even when Magento is down or slow.

  • TTL and Grace settings control when Varnish delivers stale content.

  • Learn how to handle stale data and set the right grace period.

  • Combine Grace Mode with Redis and Elasticsearch for higher resilience.

  • Brands like Everlast and Graze use Grace Mode during traffic spikes or downtime.

What is Varnish Grace Mode?

Varnish Grace mode is a caching mechanism. It allows Varnish to serve expired cached content when the backend is unresponsive.

Instead of displaying an error or making users wait during high traffic. Varnish delivers earlier cached pages, ensuring better uptime and user experience.

Configure the mechanism using the beresp.grace setting in the VCL file. It allows you to serve stale content for a defined time, even after the Time-to-Live has expired.

Grace mode is useful for sites that experience traffic spikes or maintenance events. It ensures faster load times and availability. You must take care of dynamic content. In Magento 2, combining Grace Mode with ESI blocks enhances efficiency and reliability.

KEY BENEFITS

Why Grace Mode Matters

Transform your store's resilience with intelligent caching that never lets customers down

Zero Downtime

Serve cached pages even when backend fails completely

99.9% uptime maintained

Instant Response

No waiting for slow backends during traffic spikes

50ms avg response time

Revenue Protection

Never lose sales due to backend issues

$0 downtime losses

Implementation Time

Add grace mode to your existing Varnish setup

5 min
Setup time
2 lines
VCL config

How Varnish Grace Mode Works?

ARCHITECTURE

Integration Architecture

See how Varnish Grace Mode works with your complete infrastructure stack

USERS CDN Layer Global Edge Nodes Varnish Cache Grace Mode Active Magento 2 Application Server Redis Session Store Elasticsearch Search Engine MySQL DB Grace Active
CACHE HIT RATE
95.2%
Request Flow
User CDN Varnish
Grace Protection
Active
Backend protected
Response Time
< 50ms
From cache

Stack Benefits

Multi-layer caching for maximum performance
Automatic failover at every layer
Grace mode protects during backend issues
Separate session and search systems

1. TTL and Grace Setup

TTL and Grace Setup

When Varnish caches an object, it defines two values in the vcl_backend_response subroutine:

  • TTL (Time to Live): It is the period during which you consider cached content as fresh. The cached content is valid for 5 minutes.

  • Grace Period: The duration during which you allow Varnish to serve the content. It is after the TTL expires that the Magento backend is slow or fails. After expiry, you can still serve stale content for up to 10 minutes if needed.

2. During Normal Operation

When a user visits a page:

  • If the cached object is still within its TTL period, Varnish serves it.

  • There is no contact with the backend, ensuring ultra-fast response times.

3. When TTL Expires

If the TTL has expired:

  • Varnish attempts to fetch a fresh copy from the Magento backend. It considers the content stale.

  • If the backend is slow and takes longer than expected. Varnish serves the expired content from the cache immediately to the user. It ensures no delay.

  • Meanwhile, it fetches the updated version in the background and refreshes the cache.

4. If Backend Fails

If the backend server is completely down:

  • Varnish checks if the cached content is still within the grace window.

  • If yes, it delivers the stale content as a fallback response.

  • It avoids blank pages or 500 errors for end users.

  • Once the grace period expires, the content is no longer served. It shows an error unless another fallback mechanism is in place.

6 Important Considerations for Varnish Grace Mode

1. Stale Content Risks

Stale Content Risks

Grace mode helps serve stale content when the Magento backend is slow or unresponsive. While this keeps your site live, it also means:

  • Outdated product prices or stock availability.

  • For high-frequency inventory updates or flash sales. It could lead to customer dissatisfaction or overselling.

Keep grace durations short for pages with time-sensitive data. Exclude such pages from caching altogether using cacheable.xml and custom cache rules.

2. Handling User-Specific Data

  • Magento 2 includes dynamic elements like:

    1. Shopping cart totals

    2. Recently viewed items

    3. Customer login status

    4. Wishlists and account links

  • You should not cache them as part of the full page. Use ESI to cache the static page shell while fetching personalized fragments.

  • It allows Grace Mode to serve stale content. They don't expose incorrect or private information.

3. Grace Period Duration

HOW IT WORKS

Grace Period Timeline

Watch how Varnish handles content through TTL expiration and grace periods

TTL Period (5 min) Fresh Content Served Grace Period (10 min) Stale Content Available Expired Error 0:00 Backend: OK
CURRENT STATE
Content is fresh and served directly from cache
TTL (Fresh)
0-5 min
Instant delivery
Grace (Stale)
5-15 min
Backend protected
Expired
15+ min
Must fetch new
Configuration Example
sub vcl_backend_response {
set beresp.ttl = 5m; # Fresh for 5 minutes
set beresp.grace = 10m; # Grace for 10 minutes
}

Choose the right grace duration:

  • Too short, 30s might not help much during real outages.

  • Too long, 30m, increases the risk of users seeing outdated content.

Recommended Range:

  • 2–5 minutes for most catalog and product pages.

  • Lower for dynamic landing pages or promotional banners.

Track how long your backend takes to recover from high loads or minor failures. Set a grace period.

4. Magento Full Page Cache Compatibility

Magento 2 uses its Full Page Cache system that integrates with Varnish. To ensure Grace Mode works:

  • Use Magento’s built-in bin/magento varnish:vcl:generate command to create your base VCL.

  • Ensure that you honor cache tags to enable selective purging.

  • You should not cache pages like the cart and customer/account.

Magento sends headers like Cache-Control and X-Magento-Tags. Configure Varnish to respect and parse these.

5. Test Failover Scenarios

To trust the Grace mode, you must test it under failure conditions:

  • Simulate backend slowness, using sleep() in PHP or stopping PHP-FPM.

  • Observe whether Varnish delivers stale content within the grace window.

  • Ensure that your monitoring tools reflect accurate cache behavior.

Tools for Testing:

  • curl \-I or curl \-w %{time\_total} to measure response behavior.

  • varnishlog to trace backend fetches and grace hits.

6. Monitoring and Logging

Monitoring and Logging

To ensure Grace Mode is working as expected, use:

  • varnishlog: See if grace is being used and backend fetch attempts.

  • varnishstat: Track real-time statistics, such as cache hits and backend failures.

Consider setting up external monitoring tools, such as Prometheus + Grafana. Set up Varnish Enterprise’s dashboard if using the commercial version.

Create alerts for high backend error rates or spikes in grace responses. It helps detect issues early.


Varnish Grace Mode vs Other Caching Strategies

Feature Stack Layer Purpose Key Benefits Limitations Best Fit Scenarios
1. Varnish Grace Mode HTTP Cache Layer Serves stale cached pages when the backend is slow or down High availability during backend outages
Instant failover
Reduces server load
May serve outdated content
Not ideal for dynamic or personalized pages
Traffic spikes
Temporary backend failures
Slow DB/API
2. Redis Sentinel In-Memory Store Provides high availability for Redis-based session and cache storage Automatic failover
Maintains session continuity
Resilient Redis layer
No direct impact on frontend speed
Only applies to Redis availability
Redis master node crash
Session failover support
3. Multi-region CDN Edge Delivery Distributes static and dynamic content across global edge nodes Low latency
DDoS protection
Global scalability
TLS offloading
Complex purging logic
Cache control challenges
International traffic
Performance optimization
Redundancy
4. Magento Built-in FPC Application Layer Caches full-page HTML output within Magento for faster responses Cache tag-aware
Integrated with Magento logic
Supports Varnish backend
Slower than Varnish
No built-in grace fallback
PHP-driven
Page response caching
Dynamic purging by tag
5. Database Clustering Data Layer Offers high availability and scalability at the MySQL/MariaDB level Eliminates DB single point of failure
Supports read/write separation
Complex setup
Doesn't reduce page load times
Read/write scaling
DB resilience
Regional DB access

COMPARISON

Caching Strategy Analysis

Compare different caching approaches for your Magento 2 store

Availability Performance Scalability Cost Complexity Resilience

Varnish Grace Mode

Best for: High traffic sites
Setup time: 5 minutes
Downtime protection: Excellent

Redis Sentinel

Best for: Session storage
Setup time: 30 minutes
Failover: Automatic

Multi-region CDN

Best for: Global stores
Setup time: 2 hours
Coverage: Worldwide
Key Insight

Varnish Grace Mode excels in availability and resilience with minimal complexity. While CDNs offer better global performance, Grace Mode provides the best protection against backend failures for most e-commerce stores.


3 Integration Strategies of Varnish Grace Mode

1. Varnish + Redis Architecture

  • Magento 2 supports Redis out of the box for both session and page cache storage. Varnish helps skip the page cache.

  • Redis stores volatile session data, such as customer login state and form inputs.

  • Varnish handles full-page cache. It uses grace mode to serve stale content if the backend is slow or down.

Benefits

  • It reduces pressure on the Magento database by offloading session storage to Redis.

  • It enables high availability. Varnish can serve pages, and Redis can provide user-specific info. It is even if Magento degrades.

  • It speeds up page delivery and improves fault tolerance.

2. Using Elasticsearch for Search

Using Elasticsearch for Search

  • Magento 2 uses Elasticsearch as the primary catalog search engine.

  • Varnish handles page caching but bypasses search-related traffic. These include layered navigation and live search.

  • Magento’s backend or AJAX calls query Elasticsearch.

Benefits

  • It ensures a fast and accurate product search experience.

  • It separates the search engine load from the Magento backend.

  • It keeps catalog pages available during Elasticsearch failures.

3. CDN + Varnish Grace Mode Architecture

  • CDN, like Cloudflare and Akamai, is set up in front of Varnish.

  • Configure Varnish with grace and keep settings:

  • Magento origin servers handle backend processing and uncached requests.

Benefits

  • It enables multi-layered caching.

  • High availability even when Magento is under stress or unreachable.

  • It enables geographic performance optimization. CDN edge nodes cache assets closer to users.

  • It handles traffic spikes without straining the backend.

PERFORMANCE ANALYTICS

Real-Time Cache Performance

Monitor your Varnish cache effectiveness and Grace Mode impact

HIT RATE
94.7%
MISS RATE
5.3%
GRACE HITS
1.2%
AVG RESPONSE
18ms

Cache Hit Distribution

Cache Hits
Cache Misses
Grace Mode

Backend Health

CPU Usage 32%
Memory Usage 48%

Traffic Analysis

REQUESTS/MIN
12.8K
BANDWIDTH
245 MB/s
ACTIVE CONN
2,847
ERROR RATE
0.02%

5 Common Challenges and Solutions of Varnish Grace Mode

1. Stale Content Displayed to Users

Varnish may serve outdated product data, including incorrect prices or unavailable items. It is when using grace mode that the backend remains unresponsive.

Solution:

  • Set appropriate grace and keep timeouts.

  • Use cache tags and ban mechanisms in Magento to invalidate stale content.

  • Combine with Edge Side Includes to refresh personalized blocks.

2. Grace Mode Not Triggering as Expected

MONITORING

Real-time Dashboard

Monitor your Varnish Grace Mode performance with live metrics

System Healthy
Last updated: 2 seconds ago
Cache Hit Ratio
94.8%
+2.3%
Grace Hits
1,247
Last hour
Backend Status
OK
4 healthy
Avg Response
42ms
P95: 89ms

Request Rate (per second)

1000 750 500 250
Current: 856 req/s

Grace Mode Activity

Grace mode activated 2m ago
Backend timeout - serving stale content
Grace period extended 5m ago
High load detected
Backend recovered 8m ago
Normal operation resumed

Alert Configuration

Grace hits > 1000/hour
Hit ratio < 90%
Backend failures

Varnish might fail to serve stale content if the backend fails. It is especially true if you keep objects that have expired.

Solution:

  • Ensure to configure the backend health check in Varnish. Unhealthy backends help trigger grace mode.

  • Use set beresp.grace = 1h and set beresp.keep = 6h.

  • Use retry logic or failover for backend requests.

3. SEO Risks from Outdated Pages

Search engines may index stale content served during grace periods. It affects SEO and user trust.

Solution:

  • Avoid caching pages with the 'noindex' attribute or dynamic, user-specific URLs.

  • Use Surrogate-Control headers to fine-tune cache duration per content type.

  • Leverage cache tag invalidation in Magento for timely content purges.

4. Difficulty Diagnosing Grace Mode Behavior

Debugging whether the grace mode or the regular cache serves the content. It can be unclear without logging.

Solution:

  • Add custom headers in VCL to show cache status.

  • Use tools like Varnishlog or New Relic to track the origins of responses.

5. Conflicts with Other Caching Layers

Improper coordination between Varnish Grace Mode and other caching mechanisms. It can cause inconsistent content delivery.

Solution:

  • Define cache hierarchy, such as CDN and Varnish.

  • Use consistent TTLs and cache headers across layers.

  • Invalidate the cache at all layers when critical content updates.

CONFIGURATION

VCL Config Builder

Generate your custom Varnish Grace Mode configuration with interactive controls

Configuration Settings

5 min

How long content is considered fresh

10 min

How long stale content can be served when backend is down

60 min

How long to keep objects in cache storage

Content Type Settings

Generated VCL Configuration

sub vcl_backend_response {
    # Grace Mode Configuration
    # Generated by Varnish Grace Mode Builder
    # Set Time to Live (TTL)
    set beresp.ttl = 5m;
    # Set Grace Period - serve stale content if backend is down
    set beresp.grace = 10m;
    # Set Keep Period - how long to keep in storage
    set beresp.keep = 60m;
    # Content-specific settings
    if (bereq.url ~ "^/catalog/product/") {
        # Product pages
        set beresp.ttl = 5m;
        set beresp.grace = 10m;
    }
    if (bereq.url ~ "^/catalog/category/") {
        # Category pages
        set beresp.ttl = 5m;
        set beresp.grace = 10m;
    }
    if (bereq.url ~ "^/cms/") {
        # CMS pages
        set beresp.ttl = 5m;
        set beresp.grace = 10m;
    }
    # Health check for backend
    if (beresp.status >= 500) {
        # Backend is unhealthy, increase grace
        set beresp.grace = 24h;
    }
}
sub vcl_hit {
    # Handle grace mode logic
    if (obj.ttl >= 0s) {
        # Object is fresh, deliver it
        return (deliver);
    }
    if (obj.ttl + obj.grace > 0s) {
        # Object is in grace period
        if (std.healthy(req.backend_hint)) {
            # Backend is healthy, fetch fresh
            return (miss);
        } else {
            # Backend is down, deliver stale
            return (deliver);
        }
    }
    return (miss);
}
Implementation Tips
  • Test your configuration with varnishtest before deployment
  • Monitor grace hits using varnishstat -f MAIN.grace_hits
  • Adjust values based on your traffic patterns and backend performance

Configuration Presets


3 Use Cases of Varnish Grace Mode

1. Everlast

  • Everlast performs regular Magento patch updates and deploys features late at night.

  • During these windows, the backend is in maintenance mode. Varnish still serves stale but rendered HTML pages via grace mode. It helps avoid any user disruption.

  • The DevOps team uses custom VCL logic to bypass Magento completely for certain paths. It is during maintenance. It relies on Varnish Grace’s logic to maintain content delivery.

2. JCB Shop

  • JCB shop’s eCommerce store encounters sudden traffic surges from international marketing campaigns.

  • Grace Mode protects against Magento overload. It is by keeping cached versions available for up to 1 hour. It absorbs massive traffic with no backend pressure.

  • Pair Grace mode with fast SSD-based Varnish storage to ensure rapid delivery. Magento’s cache tags help keep high-priority products updated.

3. Graze.com

  • Graze.com serves personalized carts and messages via ESI blocks. Varnish caches the shell of product pages and category listings. They pull dynamic blocks in post-load.

  • During backend strain, stale cache serves the base page and retries ESI requests.

  • They use a TTL of 15 minutes with ESI blocks excluded from the cache. It helps ensure personalization works even when the main backend is under heavy load.

STRATEGY COMPARISON

Choose the Right Caching Strategy

Compare different caching approaches for your Magento 2 architecture

Strategy Layer Purpose Benefits Best For
Varnish Grace Mode
Primary focus
HTTP Cache Serve stale content during outages
High Availability Instant Failover
Traffic spikes, Backend failures
Redis Sentinel
Session resilience
In-Memory HA for Redis cache/sessions
Auto Failover Session Continuity
Redis crashes, Multi-node setup
Multi-region CDN
Global edge delivery
Edge Distribute content globally
Low Latency DDoS Protection
International traffic, Static assets
Magento FPC
Built-in caching
Application Cache HTML output in Magento
Tag-aware Integrated
Small stores, Quick setup

Strategy Effectiveness Comparison

Varnish Grace
CDN + Varnish
Full Stack

FAQs

1. How do I set the grace period in Magento 2?

You set the grace period in your Varnish VCL file using beresp.grace. Magento doesn't control Grace but generates the base VCL. Edit the generated VCL in vcl_backend_response to define grace durations per content type.

2. What happens when the Varnish grace period expires?

Once the grace period ends, Varnish will no longer serve stale content. If the backend is still down, the user may receive an error or a blank page. To avoid this, consider using a fallback page or custom error handling logic.

3. Can I use different grace periods for different pages?

You can customize grace periods by adding conditional logic in the VCL. Match URLs or backend responses to assign different beresp.grace values. It allows you to keep static pages cached for longer while limiting stale content.

4. How do I know if grace mode is working?

Use a varnishlog to check if Grace serves the responses. Look for indicators such as hit via grace or backend fetch delays. You can also add custom response headers in VCL to signal grace usage.

5. Can I disable grace mode for specific content?

Exclude specific URLs or headers using conditions in your VCL file. Set beresp.grace = 0s for those requests to skip stale content fallback. You can also control caching behavior using Magento’s cacheable.xml.

Summary

Varnish Grace Mode Magento 2 ensures high availability by serving expired cached content. The article explores the key points of the technique, including:

  • Grace Mode serves stale content after the TTL expires to avoid delays or errors.

  • ESI blocks in Magento help separate dynamic content from cached pages.

  • Proper grace duration, cache invalidation, and monitoring are essential.

  • Real brands like Everlast use Grace Mode to ensure reliability during outages.

Ensure uptime and smooth performance with Varnish Grace Mode. Choose managed Magento hosting for advanced caching and high availability.

Ruby Agarwal
Ruby Agarwal
Technical Writer

Ruby is an experienced technical writer sharing well-researched Magento hosting insights. She likes to combine unique technical and marketing knowledge in her content.


Get the fastest Magento Hosting! Get Started