Fix Library: Performance and caching

Varnish 503 Backend Fetch Failed

Last updated:

What you see

Some pages, or the whole store, show "Error 503 Backend fetch failed" from Varnish.

Quick answer

Varnish shows this error when it could not get a good answer from Magento. On Magento stores the usual causes are response headers larger than Varnish allows (Magento sends long cache tag headers), a timeout on slow pages, or a failing health check. The Varnish log tells you which one it is.

What usually causes it?

  1. Response headers too large for Varnish limits (long X-Magento-Tags headers on category pages)
  2. Slow pages that exceed the backend timeout
  3. The health check probe failing, so Varnish marks Magento as down
  4. PHP-FPM or the web server crashing or refusing connections

How do you check it safely?

These checks only read information. Run them on staging first if you can, from the Magento root folder as the file system owner. Hosting setups differ, so paths and service names may need adjusting.

  1. Watch requests that end in a 503 and read the FetchError line.
    varnishlog -g request -q 'RespStatus == 503'
  2. Check whether Varnish thinks the backend is healthy.
    varnishadm backend.list
  3. Check the current header size limits.
    varnishadm param.show http_resp_hdr_len
    varnishadm param.show http_resp_size
  4. Call the health check directly. In the Magento generated VCL it is /pub/health_check.php, or /health_check.php when the web root is pub.
    curl -sI http://127.0.0.1:8080/pub/health_check.php
  5. Read the PHP-FPM and web server error logs for the same time.

How do you fix it?

These steps change your store. Run them on staging first, with a backup of code and database.

  1. If the log shows a header overflow, raise the limits in the varnishd start options. How you set them depends on your host. Restart Varnish afterwards.
    -p http_resp_hdr_len=65536 -p http_resp_size=98304 -p workspace_backend=131072
  2. If it is a timeout, find the slow page and fix it. Raise first_byte_timeout in the backend definition only as a stop gap.
  3. Regenerate the VCL from Magento so the probe and backend settings match your setup, then load it into Varnish.
    bin/magento varnish:vcl:generate --export-version=6 --output-file=/tmp/default.vcl
  4. If PHP-FPM is failing, fix the PHP error or raise its process limits with your host.

When should you call an expert?

Call an expert if the 503s come and go, appear only on some pages, or the host and developer each say it is the other side's problem.

Need it fixed today?

I can look at it with you. Book a 20 minute call, or if the store is down, see emergency Magento support.

FAQ

Common questions.

Is a 503 from Varnish a Varnish bug?

Rarely. Varnish is reporting that Magento did not answer in time or answered with something it could not accept. The fix is usually a setting or a slow page.

Can I just bypass Varnish?

As an emergency step, yes, but your store gets much slower under load. Fix the cause and turn Varnish back on.

Rather have it fixed for you?

Send a few lines about the problem, or book a 20 minute call. You get an honest read and a fixed quote.

Book a 20 minute call Reply within one business day (Mon to Fri).