Top Banner
Mike Willbanks | Barnes & Noble V arnish Cache
69

IPC Varnish

Apr 14, 2018

Download

Documents

trang tran
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IPC Varnish

7/27/2019 IPC Varnish

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

Mike Willbanks | Barnes & Noble

Varnish Cache

Page 2: IPC Varnish

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 

Page 3: IPC Varnish

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

Page 4: IPC Varnish

7/27/2019 IPC Varnish

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

WHAT IS VARNISH?

Official StatementWhat it does

General use case

Page 5: IPC Varnish

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.” 

Page 6: IPC Varnish

7/27/2019 IPC Varnish

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

Page 7: IPC Varnish

7/27/2019 IPC Varnish

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

Page 8: IPC Varnish

7/27/2019 IPC Varnish

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

Page 9: IPC Varnish

7/27/2019 IPC Varnish

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

 You can cache… 

Both dynamic and static files and contents.

Page 10: IPC Varnish

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.

Page 11: IPC Varnish

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

Page 12: IPC Varnish

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

Page 13: IPC Varnish

7/27/2019 IPC Varnish

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

Traditional LAMP Stack

HTTP Server Cluster

Database

Load Balancer

Page 14: IPC Varnish

7/27/2019 IPC Varnish

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

Page 15: IPC Varnish

7/27/2019 IPC Varnish

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

THE GOOD – JUMP START

Installation

General Information

Default VCL

Page 16: IPC Varnish

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

Page 17: IPC Varnish

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).

Page 18: IPC Varnish

7/27/2019 IPC Varnish

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

Page 19: IPC Varnish

7/27/2019 IPC Varnish

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

Page 20: IPC Varnish

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

Page 21: IPC Varnish

7/27/2019 IPC Varnish

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

Page 22: IPC Varnish

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

Page 23: IPC Varnish

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

Page 24: IPC Varnish

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;

}}

Page 25: IPC Varnish

7/27/2019 IPC Varnish

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

Page 26: IPC Varnish

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.

Page 27: IPC Varnish

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.

Page 28: IPC Varnish

7/27/2019 IPC Varnish

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

Page 29: IPC Varnish

7/27/2019 IPC Varnish

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

Page 30: IPC Varnish

7/27/2019 IPC Varnish

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

Page 31: IPC Varnish

7/27/2019 IPC Varnish

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

Page 32: IPC Varnish

7/27/2019 IPC Varnish

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

Page 33: IPC Varnish

7/27/2019 IPC Varnish

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

Page 34: IPC Varnish

7/27/2019 IPC Varnish

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

Page 35: IPC Varnish

7/27/2019 IPC Varnish

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

Page 36: IPC Varnish

7/27/2019 IPC Varnish

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

Page 37: IPC Varnish

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.

Page 38: IPC Varnish

7/27/2019 IPC Varnish

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

GENERAL ADJUSTMENTS

Common adjustments to make.

Page 39: IPC Varnish

7/27/2019 IPC Varnish

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

Page 40: IPC Varnish

7/27/2019 IPC Varnish

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

Page 41: IPC Varnish

7/27/2019 IPC Varnish

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

Page 42: IPC Varnish

7/27/2019 IPC Varnish

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

Page 43: IPC Varnish

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.

Page 44: IPC Varnish

7/27/2019 IPC Varnish

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

Page 45: IPC Varnish

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.

Page 46: IPC Varnish

7/27/2019 IPC Varnish

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

Page 47: IPC Varnish

7/27/2019 IPC Varnish

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

Page 48: IPC Varnish

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

Page 49: IPC Varnish

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

Page 50: IPC Varnish

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”

Page 51: IPC Varnish

7/27/2019 IPC Varnish

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

Page 52: IPC Varnish

7/27/2019 IPC Varnish

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

Page 53: IPC Varnish

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

Page 54: IPC Varnish

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.

Page 55: IPC Varnish

7/27/2019 IPC Varnish

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

Page 56: IPC Varnish

7/27/2019 IPC Varnish

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

THE CRAZY

ESI – Edge-Side Includes

Varnish Administration

VMOD

Page 57: IPC Varnish

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

Page 58: IPC Varnish

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.

Page 59: IPC Varnish

7/27/2019 IPC Varnish

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

Page 60: IPC Varnish

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>

Page 61: IPC Varnish

7/27/2019 IPC Varnish

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

Page 62: IPC Varnish

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

Page 63: IPC Varnish

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

Page 64: IPC Varnish

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

Page 65: IPC Varnish

7/27/2019 IPC Varnish

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

Page 66: IPC Varnish

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

Page 67: IPC Varnish

7/27/2019 IPC Varnish

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

Page 68: IPC Varnish

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*

Page 69: IPC Varnish

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