Johannes Wachter
Johannes Wachter
Core Developer – Sulu GmbH
Sulu Core Developer and Open-Source Enthusiast.

Use Varnish’s grace mode to keep your site responsive under pressure

Whether you have regular high traffic or sudden peaks that can overwhelm your servers quickly, you need a solution that stays rock solid under pressure, serving your content fast, while also providing you with high performance. Varnish’s reputation for responsiveness is widely known, and one of the features that helps contribute to that reputation is grace mode.

Defining grace mode

Every cache entry has a lifetime which defines how long it is considered valid or “fresh”, called the Time To Live (TTL). A Varnish feature, called grace mode, extends this time period. Johannes Wachter, Core Developer at Sulu, explains further, “Grace mode is a configuration on Varnish that allows it to respond with stale entries in the given time after the TTL is over, while it updates the cache entry in the background.” This is useful in scenarios where some degree of staleness is acceptable, for example, weather conditions or news stories.

Grace mode is inactive by default. This means when a cache entry’s TTL is over, a new, freshly created cache entry is returned. But that new entry takes time and server resources to generate – even more so if multiple users are requesting it at the same time.

When grace mode is turned on, it allows Varnish to deliver slightly stale content to users while fetching the newest version from the backend. Overall, this process adds up to faster load times at lower cost. Johannes adds, “If grace mode is activated, you can configure the time period in which the old, stale entries will be delivered.”

Check out the Varnish documentation to learn how you can use grace mode to help keep your site responsive under pressure.

How you can use Varnish’s grace mode to maintain high performance

Ensure “peak” moments go smoothly

Many caching proxies send user requests straight to the backend. During “peak times”, this move can overwhelm your servers, often bringing them down. For moments like these, Varnish has grace mode. Regardless of whether it’s dozens, hundreds, or even thousands of users who request the same page at the same time, Varnish still only sends a single request to the backend. This keeps the load away from the server, which prevents down time and uses less server energy. 

Johannes explains, “So if 100 users try to get a page at the same time, and the cache entry is old, stale, or its TTL is over, Varnish returns the old cache entry to the 100 users. In the background, one request is triggered to the server to update the cache entry in the memory.”

Use your server resources intelligently 

Varnish’s features, like grace mode, help keep your cache healthy. When grace mode dives into the cache, it updates the cache entry it’s been sent to fetch. This helps ensure you aren’t wasting server resources, like cache storage, unnecessarily on stale content, and it also helps give you a better, more accurate snapshot of how you’re using your servers. Johannes says, of grace mode, “It helps keep your content accurate and current.” 

Serve a high-quality user experience

Varnish’s grace mode can help keep users more satisfied with their experience. For one, when grace mode is activated, it runs asynchronously in the background, so the user experience is not negatively affected. Grace mode also decreases load times by providing users with an older version of the content immediately, and refreshing with the new version once it’s available. 

This depends on the type of site you run and how often the content you serve needs to change. Johannes says, “Sometimes, it’s okay if you deliver old cache entries after the TTL is over if the site doesn’t change so much.” For a news site, for example, serving a main page that is a few seconds old is not a problem if this gives your site faster load times.

Varnish delivers grace when you need it most

Grace mode is a feature unique to Varnish, and it can help you maintain a high level of responsiveness, keep your users satisfied, and your cache up-to-date and accurate. Learn more about using Varnish for caching with Sulu in the documentation.