Digg Effect Hack (that actually works)

DiggThere is nothing worse than getting recognition on one of the big social networking sites just to see your site come immediately crashing down under the wave of visitors. There are ways to optimize your site to help remedy this but there is no guaranteed fix, no matter how much optimizing you do or how much money you spend on upgrading your server. So what do you do if your site does end up succumbing to the flood of visitors? Here is a simple hack that I have used on my blogs to keep them up under the digg effect without upgrading our server:

(It should be noted: This hack doesn’t take the place of normal optimizations you should be doing anyway, such as caching. It’s more of an emergency measure, in case your optimizations aren’t enough.)

Introducing CoralCDN.

CoralCDN is a decentralized, self-organizing, peer-to-peer web-content distribution network. CoralCDN leverages the aggregate bandwidth of volunteers running the software to absorb and dissipate most of the traffic for web sites using the system. In so doing, CoralCDN replicates content in proportion to the content’s popularity, regardless of the publisher’s resources—in effect democratizing content publication.

In basic terms, CoralCDN can make a copy of your blog entry or web page (including pictures) and host it on these volunteer servers so people can still read your entry! Ideally you would want people to come to your blog directly, but this is the next best thing if your blog does go down.

So how do you use CoralCDN?
To use CoralCDN, a content publisher—or someone posting a link to a high-traffic portal—simply appends .nyud.net:8080 to the hostname in a URL.

By appending .nyud.net:8080 to the end of your host name (i.e. https://www.financialhack.com.nyud.net:8080/your_entry) visitors can reach a copy of that same page. It’s not really important HOW CoralCDN accomplishes this (although if you are interested, you can read more on their website). The important thing is harnessing this awesome service in a useful way.

Enter the unprecedented fix

Okay here’s where we get just a little more technical but bear with me.

On most web servers you can run a command called “uptime”. When you run this command it will return some text to you that will look something like: “09:53:15 up 119 days, 19:08, 10 users, load average: 3.73 7.98 0.50”. It’s at the end of this returned information that we’re most interested in. Load average.

Load average tells you how busy your web site or blog is. The first number, in this case 3.73 indicates how many processes were running (on average) in the last 1 minute. The second number 7.98 indicates the same thing for the last 5 minutes, and the last number indicates the last 15 minutes.

If through some server side code, in our web page, we determined what that first number was (lets say 16.73 for example) and at the same time decided on a maximum load (lets say 15.00 for example as this may be indicative of the “Digg Effect”) we can tell our web page that 16.73 is too high of a server load and that we want to start redirecting the page to the CoralCDN copy! Instead of running hundreds of lines of code, dozens of database calls, loading images, etc (as is the case with most blogs), we run just a few lines of code (thus using far fewer resources) and redirect the visitor to CoralCDN (this time using NO resources). The result is your blog has time to catch up to this huge demand of traffic and visitors never get the dreaded “Page has timed out”.

Since PHP is one of the more commonly used languages for blogs and websites I’ll present the code for just such a hack. This code should be pasted above all other code in your web page. If you are using wordpress, you can place this inside index.php at the very top (just below < ?php ). ------------------------------- $cpuload = get_cpu_load(); $maxload = 15; if((float)trim($cpuload[0]) > $maxload && !strstr($_SERVER[“HTTP_USER_AGENT”], “CoralWebPrx”)) {
header(“Location: http://” . $_SERVER[“SERVER_NAME”] . “.nyud.net:8080” . $_SERVER[“REQUEST_URI”]);

function get_cpu_load() {
$cpuload = shell_exec(“uptime”); //looks like this 09:53:15 up 119 days, 19:08, 10 users, load average: 3.73 7.98 0.50
$cpuload = explode(“:”, $cpuload);
$cpuload = trim($cpuload[4]); //looks like this 3.73 7.98 0.50
$cpuload = explode(“, “, $cpuload);
return $cpuload; //[0] 1 min [1] 5 min [2] 15 min