{"id":6824,"date":"2010-01-31T09:11:23","date_gmt":"2010-01-31T13:11:23","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=6824"},"modified":"2010-01-31T19:52:13","modified_gmt":"2010-01-31T23:52:13","slug":"why-my-website-goes-down","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=6824","title":{"rendered":"Why My Website Goes Down"},"content":{"rendered":"<p>It&#8217;s time to solve a mystery. (A boring technical coding mystery.)<\/p>\n<p>It&#8217;s been bothering me for years: Why does my site go down when I get linked on Reddit or Stumbleupon? It shouldn&#8217;t.  I use <a href=\"http:\/\/hostingmatters.com\/\">Hosting Matters<\/a>, and they host blogs that are far larger than mine without difficulty.   Usually my site will just vanish for a few hours, and after the fact I&#8217;ll be able to sort out what happened by looking at the incoming traffic.<\/p>\n<p>Eventually I noticed that not all traffic surges were created equal.  It was really only the ones that linked to <a href=\"?cat=14\">DM of the Rings<\/a> that crushed my site.  Links to other stuff wouldn&#8217;t even cause a hiccup, even if the overall traffic spike was larger. The natural assumption is that it must be due to the fact that the comic is image-heavy.  Except, this problem persisted even when the images were moved off-site. Hmmm. What about the scripts that set up all the comic navigation stuff? That seems like a stretch. Perhaps the script I wrote that embeds images so I don&#8217;t have to clutter up my prose with a lot of HTML? Meh.  It might be inefficient, but I can&#8217;t imagine it&#8217;s anywhere near bad enough to bring down the site where the average comic was one or two images.  But what else could it be?  What else makes the comics different from the other posts on my site?<\/p>\n<p>I&#8217;d ponder this problem for a day or so and then forget all about it until the next time my site went down. This cycle has been repeating itself for a couple of years now.<\/p>\n<p>I&#8217;ve finally figured it out. And I am really embarrassed at how long it took me.<\/p>\n<p>It&#8217;s the number of comments. Also: Duh.<\/p>\n<p>I was always looking at the early comics, where the comment count is a couple dozen or so per entry. Later in the series the comments regularly top a hundred, and the finale clocks in at just under seven hundred comments. <\/p>\n<p>I run some tests on my local machine, where I have a functional mirror of this blog. It takes my machine an agonizing <strong>three and a half seconds<\/strong> to generate the page for the finale.  Now, I&#8217;m sure the server at Hosting Matters is many times faster than my humble computer, but 3.5 seconds is still insane. Get a few thousand people in there chewing through those high-comment posts, and it&#8217;s easy to see how they could render the machine helpless.  I try disabling the display of comments and it is able to generate the page in just 0.14 seconds. So 96% of the CPU time is spent churning out the HTML for the comments, and the other 4% is spent on <em>everything else<\/em>. <\/p>\n<p>But why? Why is it taking so long to list the comments? My first thought is that this is caused by my dice roller. The code to display the dice for a comment is a recursive operation that subtracts from a number and then calls itself until the number is zero.<\/p>\n<p>As of this writing, the current comment count on the DM of the Rings finale is 678.  This requires ten levels of recursion. (Six 100 sided dice and four 20 sided dice.)  Keep in mind it has to roll a handful of dice for each and every comment in the list, with the number of dice needed gradually going up. <\/p>\n<p>Lots of dice times lots of comments time lots of comment-heavy posts = a crap-ton of HTML.  <\/p>\n<p>Every comment has a bunch of HTML around it.  Nested DIV tags and SPAN tags and TABLE tags and&#8230; wow. This is really messy.  There is a lot of extra stuff in here.  I remember how this came about: Most of it was for cross-platform reasons.  The differing behaviors of IE 6, IE 7, and Firefox were crazy to sort through, and often making very slight changes would blow out the formatting on one browser or another.  Once I had it working, I stopped messing with it because I was pissed off and sick of fighting with it. So I left this crufty mess in place, even though I most likely don&#8217;t need the majority of it.<\/p>\n<p>Just to make sure I&#8217;m getting a clean test, I remove ALL of this extraneous HTML.  I just have a single line-break between comments, but otherwise they are a big jumble. That&#8217;s fine. I&#8217;ll sort that out later. Finally, I disable the comment editing plugin and SuperCache so that I can be sure they aren&#8217;t part of the measurement.  <\/p>\n<p>The code that is left is as streamlined as it gets: WordPress yanks comments out of the database and spews them onto the page without doing anything else. It now takes almost exactly 2 seconds to build the page. Nice, but I have a feeling there is still a really big bottleneck in here someplace.<\/p>\n<p>Time to get systematic.  Each comment has several distinct parts:<\/p>\n<p>1) The comment number<br \/>\n2) The dice<br \/>\n3) The avatar<br \/>\n4) The name \/ link of the author<br \/>\n5) The permalink of the comment itself, which is wrapped around&#8230;<br \/>\n6) The date on which the comment was made and&#8230;<br \/>\n7) The time of day of the comment<br \/>\n8) An admin edit link (just for me)<br \/>\n9) The actual comment text<\/p>\n<p>Hm.  So which of these steps would be the most time consuming? My primary suspects are #2, #3, and #9.  Time to run some tests.  I&#8217;m going to disable an item, reload the page a few times to see how much time it saves, then re-enable it and run the same test for the next one.  This test isn&#8217;t super-accurate. Values jump around in 0.05 increments, but considering how huge our bottleneck is this shouldn&#8217;t be a problem.<\/p>\n<p>But let&#8217;s see how it goes&#8230;<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/chart_comments.jpg' class='insetimage'   alt='chart_comments.jpg' title='chart_comments.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Uhbuhwhut?<\/p>\n<p>One of my prime suspects &#8211; my dice roller &#8211; is so fast that it&#8217;s basically &#8220;free&#8221;. (I give myself a pat on the back.) #9, the comment text, is indeed a high priced thing to process. But the absurd surprise is that #6 and #7 are really expensive.  Together, it takes longer to show the date and time of when you left a comment then it does to display the actual text! What!?!<\/p>\n<p>I am at a loss, here. Suddenly, <a href=\"http:\/\/www.youtube.com\/watch?v=EGC09B810Yk\">everything I know is wrong<\/a>. <\/p>\n<p>While I can&#8217;t explain why it&#8217;s taking so long to do #6 and #7, I can clearly see that I don&#8217;t need to do them both.  In both cases, I&#8217;m retrieving the timestamp, I&#8217;m just printing it differently.  It&#8217;s like if your mom asked you what the date is, so you decide to walk five miles into town and look at the marque at he bank.  After you walk all the way back home and tell her, she sends you back to the bank to find out what time it is. I don&#8217;t know why it&#8217;s taking so long, but at least we can get both nuggets of data on the same overlong trip. <\/p>\n<p>That shaves a nice chunk off of the loading time. <\/p>\n<p>The next thing I notice is that there are two ways I can get the comment text. I can do it the way I&#8217;ve been doing it:<\/p>\n<p><code>comment_text ();<\/code><\/p>\n<p>Or I can replace it with a very similar line of code:<\/p>\n<p><code>echo get_comment_text ();<\/code><\/p>\n<p>The first is what takes over 0.6 seconds. <em>The second happens so fast I can&#8217;t even measure it with the benchmarking tools I&#8217;m using. It is, in effect, &#8220;free&#8221;.<\/em><\/p>\n<p>This is one of those changes that seems a little too good to be true. I must be giving up <em>something<\/em> by switching to the new way. But what? How do comment_text () and get_comment_text () differ?<\/p>\n<p>I have looked in the <a href=\"http:\/\/codex.wordpress.org\/Function_Reference\">WordPress docs<\/a>, and found nothing. As in: Neither one is even listed. I looked in the support \/ discussion forums, and the WordPress community is its usual helpful self.  In fact, <a href=\"http:\/\/wordpress.org\/support\/topic\/352678\">someone actually asked my question<\/a>.<\/p>\n<p>Alan: Hey, what&#8217;s the difference between comment_text () and get_comment_text ()?<\/p>\n<p>Betty: Have you tried looking them up in the docs?<\/p>\n<p>Alan: Uh. Yeah? Nothing there?<\/p>\n<p>Betty: Did you try reading the source?<\/p>\n<p>I cannot stress how useless it is to post RTFM in a forum like this. If you don&#8217;t know, just keep your yap shut and maybe someone else will come along and help. Of <em>course<\/em> you can read the source. It&#8217;s time consuming and you&#8217;re most likely consulting the docs and forums in an attempt to learn from someone who has already done so. <strong>This is why the forum exists.<\/strong>  <\/p>\n<p>Perhaps I just have bad luck, but in all my trips to WordPress.com, this has been my experience <em>every time<\/em>. Nobody knows, and everyone tells you to read docs that don&#8217;t exist.<\/p>\n<p>Sigh. <em>Anyway.<\/em><\/p>\n<p>Item #8 is also inexplicably expensive. Its only job is to print <em>nothing<\/em> to every single visitor to the site, unless the viewer is me, in which case it needs to print a little link.  I never click on that link, so this element can be removed. I have no idea why it eats so much time, but at least I can cut it.<\/p>\n<p>We&#8217;re now down to the page taking 0.85 seconds.  That&#8217;s a huge improvement, but it really bothers me that just printing the stupid date takes <strong>half<\/strong> of that. <\/p>\n<p>I check into it some more, and it look like all the time is being eaten when converting date formats. Apparently the date of each comment is stored thusly:<\/p>\n<p><code>2009-03-29 15:44:10<\/code><\/p>\n<p>But of course it&#8217;s much nicer to display that as:<\/p>\n<p><code>March 29th, 2009 03:03pm<\/code><\/p>\n<p>But the latter costs me 0.4 seconds.  I can double the speed of this code by sacrificing usability. It seems like the thing to do is to keep the nice dates on posts with less than a hundred comments.  Once a post passes this, it will start cutting corners and display the all numeric date.  <\/p>\n<p>(Remember, when I say 0.4 seconds, I&#8217;m talking about processing the 600+ comment page, not just a single comment. The differences would be too small for me to measure if I was only working with one comment at a time.)<\/p>\n<p>I make some other minor changes, and in the end the page loading happens in about 0.31 seconds.  It&#8217;s more than ten times faster than it was when I started.<\/p>\n<p>I haven&#8217;t made the changes live yet.  While mucking about with this mess I discovered that WordPress now supports threaded discussions.  We need this here. We have large, diverse, and multi-threaded conversations on this site.  <\/p>\n<p>Still, it&#8217;s clear this place needs an update before the next traffic surge. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s time to solve a mystery. (A boring technical coding mystery.) It&#8217;s been bothering me for years: Why does my site go down when I get linked on Reddit or Stumbleupon? It shouldn&#8217;t. I use Hosting Matters, and they host blogs that are far larger than mine without difficulty. Usually my site will just vanish [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-6824","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/6824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6824"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/6824\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}