A Drupal website is using external Rest API service.
The problem is that in a rare occasion when the Rest API service loses database or crashes for whatever reason, that causes the Drupal website to slow down.
Drupal uses many API endpoints frequently so when those are unavailable, the website becomes useless.
What would be a reliable or efficient way to check if the API is available prior to each request?
Thanks.
-
Can you give us a little more detail? Are these API requests part of the work on the server to generate the HTML file, or are these the result of AJAX requests? Are these built-in or custom Drupal Modules making these requests?Greg Burghardt– Greg Burghardt10/18/2017 14:17:51Commented Oct 18, 2017 at 14:17
-
1Those requests are aimed to another web server which hosts Rest API web service. Request are not fired from Javascript, only PHP. It's all about HTML as final result, but sorry, I don't quite understand your question.Đuro Mandinić– Đuro Mandinić10/18/2017 14:28:12Commented Oct 18, 2017 at 14:28
-
Those are custom Modules.Đuro Mandinić– Đuro Mandinić10/18/2017 16:05:20Commented Oct 18, 2017 at 16:05
-
The root cause is that you call an external system within synchronous user request. It kills reliability, scalability and more: medium.com/@wrong.about/…Vadim Samokhin– Vadim Samokhin11/02/2017 07:50:46Commented Nov 2, 2017 at 7:50
-
"Drupal uses many API endpoints frequently so when those are unavailable, the website becomes useless." — This is inaccurate. Drupal doesn't talk to API endpoints by default. It sounds like this Drupal site was customized to be reliant on external APIs. That's probably what you meant, but the original phrasing makes it sound like it's Drupal's fault, which you probably didn't mean :) Also, @Zapadlo is exactly right.Wim Leers– Wim Leers12/30/2017 00:17:20Commented Dec 30, 2017 at 0:17
1 Answer 1
First you need an alternate action for when the service is unavailable.
Then its just a matter of detecting the status of the service. Say, 5 calls time out in a 1 min period. Keep track of this stat and switch to your alternate method when it triggers.
Obvs. you want some automated way of switching it back on again of possible. Maybe a backend service to check every so often, or maybe just start sending requests again after a back off period.
-
Well, since API client uses Curl, I found out that I could set
CURLOPT_CONNECTTIMEOUT
and/orCURLOPT_TIMEOUT
options. That seems like a better alternative.Đuro Mandinić– Đuro Mandinić10/18/2017 14:07:50Commented Oct 18, 2017 at 14:07 -
1I assumed you already had a timeout. the problem with this approach is that a reasonable timeout will slow your app down as each call waits for the timeout before continuingEwan– Ewan10/18/2017 14:10:18Commented Oct 18, 2017 at 14:10
-
Not quite following.. Are you saying that if I define timeout to, say 10 seconds, and if API responds in a fraction of a second, the Curl would be unavailable for another request until those 10 seconds pass?Đuro Mandinić– Đuro Mandinić10/18/2017 14:15:18Commented Oct 18, 2017 at 14:15
-
1no, but your code would wait 10sec before knowing the service wasnt available. presumably atm it waits the default 60sec. so its an improvement.Ewan– Ewan10/18/2017 14:18:57Commented Oct 18, 2017 at 14:18
-
1If this is possible (depends what service we're talking about) ,but maybe try to move request code to frontend , to jquery for example . Is service will not respond in a given time then you can produce some html outputMichał G– Michał G12/27/2017 21:41:03Commented Dec 27, 2017 at 21:41