Varnish: Optimize the loading of your web pages

Loading a web page may seem simple for a user: type a URL, hit Enter, and voilà, the site appears. But behind this apparent simplicity lies a complex mechanism, and above all, performance issues that can penalize your users. Let’s explore how to address this problem in this blog dedicated to Varnish!

Understanding the actors of a web request

A web request goes through several stages and involves different actors, each having a specific role and impact on the overall response time:
  • The client (browser): It initiates the request by typing the URL and sending a request to the server.
  • The DNS servers: They translate the domain name into an IP address, an essential step to locate the appropriate server.
  • The content delivery networks (CDN): They serve static files, such as images or scripts, from servers close to the user.
  • The application server: It receives the request, performs the necessary dynamic processing (like querying a database or executing code), and generates a response.
  • The database server: Often solicited by the application server to provide real-time data.
  • The intermediate caches: They intercept and respond directly to requests when a cached version of the content is available.

The proportion of time consumed by each actor

In the total response time, each actor contributes differently. For example:
  • The DNS resolution can represent 10% of the total time.
  • The CDNs optimize static content to reduce their impact, but it remains around 20%.
  • The application server and the database often represent more than 50% of the total response time, especially for dynamic pages. This bottleneck is particularly problematic during traffic spikes.
This is precisely where Varnish comes in to relieve the application server and reduce that 50%.

Introduction to Varnish

Varnish, a key player in optimization

What is Varnish? Varnish is a reverse proxy designed to cache responses generated by an application server. Placed right in front of the application, it acts as a buffer between the client and the final server, thus speeding up response time.By intercepting requests and serving pages directly from its cache, Varnish significantly reduces the load on the application server. The result? Faster loading times for your users and better resource allocation on your infrastructure.Its positioning: Unlike a CDN that is often located at the edge of the network, Varnish operates directly next to the application server. This makes it an ideal choice for sites requiring fine cache customization.

How Varnish works

When a request reaches Varnish, it checks if the requested response is already cached. If so, it immediately returns it without querying the server. Otherwise, it forwards the request to the server, caches the response for future use, and then returns it to the client. This simple operation on the surface hides impressive power to optimize performance.

The benefits of Varnish

Robustness in all circumstances

Varnish is known for its ability to absorb traffic spikes, especially during DDoS attacks. By maintaining a cache ready to respond to requests, it limits the impact of these attacks on the main server.Even if your main site temporarily goes down, Varnish can continue to serve cached pages. This continuity of service is valuable for maintaining a smooth user experience.

A portable and open-source tool

Varnish is an open-source utility, meaning it is freely accessible and can be tailored to your specific needs. Its adoption is facilitated by rich documentation and an active community.

Managing ESI

Thanks to Edge Side Includes (ESI), Varnish can manage dynamic content. For example, it is possible to cache a page while dynamically integrating specific blocks, such as a shopping cart or personalized information.

The challenges of using Varnish

The difficulty of jargon and VCL

One of the major obstacles to adopting Varnish is the need to master VCL (Varnish Configuration Language). While powerful, this language introduces a learning curve, especially to understand and manage implicit behaviors.

Implicit behaviors

For example, by default, if a request contains a cookie, Varnish may decide not to cache the response. This behavior, while logical in some cases, can be surprising and requires adjustments to fit your needs.

The complexity of the stack

Adding Varnish to your architecture involves managing an additional component. This requires adequate supervision to ensure its proper functioning, which can be a challenge for limited technical teams.

Our turnkey solution with Docker

To facilitate the integration of Varnish and ensure optimal performance, we have developed a solution based on Docker.

An optimized and monitored stack with Docker

By containerizing Varnish, along with other stack components, we simplify their management and deployment. Docker allows monitoring performance, automating updates, and maintaining a homogeneous and predictable architecture.

Two years of stability for guaranteed performance

Our current configuration has proven itself: stable and performant, it is used daily to ensure reduced loading times, even under heavy load.

Easy management of each stack component

Thanks to Docker, each element (Varnish, Caddy, PHP, etc.) can be managed independently. This allows for simplified maintenance and the ability to quickly adapt the configuration based on specific needs.

With Varnish, you optimize your performance and enhance the user experience. And thanks to our expertise and proven Docker stack, you benefit from a turnkey solution, ready to tackle all the challenges of the modern web.