Main Content

Handling failures / absences of your backend server nicely

Archive - Originally posted on "The Horse's Mouth" - 2013-02-08 18:09:12 - Graham Ellis

It's good for your website to put a professional smile on it's face, even when it's having problems with its backend!

On high traffic sites (for example - TV show and news sites), backend servers can occasionally get overloaded and you'll want your web site visitor to get a message to that effect, rather than a failure. If the feed is a regular news feed, you might even be able to respond with the latest (cached) news if it's less that a few minutes old, and your user will be none the wiser!

Here's a demonstration of one way to do this - setting up a front end Apache httpd to divert requests it would normally pass on to a proxy to a local PHP script if the proxy is not available. Simply add something like
  ErrorDocument 503 "/proxydown.php"
into the httpd.conf file, a file it includes, or an appropriate .htaccess file.

Then within that PHP script, you can force a 200 (OK) header to be returned:
  header("HTTP/1.1 200 OK");
and the PHP script can work out what's running, and what was asked for:
  $requested = $_SERVER["REQUEST_URI"];
  $gotinstead = $_SERVER["PHP_SELF"];
may give when you print out those variables
  You called for /dryfeet but it is not available
  You got instead /proxydown.php

This example is a demonstration which I've written to show the sort of thing that can be done when perhaps you have httpd fronting a number of Tomcat servers, and you want the httpd to catch and control any times that it fails to contact a Tomcat. The complete demonstration is [here] - that's a basic framework into which you can add logging, caching, emailin the system adminsitrator if the problem persists, and lots of other facilities.