Skip to main content
Home >Blogs >AI in 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
Loading a web page may seem simple to a user: type a 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 players in a web request

A web request goes through several stages and involves different players, 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 in locating the appropriate server.
  • Content Delivery Networks (CDN): These serve static files, such as images or scripts, from servers close to the user.
  • The application server: It receives the request, performs necessary dynamic processing (such as querying a database or executing code) and generates a response.
  • The database server: Often requested by the application server to provide data in real time.
  • 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 player

In the total response time, each player contributes differently. For example:
  • DNS resolution can account for 10% of the total time.
  • CDNs optimise static content to reduce their impact, but this still represents 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%.

An 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. Positioned 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 significantly 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 edge of the network, Varnish operates directly alongside the application server. This makes it an ideal choice for sites requiring fine-tuned 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 contacting the server. Otherwise, it forwards the request to the server, saves the response in cache for future use, and then returns it to the client. This simple-looking operation hides an impressive power to optimise performance.

The Advantages of Varnish

Rock-solid robustness

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

A portable and open-source tool

Varnish is an open-source utility, which means it is accessible for free and can be adapted to your specific needs. Its adoption is aided by extensive 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 terminology and VCL

One of the major hurdles to adopting Varnish is the need to master VCL (Varnish Configuration Language). Although powerful, this language introduces a learning curve, especially 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 certain cases, can be surprising and requires adjustments to suit your needs.

Increased stack complexity

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 guarantee optimal performance, we have developed a solution based on Docker.

An optimised and monitored stack with Docker

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

Two years of stability for guaranteed performance

Our current configuration has proven itself: stable and high-performing, 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 to specific needs.

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