How to implement a ‘fail whale’ to graciously limit server load (Solutions for system capacity governing)

With our website, we want to make it so that if the site is about to go down, it instead displays a custom 503 (based on a server load limit set by us). Is there something that can handle this that is already built in, or a plugin that might help?

I know manual switching maintenance mode plugins are available but if the site hits heavy traffic they will get taken out.

Related posts

Leave a Reply

4 comments

  1. Twitter and other high-volume sites probably do this one layer ahead of the servers. Probably with a load balancer that can detect the server load over all servers and if the load is too high (when machines stop replying) they redirect traffic to a server that returns only the “fail whale” page.

    This answer on Stack Overflow suggests to look at sys_getloadavg() and display an error when it is too high (and “too high” should be defined by trial and error). I would do this in the main index.php file that “boots” WordPress, to get a quick exit and not put more load on the server.

  2. If your system is on linux, you can read the actual server load from /proc/loadavg

    For example:

    $load = reset ( explode ( ' ' , file_get_contents('/proc/loadavg') ) );
    
    if ($load > 3.14 )
    {
        // load too high, 
        header('HTTP/1.1 503 Service Temporarily Unavailable');
        header('Status: 503 Service Temporarily Unavailable');
        header('Retry-After: 60');
        fail_whale();
    }
    

    EDIT:
    added header method lines.

  3. You can customize message for when database is down by drop-in db-error.php in wp-content directory.

    But if PHP is down I do not think there is anything that WP or its plugin would be able to do. It is better to ask your hosting how can you customize server’s 503 page.

  4. In your .htaccess file, add this line:

    ErrorDocument 503 /error503.html

    Then make an error503.html file in the root of the site that has your custom stuff in it.

    This may or may not work depending on how your hosting is configured.