Varnish Cache : Boost Your Website Speed!

NHAILA Achraf
4 min readAug 3, 2023
workflow varnish

Varnish is like a super-fast memory bank for websites. When it gets a request from clients (users), it will try to respond instantly from its cache.

But if Varnish doesn’t have the answer in its cache, no worries! It sends the request to the backend.

Lightning speed response

How Varnish Works (Briefly)

1 User makes a request to the web server.
2 Varnish checks its cache for the requested content.
3–1 If the content is in the cache, Varnish serves it directly to the user, avoiding the need to access the backend server.
2–2 If the content is not in the cache, Varnish forwards the request to the backend server to fetch the content, caches it, and serves it to the user.

Varnish uses an efficient caching mechanism to determine when cached content should be invalidated or refreshed based on HTTP headers and user-defined rules.

Use Cases for Varnish Cache:

  1. The Flash Speed Booster: Is your website loading slower than a sleepy sloth? Varnish Cache comes to the rescue! Use it as a caching to store and serve your web pages super-fast, reducing server load and making your users happier than a kid in a candy store.
  2. Traffic Jam Tamer: Are you expecting a massive surge of visitors due to a viral post or a big marketing campaign? Fear not! Varnish Cache can handle the stampede of traffic by caching content, so your website stays alive and kicking, no matter how many fans come knocking.
  3. E-Commerce Wonder: Online stores need speed to keep impatient shoppers from abandoning their carts. Varnish Cache swoops in, caching product pages and speeding up checkout like a speedy checkout lane at the supermarket.
  4. The Dynamic Content Sorcerer: Have ever-changing content like news articles or social media feeds? Varnish Cache can magically cache dynamic content, so your users get the latest updates faster than a wizard casting a spell.

Install Varnish Cache:

Open your terminal (or command prompt) on the server where your website lives.

Example for Ubuntu users :

Step 1: Update the package list
Step 2: Install Varnish Cache
Step 3: Start and Enable Varnish
Step 4: Check Varnish Status (Optional)

sudo apt update
sudo apt install varnish
sudo systemctl start varnish
sudo systemctl enable varnish
sudo systemctl status varnish

Now you have Varnish Cache, making your website blazing fast! 😃

Basic Commands in Varnish

Here are some essential Varnish commands that you can use via the command-line interface (CLI):

  1. varnishd: This is the Varnish daemon command used to start the Varnish Cache server.
varnishd -f /etc/varnish/default.vcl

2. varnishlog: This command is used to monitor the Varnish log in real-time.

varnishlog

3. varnishstat: To display various statistics about the Varnish Cache server.

varnishstat

4. varnishadm: Provides an interactive CLI to manage Varnish Cache.

  varnishadm

5. varnishncsa: Similar to Apache’s mod_log_config, this command formats the Varnish log for easy readability.

varnishncsa

Detecting Problems in Logs

Varnish logs can be valuable in troubleshooting issues with the cache or backend server. The varnishlog command allows you to monitor real-time log data. Here are some common problems and how to detect them in the logs:

  1. Cache Misses: If you notice frequent “cache miss” messages, it indicates that Varnish is fetching content from the backend server often, potentially due to incorrect cache settings or cache bypass headers.
  2. Backend Fetch Failures: Look for “backend fetch failed” messages, which indicate that Varnish couldn’t retrieve content from the backend server. This may be due to a misconfigured backend or server unavailability.
  3. Cache Expire and TTL: Check for “object moved to the cache” and “object expired” messages. These show that Varnish is caching content correctly and refreshing it based on cache expiration settings.
  4. VCL Errors: If there are issues with your VCL (Varnish Configuration Language), the logs may show “VCL compilation errors.”
  5. Overloaded Backend: Consistently high “Fetch no body” values may indicate that your backend server is overloaded and not responding to requests.

Flexibility

One of the key features of Varnish Cache, in addition to its performance, is the flexibility of its configuration language, VCL.

VCL enables you to write policies on how incoming requests should be handled.

Varnish default configuration file. For source installations, look for /usr/local/etc/varnish/default.vcl, and for package lovers, it’s probably at /etc/varnish/default.vcl.

This is a brief introduction to Varnish Cache, basic commands, and log monitoring….

Your website can be as fast as a rocket-powered unicorn! 🚀🦄

Easy peasy! 🏁
Hope this will help!
NHAILA Achraf

--

--