{"id":631,"date":"2009-04-28T00:41:09","date_gmt":"2009-04-28T04:41:09","guid":{"rendered":"http:\/\/cd34.com\/blog\/?p=631"},"modified":"2009-04-28T00:43:06","modified_gmt":"2009-04-28T04:43:06","slug":"varnish-saves-the-day-maybe","status":"publish","type":"post","link":"https:\/\/cd34.com\/blog\/scalability\/varnish-saves-the-day-maybe\/","title":{"rendered":"Varnish saves the day&#8230;. maybe"},"content":{"rendered":"<p>We had a client that had a machine where apache was being overrun&#8230; or so we thought.\u00a0 Everything pointed at this one set of domains owned by a client and in particular two sites with 100+ elements on the page.\u00a0 Images, css, javascript and iframes composed their main page.\u00a0 Apache was handling things reasonably well, but, it was immediately obvious that it could be better.<\/p>\n<p>The conversion to <a href=\"http:\/\/varnish.projects.linpro.no\/\">Varnish<\/a> was quite simple to do even on a live server.\u00a0 Slight modifications to the Apache config file to listen to port 81 on the set of domains in question, and a quick restart.\u00a0 Varnish was configured to listen to port 80 on that particular IP and some minor modifications were made to the startup.vcl file to modify things slightly:<\/p>\n<blockquote><p>sub vcl_fetch {<br \/>\n&nbsp;&nbsp;if (req.url ~ &#8220;\\.(png|gif|jpg|swf|css|js)$&#8221;) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;set obj.ttl = 3600s;<br \/>\n&nbsp;&nbsp;}<br \/>\n}<\/p><\/blockquote>\n<p>A one hour cache should be granular enough to do a bit more good on these sites, overriding the default of two minutes.\u00a0 After an hour, it was evident that the sites did peform much more quickly, but, we still had a load issue.\u00a0 Some modifications of the apache config alleviated some of the other load problems after we dug further into things.<\/p>\n<p>After 5 hours, we ended up with the following statistics from varnish:<\/p>\n<pre>\r\n0+05:18:24\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 xxxxxx\r\nHitrate ratio:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 10\u00a0\u00a0\u00a0\u00a0\u00a0 100\u00a0\u00a0\u00a0\u00a0 1000\r\nHitrate avg:\u00a0\u00a0\u00a0\u00a0 0.9368\u00a0\u00a0 0.9231\u00a0\u00a0 0.9156\r\n\r\n62576\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1.00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 3.28 Client connections accepted\r\n466684\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 57.88\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 24.43 Client requests received\r\n411765\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 48.90\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 21.55 Cache hits\r\n148\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.01 Cache hits for pass\r\n32018\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7.98\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1.68 Cache misses\r\n54761\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8.98\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2.87 Backend connections success\r\n0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.00\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0.00 Backend connections failures\r\n45411\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7.98\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2.38 Backend connections reuses\r\n48598\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7.98\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2.54 Backend connections recycles\r\n<\/pre>\n<p>Varnish is doing a great job.\u00a0 The site does load considerably faster, but, it didn&#8217;t solve the entire problem.\u00a0 It did reduce the number of apache processes on that machine from 450 to 170 or so, freed up some ram for cache, and did make the server more responsive, but, it probably only contributed to 50% of the issue.\u00a0 The rest of it was cleaning up some poorly written php code, modifying a few mysql tables and adding some indexes to make things work more quickly.<\/p>\n<p>After we fixed the code problems, we debated removing Varnish from their configuration.\u00a0 Varnish did buy us time to fix the problem and does result in a better experience for surfers on the sites, but, after the backend changes, it is hard to tell whether it makes enough impact to keep a non-standard configuration running.\u00a0 Since it is not caching the main page of the site and is only serving the static elements (the site sets an expire time on each generated page), the only real benefit is that we are removing the need for apache to serve the static elements.<\/p>\n<p>While testing another application, we were able to override hardcoded expire times and forcing a minimally cached page.\u00a0 Even if we cached a generated page for two minutes, it could be the difference between a responsive server and a machine struggling to keep up.\u00a0 Since WordPress, Joomla, Drupal and others set expire times using dates that have passed, they ensure that the site html being output is not cached.\u00a0 Varnish allows us to ignore that, and to set our own cache time which could save a site hit with a lot of traffic.<\/p>\n<blockquote><p>sub vcl_fetch {<br \/>\n&nbsp;&nbsp;if (obj.ttl &lt; 120s) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;set obj.ttl = 120s;<br \/>\n&nbsp;&nbsp;}<br \/>\n}<\/p><\/blockquote>\n<p>would give us a minimum two minute cache which would cut the requests to a dynamically generated page considerably.<\/p>\n<p>It is a juggling act.\u00a0 Where do you make the tradeoff and what do you accelerate? Too many times the solution to a website&#8217;s performance problem is to throw more hardware at it.\u00a0 At some point you have to split the load on multiple servers, adding new bottlenecks.\u00a0 An application designed to run on a single machine becomes difficult to split to two or more machines, so, many times we do what we can to keep things running on a single machine.<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/scalability\/varnish-saves-the-day-maybe\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>We had a client that had a machine where apache was being overrun&#8230; or so we thought.\u00a0 Everything pointed at this one set of domains owned by a client and in particular two sites with 100+ elements on the page.\u00a0 Images, css, javascript and iframes composed their main page.\u00a0 Apache was handling things reasonably well, [&hellip;]<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/scalability\/varnish-saves-the-day-maybe\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[9,21],"class_list":["post-631","post","type-post","status-publish","format-standard","hentry","category-scalability","tag-apache","tag-varnish"],"_links":{"self":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":3,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"predecessor-version":[{"id":634,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/631\/revisions\/634"}],"wp:attachment":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}