Web Services Design – Pattern for Checking Online Service Availability

client-serverdesignpollingweb services

I'm not sure that this is entirely on-topic, but I'll try to make it so.

I have an online service (API) and an Android application that uses it for all actions in the app. At the current moment, nothing is stored locally, so I have to depend on the API being online, and would like to warn the user when it's not (for whatever reason).

I'm wondering what the preferred method of checking availability is. As this is the first time I've ever done this, I've tried to do my research and have come up with three potential solutions:

1. Check availability on action

When the user takes an action, check the API's status. Currently I'm doing this (Java):

public class ServiceAvailability implements Runnable {

    private volatile boolean available;

    @Override
    public void run() {

        available = false;

        try {
            HttpURLConnection connection =
                    (HttpURLConnection) new URL("http://myservice.com").openConnection();
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(1000);
            connection.setRequestMethod("HEAD");

            int responseCode = connection.getResponseCode();

            if (200 <= responseCode && responseCode <= 399) {
                available = true;
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    public boolean isAvailable() {

        return available;
    }

It works just fine. Just send a HTTP HEAD request to the site and see if the response is a good one. Every time the user takes an action (e.g., runs a search, clicks someone's profile, etc.) it calls the isAvailable() method, and displays an error message in the event that the available = false.

The advantage of this is that if the service goes down, the user will immediately know, and will be warned that they will not be able to use the app until the service is brought online again.

I can't think of any disadvantages of this at the moment…

2. Check availability on interval

The other solution (that I haven't actually implemented) is run a thread in the background and HTTP HEAD the service every X seconds/minutes and update the status accordingly; at that point, I could call isAvailable() which would simply return the boolean set by the result of the HEAD request.

The advantage to this is that a visual can be shown (for the duration) when the service is not online, letting the user know ahead of time that their request will not be processed.

The disadvantage that I see is that while HEAD requests are very minimal in the amount of bandwidth they use, 3G users with very little data or users with slow connections might have trouble sending the HEAD and receiving the response in a timely manner.

3. Combine the two

The last approach that I see is a combination, where the thread in the background checks availability every X seconds, and action methods using the API also check availability through an additional HEAD request.

The advantage that I see here is that the user will know (visually) when the service is offline for the duration, as well as have a fallback if the status of the API happens to change during the X seconds duration that the API was last checked.

From what I can see, the Facebook Messenger app (iOS at least) implements this. When you open the app, it 'attempts to connect' (visual up top) and if your connection drops, it will let you know with a bright red visual. Trying to take action when you're not connected (to their service) also gives you an error.

Question

Is there a single one of these 3 methods that are preferable, or a different method that I haven't mentioned? Furthermore, is there an actual design pattern for this (e.g., Singleton on a thread), one more preferable than what I've implemented?

Edit: As a user of the app myself, I feel that a pretty visual telling me that I won't be able to do something before I actually try is better than trying and failing. Furthermore, I want to implement this in order to determine more than just an 'up' or 'down' status of the API. For example, when the API is in maintenance for an update, I want to be able to receive a 503 and let the user know visually that the service is in maintenance mode, will be online shortly, and not to panic.

I'm not exactly sure how many users will be using the app in the long run, so I'm trying to prepare ahead of time in the event of API outages and maintenance. I don't have a large-scale infrastructure that can support millions of users like Facebook, so I believe that this availability check is reasonable in order to provide good (decent?) user experience, as I know for a fact that the API will not have 100% up-time. Not to mention that I'm the sole developer and the service might go down while I'm sleeping!

Best Answer

I would Check Availability on Failed Action. Just issue your calls normally and if you get a failure, then call the Service Availability function to verify.

Assume service is up, if one gets and error, then issue the additional call to verify availability.

Related Topic