How to stop requesting an external API with 504 time-out response in PHP codebase

1.1k views Asked by At

I'm using curl to request a free external 3rd party API in my PHP application. When there are too many requests at a time, it goes down and shows 504 Gateway time-out. Is there any option, when a request has no response in more than 5s then it would stop waiting for API response & go for the next statements?

As I know while adding an external API in an application, becomes a dependency. Need your attention & guidance.

2

There are 2 answers

0
Svetlozar Stoyanov On

What happens is that the 3rd party web server triggers “self-defense” by returning 504 error, when can’t handle certain amount of requests per minute. To avoid that you need to honor their quality of service (QoS) by throttling down the number of requests right before reaching the limit or after you get the error. You can do that by:

  1. Find how many request per minute the 3rd party site can handle either by a) looking at their docs or asking, b) checking the result from the API how many requests you are allowed to do in a minute or by c) experimenting and measuring it.
  2. Use some kind of queue that all requests for that API will be added to to it and a code that will handle messages once there is message in the queue, then call the API, then remove from the queue or move to failed_queue. For queue you could use external library or create a simple table that saves messages as records. Code checking and handling the queue could be executed on a corn job.
  3. Count how many requests you send so far and the last time since when the previous request was send. Check, if you if the requests are less than the limit-1. If is less execute the API requests, if it’s more or 504 was received than wait 1 minute, e.g.’sleep(60)’, after that send a request again.

It’s better to avoid 504 before it happens, because the API website could return it for longer time, than the time needed to prevent it.

0
James McCaughey On

To expand on what Svetlozar said, if you receive a timeout due to the service being unavailable because of an exorbitant number of requests, and you skip to the next curl request, you are simply sending more requests while the service is still unavailable. This could lead to an extended downtime as well as prevent you from getting the desired results.

As Svetlozar suggested, you could and should throttle your requests. Determine the threshold and ensure that you stay below it.

Depending on the API and your specific use case, particularly how up-to-date your data needs to be, you may also want to consider caching the results and retrieving data from the cache before making direct API calls. Then, you can set an expiration time for your cached results to ensure your data is not too outdated. This approach can help reduce the number of rapid-fire requests.

I implemented caching for a caller ID API. Every time a call came in, the phone server would contact my internal server to request the Caller ID information. If my server had cached the data, it would provide it; otherwise, it would send a request to the API. Data older than one month would be automatically pruned. This significantly reduced the number of API requests needed, especially for repeat callers.