Make Your Webapp Shine With Varnish Part 2 Backends

Pre-req reading: Part 1

In this part we will cover setting up a backend. A backend is your application server, whether this be apache / nginx / iis (IIS - Is Inherently Stupid) you are telling varnish where it should sends it’s requests to. Very basic configuration

.backend app1 {
    .host = "127.0.0.1";
    .port = "8080;"
}

For a quick start that’s it really you tell varnish a backend and the port to connect to it on … just make sure you use it in vcl_recv, but you’re not here for simple and quick start are you? lets add the following.

  • timeout settings
  • probe settings

Timeout settings

Your timeout settings deinf how long varnish should wait for a response from your backend

.backend app1 {
    .host = "127.0.0.1";
    .port = "8080;"
    .connect_timeout = 0.05s;
    .first_byte_timeout = 2s;
    .between_bytes_timeout = 2s;
}
  • connect_timeout wait 50ms for a tcp connection to take place
  • first_byte_timeout wait 2s for the first byte of data to be sent from the backend
  • between_bytes_timeout wait 2s if there is a pause mid data stream

Timeouts are a basic way of determining if a backend is down / miss behaving if you have multiple backends if timeouts occur then the backend is marked as sick and the other backends will be used.

probe settings - Trust me I’m a doctor

.backend app1 {
    .host = "127.0.0.1";
    .port = "8080;"
    .connect_timeout = 0.05s;
    .first_byte_timeout = 2s;
    .between_bytes_timeout = 2s;
    .probe = {
    .url = "/status.html";
    .timeout = 0.05s;
    .window = 5;  
    .threshold = 3;   #60% of last checks must of been OK for this backend to be healthy
    .interval = 2s;   #how often to run the checks
    }
}
  • url the URL to to query this must return a 200 OK response, you could use a php script to return a 500 on say a mySQL outage
  • timeout how long to wait for a 200 OK response from the URL
  • window keep the result of the last 5 probes in memory
  • threshold how many of the window total must be OK for the backend to be “healthy”
  • interval how often to run the probe

And that about wraps up this post.

Comments