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;

    public void run() {

        available = false;

        try {
            HttpURLConnection connection =
                    (HttpURLConnection) new URL("http://myservice.com").openConnection();

            int responseCode = connection.getResponseCode();

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

    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.


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!

  • Depending on your app's purpose and your user's appetite for availability, it is typically the case your users will want your app to remain available despite being disconnected (say, poor wireless connection). If this is the case, you will have to revise the "no local storage" design point. Once you've done that, the app will queue up every user command into a local database. At that point the app-server interaction will be similar to CodeART's answer.
    – rwong
    Commented Jan 26, 2015 at 21:07
  • If there are fundamental reasons that forbid local storage, please mention that in your question. For example, some possible reasons are: secrecy, minimization of local storage footprint, keeping the app software architecture simple, etc.
    – rwong
    Commented Jan 26, 2015 at 21:08
  • @rwong You bring up a good point about local storage. The purpose of the app has to remain opaque for the moment (about to release a beta); however, the local storage will strictly be used for paid versions of the app, wherein network actions such as favoriting will be stored locally as well as in the database online. The majority of the actions in the app will be performed through the API and not local storage. Commented Jan 27, 2015 at 1:18

3 Answers 3


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.

  • This was my original thought, but please see my edit to my question for a bit of added info :) Commented Jan 27, 2015 at 1:23
  • Scratch my previous comment - I realize now that this is what I should be doing, and I can serve different response codes for different events based on the status of the service during the service availability check. Commented Jan 27, 2015 at 21:41

Just because a http-ping at time t gets a response, you've still got a large uncertainty that a request at time t' will complete.

I suggest you not ping the server if it can be avoided because it is a poor proxy measure of actual availability in the future. If a request is made and no response is received, retry with an exponential back-off interval.

  • This is very true, and I suppose that I didn't really think about this... I agree that it's a poor attempt to ensure availability before sending a request, but there are more reasons that I want to do this than simply checking if the service is online before attempting a network action. Please refer to the edit in my question. +1 for exponential back-off suggestion, I may revert to this soon. Commented Jan 27, 2015 at 1:32
  • If you keep the server idle-polling, the reverse of exponential back-off applies. For example the Network Time Protocol considers "it was up last time I queried it" as a predictor of what its state will be in the future and increases the polling interval to accommodate.
    – msw
    Commented Jan 27, 2015 at 2:07

It happens to be that service is your resource and for some reason it may not be available. What would you do if your client application talks directly to the database. Would you check availability of the database before trying to query it? I would not.

I think service not being available is an exceptional circumstance. I would expect my service to be available at all times, and if it's not, then something exceptionally bad happened and I will wait for client call to fail and exception to be thrown.

How will I handle exception is a different story. I might re-try calling the service, I might look at the type of exception to see if it's a timeout and service URI simply cannot be resolved.

What you have to think about is what value do you add to your program by checking for service's availability. Ok, you tell user that service is not available, but user probably doesn't care and if anything they probably get more frustrated as they might not know what service is. Don't bog them down with details, tell them that something went wrong and army of monkeys are working on it.

  • While I agree that the user might not care, I am not of that opinion. For example, I really enjoy the fact that Facebook Messenger has a red banner that says 'not connected' when I'm obviously not connected. It lets me know that my messages won't send until I'm connected, which in my opinion is good user experience (warning instead of try --> fail). That said, please see the edit to my question, as I added additional information for clarification. +1 for your suggestions though! Commented Jan 27, 2015 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.