Pattern for checking online service availability

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("https://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!

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.

2

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.

2

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Pattern for checking online service availability

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("https://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!

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.

2

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.

2

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Pattern for checking online service availability

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("https://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!

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.

2

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.

2

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.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật