Wednesday, August 15, 2007

Web Hosting HOW TO: Save your server AND customer when digg/reddit/slashdot effect hits his/her website

So you're running a web hosting business. You have lots of loyal customers, some of them are with you for the past 8 years. One of your customers gets lucky - a huge burst of traffic hits his website. If you don't do something quickly, your server will go down pretty soon. What's your call? "Suspend website, of course". WRONG! Get this - almost EVERY webmaster dreams of getting to the front page of digg, reddit or slashdot. If you have a reputation of suspending everyone who gets to the frontpage, you are to be avoided. Instead, consider other ways to solve this problem. I will share one method with you, we were successfully using it when I was working in one web hosting company a year back. It's a very simple method and takes only a minute to do.

Your customer's website is dynamic, probably running a heavy script which is why your server can't handle the traffic. You simply need to make it static (not all of the website, just the page which is being hit at the moment).

So here's how you can do this:
  1. Save your customer's dynamic page (which is under heavy load at the moment) as a static .html file
  2. Edit your apache config or customer's .htaccess file. You'll need to turn that dynamic page into static one:
    instead of opening
    it should open
    So when somebody visits this page, static file will be served instead of dynamic even though you see .php in the URL - no php and no MySQL will be touched.
    We'll use mod_rewrite for that. Here's what you need to put to .htaccess file:
    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^page=AwesomeArticle(.*)$
    RewriteRule ^index.php$
  3. Save it and watch your server going back to life. All your customer's website is dynamic except this page.
You can now e-mail your customer informing of what you've done to save his/her website and congratulating on getting to the front page. You have just saved your server and most importantly - your customer!

Once the traffic gets back to normal, you can remove rewrite rules.

There are other ways to solve this problem - making a mirror, offloading to other server, etc.. It doesn't matter how you choose to solve this problem as long as you don't suspend website in the most important time of it's life.

Note: this is a repost to my previous article "Digg effect took down your website. How to come back online with only a few lines of code.".