Skip to main content
Accueil >Blogs >L'IA dans le web

The essential Varnish

Discover how Varnish, a reverse proxy and HTTP cache, reduces response times and improves the performance of a Docker architecture.

The essential Varnish
Sur cette page1 sections
Loading a web page can seem simple for a user: type an URL, press Enter, and there you go, the site appears. But behind this apparent simplicity lies complex mechanics and, above all, performance issues that can penalise your users. Let's see together 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 with a specific role and an impact on the overall response time:
  • The client (browser) : It initiates the request by typing the URL and sending a request to the server.
  • DNS servers : They translate the domain name into an IP address, an essential step for locating the appropriate server.
  • Content Delivery Networks (CDNs) : These 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 (such as querying a database or executing code) and generates a response.
  • The database server : Often called upon by the application server to provide real-time data.
  • 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:
  • DNS resolution can represent 10% of the total time.
  • CDNs optimise static content to reduce their impact, but this 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 peaks.
This is precisely where Varnish steps in to relieve the application server and reduce that 50%.

Introduction to Varnish

Varnish, a key player in optimisation

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 accelerating response time.By intercepting requests and serving pages directly from its cache, Varnish considerably reduces the load on the application server. The result? Faster loading times for your users and better allocated resources on your infrastructure.Its positioning : Unlike a CDN which is often located at the network edge, Varnish acts directly next to the application server. This makes it an ideal choice for sites requiring fine-grained cache customisation.

How Varnish works

When a request reaches Varnish, it checks if the requested response is already in cache. If so, it returns it immediately without calling the server. Otherwise, it forwards the request to the server, saves the response in cache for future use, then returns it to the client. This seemingly simple operation hides impressive power for optimising performance.

The advantages of Varnish

Unrivalled robustness

Varnish is known for its ability to absorb traffic peaks, particularly 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 goes down temporarily, Varnish can continue to serve cached pages. This service continuity is valuable for maintaining a smooth user experience.

A portable and open source tool

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

ESI management

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 personalised 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). Although powerful, this language introduces a learning curve, particularly for understanding and managing implicit behaviours.

Implicit behaviours

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

The increased 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 represent a challenge for limited technical teams.

Our turnkey solution with Docker

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

Une stack optimisée et monitorée avec Docker

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

Two years of stability for guaranteed performance

Our current configuration has proven its worth: stable and efficient, it is used daily to guarantee 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 according to specific needs.

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