1

In php I would like to serve a customized error message before any further processing.

Here is what I have

if($overloaded==true){
    header('HTTP/1.0 503 Service Temporarily Unavailable');
    header('Status: 503 Service Temporarily Unavailable');
    header('Retry-After: 300');//300 seconds
    die();
}

from: How do I make php page return a 503 error (or anything non-200)

About 503 Service Unavailable on w3.org :

  Note: The existence of the 503 status code does not imply that a
  server must use it when becoming overloaded. Some servers may wish
  to simply refuse the connection.

from: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4

How should I serve an HTTP request when my server is overloaded?

Community
  • 1
  • 1
RafaSashi
  • 16,483
  • 8
  • 84
  • 94
  • Status codes are fairly opinion based in these circumstances. My question would be how are you determining the server is overloaded? 503 seems to be the obvious choice if you have some sort of way to determine your servers are actually overloaded, but 504 would also work if the overload is determined by a timeout. – Devon Bessemer Nov 21 '14 at 17:23
  • You are right it is opinion based but I am looking for multiple alternatives in this case. I don't want to retrive some error message after a long waiting time – RafaSashi Nov 21 '14 at 17:25
  • 1
    The status code has no real correlation with the code or the waiting time. You can issue whatever status code you want. It is up to the client to interpret that status code for what it is, you can create your own guidelines for end users if one status code doesn't exactly fit what you want. The standardized status codes are just an easy way for clients to universally recognize specific problems when connecting to an application/site. – Devon Bessemer Nov 21 '14 at 17:31
  • Really good stuff you provided, both of you thanks! this is how to determine the server is overloaded http://php.net/manual/en/function.sys-getloadavg.php – RafaSashi Nov 21 '14 at 17:41
  • 1
    I don't know if I would rely on the load average alone to determine if a server is overloaded. I mean if you have a load average of 80 on a quad-core processor, then sure, it is overloaded by a long shot, but a load average of 25 on a 24 processor server could mean it is 100% loaded or it could be handling just fine. It isn't a perfect science. – Devon Bessemer Nov 21 '14 at 17:43
  • Of course Devon. I am not going to use 80 like in the example. I have benchmarked it and I will use it only in extreme cases (+ a security margin :p) as you said It isn't a perfect science but I am learning it anyway – RafaSashi Nov 21 '14 at 17:56

1 Answers1

2

If you want to be accurate, you really only have 2 choices as far as I'm aware, and that's 503 or 500.

500 means you weren't able to fulfill the request for an unexpected condition/reason.

503 means you weren't able to fulfill the request due to a temporary overloading or maintenance of the server.

From what I know Error 500 is a very generic error and that 503 is most accurate and I would highly recommend you use that over 500. Accuracy is always best.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • thanks. I was just trying to clarify the "Some servers may wish to simply refuse the connection" because it doesn't sound like a 5xx error – RafaSashi Nov 21 '14 at 17:30
  • and in the case I send the 503 directly what should I do whith header('Retry-After: 300');//300 seconds – RafaSashi Nov 21 '14 at 17:31
  • 1
    Take a look at this question here http://stackoverflow.com/questions/3764075/retry-after-http-response-header-does-it-affect-anything - Keep in mind the answer is from 2010. –  Nov 21 '14 at 17:32
  • 2
    @RafaSashi in other words, it depends. There are very few cases where you would need a retry-after. In most cases, users are likely to refresh on their own if a page doesn't load. In other cases, programs are likely to try again at a later date if they fail to connect. I can't think of many situations where you're going to have a client waiting around for the Retry-After header to take effect even if it is properly implemented. – Devon Bessemer Nov 21 '14 at 17:35