69
Mike Willbanks | Barnes & Noble V arnish Cache

IPC Varnish

  • Upload
    tdt

  • View
    261

  • Download
    0

Embed Size (px)

Citation preview

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 1/69

Mike Willbanks | Barnes & Noble

Varnish Cache

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 2/69

Housekeeping…

 •  Talk

 – Slides will be posted after the talk.

• Me – Sr. Web Architect Manager at NOOK

Developer 

 – Prior MNPHP Organizer 

 – Open Source Contributor 

 – Where you can find me:•  Twitter : mwillbanks G+: Mike Willbanks

•  IRC (freenode): mwillbanks Blog:http://blog.digitalstruct.com 

• GitHub: https://github.com/mwillbanks 

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 3/69

 Agenda•  Varnish?

•  The Good : Getting Started

• 

The Awesome : General Usage•  The Crazy : Advanced Usage

•  Gotchas

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 4/69

WHAT IS VARNISH?

Official StatementWhat it does

General use case

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 5/69

Official Statement

“Varnish is a web application accelerator. You install it in front of your web application and it will speed it 

up significantly.” 

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 6/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 7/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 8/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 9/69

 You can cache… 

Both dynamic and static files and contents.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 10/69

 A Scenario•  System Status Server 

 – Mobile apps check current status.

 – If the system is down do we communicate?

 – If there are problems do we communicate?

 – The apps and mobile site rely on an API

•  Trouble in paradise? Few and far in between.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 11/69

The Graph - AWS

0

10000

20000

30000

40000

50000

60000

70000

80000

Small X-Large Small Varnish

Requests

Requests

0

50

100

150

200

250

300

350

400

450

500

Small X-Large Small Varnish

Time

Time

0

2

4

6

8

10

12

14

Small X-Large Small Varnish

Peak Load

Peak Load

0

100

200

300

400

500

600

700

Small X-Large Small Varnish

Req/s

Req/s

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 12/69

The Raw DataSmall X-Large SmallVarnish

Concurrency 10 150 150

Requests 5000 55558 75000

Time 438 347 36Req/s 11.42 58 585

PeakLoad 11.91 8.44 0.35

Comments

19,442failed

requests

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 13/69

Traditional LAMP Stack

HTTP Server Cluster

Database

Load Balancer

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 14/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 15/69

THE GOOD – JUMP START

Installation

General Information

Default VCL

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 16/69

Installationrpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm

yum install varnish

curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -echo "deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0" | sudo

tee -a /etc/apt/sources.listsudo apt-get update

sudo apt-get install varnish

git clone git://git.varnish-cache.org/varnish-cachecd varnish-cache

sh autogen.sh./configure

make && make install

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 17/69

Varnish Daemon•  varnishd

 –  -a address[:port] listen for client

 – -b address[:port] backend requests

 –  -T address[:port] administration http

 –  -s type[,options] storage type (malloc, file,persistence)

 –  -P /path/to/file PID file – Many others; these are generally the most

important. Generally the defaults will do with just modification of the default VCL (more on it

later).

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 18/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 19/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 20/69

So what’s actually caching?•  Any requests containing

 – GET / HEAD

 – TTL > 0

•  What cause it to miss?

 – Cookies

 –  Authentication Headers

 – Vary “*”

 – Cache-control: private

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 21/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 22/69

HTTP Caching•  RFC 2616 HTTP/1.1 Headers

 – Expiration

•  Cache-Control

•  Expires

 – Validation

• Last Modified

•  If-Modified-Since

•  ETag

•  If-None-Match

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 23/69

TTL Priority•  VCL

 – beresp.ttl

•  Headers

 – Cache-control: s-max-age

 – Cache-control: max-age

 – Expires

 – Validation

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 24/69

Use Wordpress?backend default {

.host = "127.0.0.1“;

.port = "8080";

}

sub vcl_recv {

if (!(req.url ~ "wp-(login|admin)")) {unset req.http.cookie;

}

}

sub vcl_fetch {if (!(req.url ~ "wp-(login|admin)")) {

unset beresp.http.set-cookie;

}}

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 25/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 26/69

Varnish Configuration

Language•  VCL State Engine

 –  Each Request is Processed Separately &Independently

 – States are Isolated but are Related –  Return statements exit one state and start another 

 –  VCL defaults are ALWAYS appended below your ownVCL

•  VCL can be complex, but… 

 –  Two main subroutines; vcl_recv and vcl_fetch –  Common actions: pass, hit_for_pass, lookup, pipe,

deliver 

 –  Common variables: req, beresp and obj

 –  More subroutines, functions and complexity can arise

dependent on condition.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 27/69

Request

Response

vcl_recv

re q.

vcl_hash

vcl_hit

vcl_miss

vcl_fetch

vcl_deliver

re q.

re q.

 o b j.

res p.

re q.

 bere q.

 beres p.

re q.

 bere q.

vcl_pipe

re q.

 bere q.

vcl_pass

re q.

 bere q.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 28/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 29/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 30/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 31/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 32/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 33/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 34/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 35/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 36/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 37/69

vcl_fetch•  Fetch retrieves the response from the

backend.

•  No Cache if …

  – TTL is not set or not greater than 0.

 – Vary headers exist.

 – Hit-For-Pass means we will cache a passthrough.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 38/69

GENERAL ADJUSTMENTS

Common adjustments to make.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 39/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 40/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 41/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 42/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 43/69

Directors – The TypesDirector Type Description

Random Picks based on random and weight.

Client Picks based on client identity.

Hash Picks based on hash value.

Round Robin Goes in order and starts over 

DNS Picks based on incoming DNS host,random OR round robin.

Fallback Picks the first “healthy” server.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 44/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 45/69

Load Balancing

Implementing a simple varnish load balancer.

Varnish does not handle SSL termination.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 46/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 47/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 48/69

Purging•  The various ways of purging

 – varnishadm – command line utility

 – Sockets (port 6082) – HTTP – now that is the sexiness

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 49/69

Purging Examplesvarnishadm -T 127.0.0.1:6082 purge req.url == "/foo/bar“

telnet localhost 6082

purge req.url == "/foo/bar

telnet localhost 80

Response:

Trying 127.0.0.1...

Connected to localhost.

Escape character is '^]'.

PURGE /foo/bar HTTP/1.0

Host: bacon.org

curl –X PURGE http://bacon.org/foo/bar

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 50/69

Distributed Purging•  curl multi-request (in php)

•  Use a message queue

 –  Use workers to do the leg work for you

•  You will need to store a list of servers “somewhere”

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 51/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 52/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 53/69

VERIFY YOUR VCL

You likely want to ensure that your cache is:

1.  Working Properly

2.  Caching Effectively

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 54/69

What is Varnish doing… 

Varnishtop will show you real time information on your system.

•  Use -i to filter on specific tags.

•  Use -x to exclude specific tags.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 55/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 56/69

THE CRAZY

ESI – Edge-Side Includes

Varnish Administration

VMOD

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 57/69

ESI – Edge Side Includes

•  ESI is a small markup language much likeSSI (server side includes) to includefragments (or dynamic content for that

matter).•  Think of it as replacing regions inside of a

page as if you were using XHR (AJAX) butsingle threaded.

•  Three Statements can be utilized. – esi:include – Include a page

 – esi:remove – Remove content

 – <!-- esi --> - ESI disabled, execute normally

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 58/69

ESI Diagram

Page Content

<esi:includesrc="header.php" />

Backend

Varnish

Varnish detects ESI, requests from backend OR checks cachedstate.

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 59/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 60/69

ESI Usage<html>

<head><title>Rock it with ESI</title></head>

<body>

<header>

<esi:include src=”header.php" />

</header>

<section id="main">...</section>

<footer></footer>

</body>

</html>

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 61/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 62/69

Embedded C for syslogC{

#include <syslog.h>

}C

sub vcl_something {

C{

syslog(LOG_INFO, "Something happened at VCL line XX.");

}C

}

# Example with using varnish variables

C{

syslog(LOG_ERR, "Spurious response from backend: xid %s request %s%s \"%s\" %d \"%s\" \"%s\"", VRT_r_req_xid(sp),

VRT_r_req_request(sp), VRT_GetHdr(sp, HDR_REQ, "\005host:"),

VRT_r_req_url(sp), VRT_r_obj_status(sp), VRT_r_obj_response(sp),VRT_GetHdr(sp, HDR_OBJ, "\011Location:"));

}C

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 63/69

Varnish Modules / Extensions

•  Taking VCL embedded C to the next

level

•  Allows you to extend varnish and createnew functions

•  You could link to libraries to provide

additional functionality

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 64/69

VMOD - std

•  toupper 

•  tolower 

•  set_up_tos•  random

•  log

•  syslog

•  fileread

•  duration•  integer 

•  collect

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 65/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 66/69

Management Console

•  varnishadm –T localhost:6062

 – vcl.list – see all loaded configuration

 – vcl.load – load new configuration – vcl.use – select configuration to use

 – vcl.discard – remove configuration

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 67/69

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 68/69

GOTCHAS

Having Keep-Alive off 

No SSL Termination

No persistent cache

ESI multiple fragments

Cookies*

7/27/2019 IPC Varnish

http://slidepdf.com/reader/full/ipc-varnish 69/69

QUESTIONS?

These slides will be posted to SlideShare & SpeakerDeck.

SpeakerDeck: http://speakerdeck.com/u/mwillbanks 

Slideshare: http://www.slideshare.net/mwillbanks 

Twitter: mwillbanks

G+: Mike Willbanks

IRC (freenode): mwillbanks

Blog: http://blog.digitalstruct.com GitHub: https://github.com/mwillbanks