Varnish .max_connections Parameter – How to Calculate for Nginx Backend

cacheconnectionnginxphp-fpmvarnish

I have seen .max_connections parameter in varnish default.vcl and by seraching in web, found the artile below
.max_connections: how many connections you authorize Varnish to open to this backend. Remember that connections are pooled and reused, so your backend should have an easier life anyway, but sometimes, you need to protect it even more. Be careful though because that is a hard limit, and if it is reached, Varnish won't use this backend, even if it means returning an error to the user.

Now, I want to know if max connection means request/sec or concurrent user or anything else.
How will I calculate it for a backend nginx server?

Best Answer

The need for multiple backend connections

In HTTP/1.1 there is no request multiplexing on a single connection. If there's multiple requests to Varnish at the same time, for resources that aren't stored in cache, multiple backend connections might be opened up.

Varnish will keep connections open to the backend and re-use them for other requests. Varnish will also perform request coalescing and collapse backend requests for the same resource.

That being said, there are still situations where Varnish needs to open up multiple connections to a single backend.

Limiting backend connections

Depending on the amount of traffic your platform gets, depending on the hit rate of your cache, and the resources that your backend servers have, you might want to limit the connections to the backend.

The idea of using Varnish, is to avoid excessive connections to your origin servers. By introducing max_connections in your backend definition, you can protect your origin from excessive revalidation in Varnish.

If your hit rate is decent, you will never hit that limit, but if you do max_connections will ensure fetches get queued until a connection gets freed up.

Calculating max_connections

max_connections is there to prevent your server from going out of memory.

The value you're going to choose depends on the amount of resources your origin server has, and the average time it takes to satisfy a request.

You can perform a load test using ab or siege and figure out how many simultaneous connections your origin can handle before going into high load.

Please make sure keepalive is turned off during those tests

It's very much trial and error. Make sure that your initial max_connections value is way too high, send enough traffic, and look for the tipping point.

Lower the value of max_connections, rinse, repeat.

Once you figured out the max_connections setting for your origin, keep it in the webserver configuratino of your origin (Apache or Nginx).

Eventually, you'll also set that value in your Varnish backend definition.

The idea is that Varnish shouldn't allow more simultaneous connections to your backend servers, than what they're configured to handle.