Varnish Cache

5,316 views

Published on

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,316
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
61
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Varnish Cache

  1. 1. Northeast PHP August 12, 2012Varnish, The Good, The Awesome, and the Downright Crazy By Mike Willbanks Sr. Web Architect Manager NOOK Developer
  2. 2. Housekeeping… • Talk   Slides will be online later! • Me   Sr. Web Architect Manager at NOOK Developer   Prior MNPHP Organizer   Open Source Contributor (Zend Framework and various others)   Where you can find me: • Twitter: mwillbanks G+: Mike Willbanks • IRC (freenode): mwillbanks Blog: http://blog.digitalstruct.com • GitHub: https://github.com/mwillbanks2
  3. 3. Agenda • What is Varnish • The Good : Why…   The quick, easy and hardly informed way… • The Awesome : How…   VCL’s, Directors and more… • The Crazy : Go…   ESI, Purging, VCL C, and VMOD… • Varnish Command Line Apps   varnishtop, varnishstat, etc.3
  4. 4. What is Varnish?Official StatementWhat the hell it meansGraphs, oh my!
  5. 5. Official Statement “Varnish is a web application accelerator. You install it in front of your web application and it will speed it up significantly.”5
  6. 6. What The Hell? Tell me! • Varnish allow you to accelerate your website   By using memory and keeping in mind cookies, request headers and more… • It caches pages so that your web server can RELAX!   What about my apache, tomcat, nginx and (mongrel|thin| goliath….)   Generally caching by TTL + HTTP Headers (cookies too!) • A load banancer, proxy and more…   What? …. Yes, it can do that!6
  7. 7. A General Use Case • CaringBridge Status Server   Getting a message to mobile users.   The system is down, or we want to be able to communicate a message to them about some subject… maybe a campaign.   The apps and mobile site rely on an API • Trouble in paradise? Few and far in between.   Let an API talk to a server…   A story on crashing and burning before varnish.7
  8. 8. The Graph - AWS Req/s Peak Load700 14600 12500 10400 8300 Req/s Peak Load 6200 4100 2 0 0 Small X-Large Small Varnish Small X-Large Small Varnish Time Requests500 80000450 70000400 60000350300 50000250 40000 Time Requests200 30000150 20000100 50 10000 0 0 8 Small X-Large Small Varnish Small X-Large Small Varnish
  9. 9. The Raw Data Small   X-­‐Large   Small  Varnish   Concurrency   10   150   150   Requests   5000   55558   75000   Time   438   347   36   Req/s   11.42   58   585   Peak  Load   11.91   8.44   0.35   19,442   Comments   failed  reqs  9
  10. 10. The Good – Listen Up!InstallmentDocumentationFinding Existing VCL’s
  11. 11. Installment • RTM : http://goo.gl/hl4Tt   Debian: sudo apt-get install varnish   EPEL: yum install varnish • only 6.x otherwise you’ll be out of date!   WOOT Compiling #git • git clone git://git.varnish-cache.org/varnish-cache • cd varnish-cache • sh autogen.sh • ./configure • make && make install11
  12. 12. 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).12
  13. 13. Documentation • Reference Manual   https://www.varnish-cache.org/docs/3.0/reference/index.html • Tutorial – more like a book version of the reference manual   https://www.varnish-cache.org/docs/3.0/tutorial/index.html • Knock yourselves out! There is a ton of documentation • Yes, this makes happy developers.   Documentation is very accurate, read carefully.   Focus heavily on VCL’s, that is generally what you need.   I’m attempting to show you some of how this works but you will require the documentation to assist you.13
  14. 14. Existing VCL’s – The truly lazy… • VCL’s are available for common open source projects   Hi wordpress and drupal! • https://www.varnish-cache.org/trac/wiki/VarnishAndWordpress • https://www.varnish-cache.org/trac/wiki/VarnishAndDrupal   Examples of all sorts of crazy • https://www.varnish-cache.org/trac/wiki/VCLExamples14
  15. 15. Wordpress = Bad Slashdot Bad!!! 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; } }15
  16. 16. The Awesome – Going PlacesVCLDirectorsA Few Examples
  17. 17. VCL’s by Diagram…17
  18. 18. VCL – 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 own VCL • 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.18
  19. 19. VCL – Subroutines – breaking it down. • vcl_init – VCL is loaded, no request yet; VMOD initialization • vcl_recv – Beginning of request, req is in scope • vcl_pipe – Client & backend data passed unaltered • vcl_pass – Request goes to backend and not cached • vcl_hash – call hash_data to add to the hash • vcl_hit – called on request found in the cache • vcl_miss – called on request not found in the cache • vcl_fetch – called on document retrieved from backend • vcl_deliver – called prior to delivery of cached object • vcl_error – called on errors19 • vcl_fini – all requests have exited VCL, cleanup of VMOD’s
  20. 20. VCL - Variables • Always Available • Backend Req Prepartion   now – epoch time   bereq – backend request • Backend Declarations • Retrieved Backend Request   .host – hostname / IP   beresp – backend response   .port – port number • Cached Object • Request Processing   obj – Cached object, can only   client – ip & identity change .ttl   server – ip & port • Response Preparation   req – request information   resp – http stuff20
  21. 21. VCL - Functions • hash_data(string) – adds a string to the hash input.   Request host and URL is default from the default vcl. • regsub(string, regex, sub) – substitution on first occurance   sub can contain numbers 0-n to inject matches from the regex. • regsuball(string, regex, sub) – substitution on all occurances • ban(expression) – Ban all objects in cache that match • ban(regex) – Ban all objects in cache that have a URL match21
  22. 22. Directors • Directors allow you to talk to the backend servers • Directors are a glorified reverse proxy   Allows for certain types of load balancing   Allows for talking to a cluster “A director is a logical group of backend servers clustered together for redundancy. The basic role of the director is to let Varnish choose a backend server amongst several so if one is down another can be used.”22
  23. 23. Directors – The Types • Random Director – picks a backend by random number • Client Director – picks a backend by client identity • Hash Director – picks a backend by URL hash value • Round-Robin Director – picks a backend in order • DNS Director – picks a backend by means of DNS   Random OR Round-Robin • Fallback – picks the first “healthy” backend23
  24. 24. Director - Probing • To ensure healthy backends, you need to use probing.   It really sounds like a colonoscopy for servers; which it is. • Variables   .url   .request   .window   .threshold   .intial   .expected_response   .interval   .timeout24
  25. 25. Example VCL Configuration25
  26. 26. The CrazyESI – Edge-Side IncludesPurgingVMOD
  27. 27. ESI – Edge Side Includes • ESI is a small markup language much like SSI (server side includes) to include fragments (or dynamic content for that matter). • Think of it as replacing regions inside of a page as if you were using XHR (AJAX) but single threaded. • Three Statements can be utilized.   esi:include – Include a page   esi:remove – Remove content   <!-- esi --> - ESI disabled, execute normally27
  28. 28. ESI – By Diagram28
  29. 29. Using ESI • In vcl_fetch, you must set ESI to be on   set beresp.do_esi = true;   By default, ESI will still cache, so add an exclusion if you need it • if (req.url == “/show_username.php”) { return (pass); } • This is a good thing, you may want to cache user information to the right people (aka by cookie value) so that you don’t reload it on every request.   Varnish refuses to parse content for ESI if it does not look like XML • This is by default; so check varnishstat and varnishlog to ensure that it is functioning like normal.29
  30. 30. ESI – By Example <html> <head><title>Rock it with ESI</title></head> <body> <header> <esi:include src="/user_header.php" /> <!-- Dont do this as youd lose the advantage of varnish --> <!--esi <?php include user_header.php; ?> --> </header> <section id="main"></section <footer></footer> </body> </html>30
  31. 31. Purging • The various ways of purging   varnishadm – command line utility • It’s the ole finger in the back of the throat   Sockets (port 6082) – everyone likes a good socket wrench • Sure, Ipecac is likely overkill.   HTTP – now that is the sexiness • A few headers, nothing forced.31
  32. 32. Purging Examples varnishadm -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.org32
  33. 33. Distributed Purging • Distributed Purging… like a sorority party.   Use a message queue (or gearman job server)   Have a worker that knows about the varnish servers   Submit the request to clear the cache in the asynchronously or synchronously depending on your use case. • Have enough workers to make this effective at purging the cache quickly.   This will make it far easier to scale; you can either store the servers in a config file, database or anything else you think is relevant.33
  34. 34. Embedding C in VCL – you must be crazy • Before getting into VMOD; did you know you can embed C into the VCL for varnish? • Want to do something crazy fast or leverage a C library for pre or post processing? • I know… you’re thinking that’s useless..   On to the example; and a good one from the Varnish WIKI!34
  35. 35. VCL - Embedded C for syslog – uber sexy C{ #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:")); }C35
  36. 36. VMOD – Varnish Modules / Extensions • Taking VCL embedded C to the next level • Allows you to extend varnish and create new functions • Now, if you are writing modules for varnish you have a specialty use case!   Go read up on it!   https://www.varnish-cache.org/docs/trunk/reference/vmod.html36
  37. 37. VMOD - std • The VMOD std is shipped with varnish; it provides some useful commands   toupper   syslog   tolower   fileread   set_up_tos   duration   random   integer   log   collect37
  38. 38. Varnish Command Line Appsvarnish varnishadm varnishhistvarnishlog varnishncsa varnishreplayvarnishsizes varnishstat varnishtest varnishtop
  39. 39. What is Varnish doing… • What is varnish doing right now? • How do I debug what is happening?   varnishtop39
  40. 40. What is Varnish doing…40
  41. 41. Logging • Many times people want to log the requests to a file   By default Varnish only stores these in shared memory.   Apache Style Logs • varnishncsa –D –a –w log.txt   This will run as a daemon to log all of your requests on a separate thread.41
  42. 42. Logging42
  43. 43. Cache Warmup • Need to warm up your cache before putting a sever in the queue or load test an environment?   varnishreplay –r log.txt • Replaying logs can allow you to do this. This is great for when you are going to be deploying code to check for performance issues.   Although… be careful so that you don’t POST data or create data on peoples accounts. Maybe cat the file and remove anything that executes on data.43
  44. 44. Cache Hit Ratios? No Problem • How to see your cache hit ratios…   varnishstat • Want to parse them from XML so you can create a sexy administration panel?   varnishstat –x44
  45. 45. Cache Hit Ratios? No Problem45
  46. 46. 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

×