-1

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.

Đuro Mandinić
  • 216
  • 1
  • 7
  • 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 Oct 18 '17 at 14:17
  • 1
    Those 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ć Oct 18 '17 at 14:28
  • Those are custom Modules. – Đuro Mandinić Oct 18 '17 at 16:05
  • The root cause is that you call an external system within synchronous user request. It kills reliability, scalability and more: https://medium.com/@wrong.about/wrong-ways-of-defining-service-boundaries-d9e313007bcc – Vadim Samokhin Nov 02 '17 at 07: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 Dec 30 '17 at 00:17

1 Answers1

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.

Ewan
  • 70,664
  • 5
  • 76
  • 161
  • Well, since API client uses Curl, I found out that I could set `CURLOPT_CONNECTTIMEOUT` and/or `CURLOPT_TIMEOUT` options. That seems like a better alternative. – Đuro Mandinić Oct 18 '17 at 14:07
  • 1
    I 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 continuing – Ewan Oct 18 '17 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ć Oct 18 '17 at 14:15
  • 1
    no, but your code would wait 10sec before knowing the service wasnt available. presumably atm it waits the default 60sec. so its an improvement. – Ewan Oct 18 '17 at 14:18
  • I see. Yes, that would be great. Because, for various legacy reasons, the website in question has the default PHP time limit set to 15 minutes! :-P – Đuro Mandinić Oct 18 '17 at 14:33
  • 1
    If 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 output – Michał G Dec 27 '17 at 21:41