Tuesday, June 19, 2007

Digg effect took down your website. How to come back online with only a few lines of code.

So you have a really powerful and expensive server but you haven't done any optimizations for your website which is probably a reason why it's down now, after only 30 diggs. Don't worry, there's a quick fix to it and only takes a couple of minutes to do.

Your website is dynamic (probably PHP & MySQL). Each time somebody visits a page, your server has to run a php script which would connect to MySQL database and run some queries. Average PHP script would eat so many resourses that without any optimization, Digg Effect would be fatal. But what if instead of that dynamic page you'd have a static .html file there? No need to hit php, no need to hit MySQL - it would run even faster than a script with a really good optimization and much more faster than your current script (maybe up to 50 times, or up to 100 times - depends on how bad is your script) which simply means more visitors can see your page.

Let's say your URL is: http://MySuperWebsite.com/index.php?page=AwesomeArticle

So here's how we'll do this:
  1. Save your dynamic page (which is under heavy load at the moment) as a static .html file
  2. Edit .htaccess file. You'll need to turn your dynamic page into static:
    instead of opening http://MySuperWebsite.com/index.php?page=AwesomeArticle
    it should open http://MySuperWebsite.com/AwesomeArticle_static.html
    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 your .htaccess file:
    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^page=AwesomeArticle(.*)$
    RewriteRule ^index.php$
    AwesomeArticle_static.html
  3. Save it and watch your website getting back online. All your website is dynamic except this page. This is just a temporary fix. You can now focus on optimizing your scripts.

 


Put a Poll in your Sig - SigPoll.

11 comments:

Anonymous said...

It would be best to avoid this situation in the first place. Why not make all your content static?

Paul said...

Yes - it would be best to avoid it. But if it happens then I believe that's the fastest way to bring your website back online (optimization may take days and the digg effect is happening now).

You can't make all your content static if your website is, let's say, a forum. When one of your forum pages is being dugg and it couses problems, you can just make it static. Of course, it will make difficult for new comments to appear on this thread, but at least your forum will be online.

Anonymous said...

Isn't this the job of the cache on your web server?

Paul said...

It is. Unless your website is fairly dynamic and you haven't done enough optimization. This post is not about how you should prepare for digg effect but what you can do if it happens when you're not prepared yet.

Drew said...

I'll stick with memcached .

Simona said...

I read this on Hacker Zen a month ago.

http://www.hackerzen.com/05-2007/survive-server-traffic-storm

10 ways to survive a traffic storm. Of course more importantly is having proper hosting and using caching.

"You'll probably want to use this as a last resort."

Simona said...

the linkified article

Paul said...

Simona, I checked your link. That article suggests redirecting traffic to off-site cached page which is different than my suggestion (I suggest converting dynamic page to a static one, not redirecting) but it's also a quite good solution. But yes, it should only be used as the last resort.

Jeff said...

You should always use a mod rewrite for dynamic pages. They are way easier for search engine to index, which increases the traffic to your site.

richard said...

Nice tip, Paul. It seems the other comments on your thread didn't get what you were trying to say but I did.

Anonymous said...

I thought it was wealthy to be some unexciting old post, but I’m contented I visited. I will mail a connect to this area on my blog. I fancy my visitors purpose light upon that very useful.