2

Current I have an application that makes 3rd party API calls, and the data returned is the primary data I display on the webpage.

If the API is down, my website currently goes down (crashes from timeouts, and also eventually brings IIS down).

What options are there for me to take to prevent the website from going down?

I am leaning towards some sort of monitoring service, that if the API is down, I redirect to page explaining my application is also down.

loyalflow
  • 14,275
  • 27
  • 107
  • 168

4 Answers4

2

If possible I would personally store the information and sync it every X mins based on your requirements.

If the API is down then you still have the last lot of sync'd data which means no down time.

EDIT

If you need real time data and would like to forward to a page when the webservice isn't available then you can do something like:

internal bool WebServiceAvailable()
{
    bool Result = false;
    string Url = {ENTER URL}
    try
    {
        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(Url);
        Req.Timeout = 3000;

        using (HttpWebResponse Rsp = (HttpWebResponse)Req.GetResponse())
        {
            if (Rsp.StatusCode == HttpStatusCode.OK)
            {
                Result = true;   
            }
        }
    }
    catch (WebException) { }

    return Result;
}

if (!WebServiceAvailable() 
{
    //Redirect to Not Available page
}

I use this and it seems to work well.

webnoob
  • 15,747
  • 13
  • 83
  • 165
  • It has to be real-time results, that's the problem. – loyalflow Jan 21 '13 at 16:15
  • This isn't such a good idea. You will always be trying to make a request even if the service is down. To make your application more responsive during the time the service is down you need to limit the requests somehow. – blank Jan 21 '13 at 18:20
  • You won't know it's down unless you check it ... a simple http request isn't going to cause huge problems is it? It's not making any web service calls so no overhead of processing. – webnoob Jan 21 '13 at 22:40
  • If you look at the circuit breaker pattern you'll see a way of handling it - you basically limit the requests until one makes it through. Also, if you keep on hammering the 3rd party api with requests (even with a timeout) it might actually make the problem worse by not allowing it to recover. – blank Jan 22 '13 at 07:53
2

Use the Circuit Breaker pattern. See here for some advice on implementation in C#.

And read Michael Nygard’s book and this Netflix blog post for great advice and here's a StackOverflow question on the circuit breaker.

Community
  • 1
  • 1
blank
  • 17,852
  • 20
  • 105
  • 159
1

Use HttpWebRequest.Timeout (on MSDN) to wait for a reasonable amount of time, catch the exception to know when to deliver the user something like 'Try again later!', etc. The default timeout is just too long (100s).

If your business allows it, you can even offer old/cached data to the user in the case of a failure.

istepaniuk
  • 4,016
  • 2
  • 32
  • 60
0

Could you call your server side code (which I presume calls the 3rd party API) from an AJAX call within your web page (maybe use jquery for ease). That way if you get an error or timeout from AJAX response you can update your page accordingly without breaking your website.

You could setup a regular AJAX polling to your endpoint (which in turn polls the API for a heartbeat)

iandayman
  • 4,357
  • 31
  • 38