Design: How to Solve 503 Error of API when Resources are not available?

Design: How to Solve 503 Error of API when Resources are not available?

The HTTP 503 Error indidicates the server is busy. It might be due to the fact that the servers are overloaded (high load average).

One of my API is designed to return 503 Error when the cached file is not available. It is designed as the follows:

1
2
3
4
if (!is_file($file)) {
throw_503();
die();
}

The $file will be updated periodically e.g. every minute via Crontab. When the OS is writing to that file, the is_file will return false because the file is in-use.

One way of solving this is to have multiple caching versions, to avoid the single point of failure. For example, if a file-1 is not available we can check for file-2. These two files should not be updated at the same time.

Another way to resolve this issue is to add a delay check (hacky solution), for example:

1
2
3
4
5
6
7
if (!is_file($file)) {
sleep(1); // delay check.
}
if (!is_file($file)) {
throw_503();
die();
}

Every little helps! I hope this helps!

Steem On!~

*Reposted to Blog

If you like my work, please consider voting for me, thanks!
https://steemit.com/~witnesses type in justyy and click VOTE



Alternatively, you could proxy to me if you are too lazy to vote!

Also: you can vote me at the tool I made: https://steemyy.com/witness-voting/?witness=justyy

Visit me at: https://steemyy.com


This page is synchronized from the post: ‘Design: How to Solve 503 Error of API when Resources are not available?’

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×