Varnish 101 – An introduction to caching, Varnish, and .vcl files.



Varnish 101 – An introduction to caching, Varnish, and .vcl files.

0 0


varnish101


On Github jwfuller / varnish101

Varnish 101

An introduction to caching, Varnish, and .vcl files.

James FullerWeb Application DeveloperUniversity of Iowa - ITSd.o: fullerja

Follow along at: http://rawgit.com/jwfuller/varnish101/master/index.html#/

What is Varnish?

  • “A web application accelerator also known as a caching HTTP reverse proxy.”
  • A piece of software that makes dynamic websites really really really fast.
  • It sits between the client and the webserver and caches webpages.

Drupal with out any caching

~ 3 mph

http://s0.geograph.org.uk/photos/06/39/063959_49f25f81.jpg

Drupal with core caching

~ 80 mph

http://upload.wikimedia.org/wikipedia/commons/2/2d/390017_Rugby_station.jpg

Drupal with Varnish

~ 357.2 mph

https://c2.staticflickr.com/2/1242/1385598849_303b87f58c_z.jpg?zz=1

Loading a webpage from Drupal

Plain ol' Drupal

User requests a URL. Web server (Apache/Nginx/IIS) sends it to Drupal. Drupal bootstraps and initializes the database, sessions etc. Maps the path to a callback function, which gets the primary content. Modules can hook into the process and extend functionality and alter the content. The Theme System generates the HTML and applies styles it to. Drupal returns a fully formed HTML page to the browser. The browser renders the HTML page for the user.

Drupal with core caching

User requests a URL. Web server (Apache/Nginx/IIS) sends it to Drupal. Drupal bootstraps and initializes the database, sessions etc.
Maps the path to a callback function, which gets the primary content. Modules can hook into the process and extend functionality and alter the content. The Theme System generates the HTML and applies styles it to.
Drupal returns a fully formed HTML page to the browser. The browser renders the HTML page for the user.

Drupal (Now with Varnish!)

User requests a URL
Web server (Apache/Nginx/IIS) sends it to Drupal. Drupal bootstraps and initializes the database, sessions etc. Maps the path to a callback function, which gets the primary content. Modules can hook into the process and extend functionality and alter the content. The Theme System generates the HTML and applies styles it to. Drupal returns a fully formed HTML page to the browser.
The browser renders the HTML page for the user.

How does Varnish do this?

A request is recieved Varnish determines if it should Lookup, Pass, Pipe or Error.
  • Lookup = Request that could be cached
  • Pass = Request cannot be cached (cookies) or should not be cached (configuration)
  • Pipe = Request should be mainlined to the server (POST or similar transmition of data that should not be interupted)
  • Error = Backend is down or varnish is configured to error
Assuming a Lookup, Varnish sends the request to Drupal. Drupal does the long look up and responds. Varnish caches a copy of that response.
  • This can be from RAM or disk, RAM is highly recommended.
Request is made for the same thing Varnish does a Lookup
  • Checks to make sure the cache lifetime has not expired
Responds with its copy from RAM. (Skipping Drupal entirely)

Installation

(20,000 ft view)

Download and install Varnish
  • (apt-get, yum, make/install)
Swap Apache off port 80 and put Varnish on Adjust your configuration in the .vcl file Install the Drupal Varnish module
  • (including editing your settings.php file)
Adjust your Performance settings

Anatomy of a VCL

Backend Probe

# Define the health check for Drupal Servers.
probe drupal_healthcheck {
   .url = "/health_check.php";
   .interval = 5s;
   .timeout = 1s;
   .window = 5;
   .threshold = 3;
   .expected_response = 200;
}
					

Backend Definitions

# Define the list of backends (web servers).
# Port 80 Backend Servers
backend web1 {
	.host = "192.10.0.1”;
	.probe = drupal_healthcheck;
	}

backend web2 {
	.host = "192.10.0.2”;
	.probe = drupal_healthcheck;
	}

# Port 443 Backend Servers for SSL
backend web1_ssl {
	.host = "192.10.0.1”;
	.port = "443”;
	.probe = drupal_healthcheck;
	}

backend web2_ssl {
	.host = "192.10.0.2”;
	.port = "443”;