Implementing a sever fail over feature

4 comments
Suppose you have a client-server application and you want to automatically switch to a standby/backup server when the primary server is unavailable due to either failure or scheduled shut down. I present you how to achieve that by this post.

We can have the primary and secondary urls in a configuration file and load them to a list at the beginning of the system. For the demonstration purpose I have hard coded list of urls.

We can achieve it easily by checking the response code sent by the server. I believe the code it self-explanatory.

public class Envision {

    public static void main(String[] args) throws MalformedURLException {
        HttpURLConnection httpURLConnection = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        String result = "";
        String inStr = null;
        List<URL> urls = new ArrayList<URL>();
        urls.add(new URL("http://example_primary.com"));
        urls.add(new URL("http://example_secondary.com"));


        try {
            for (int i = 0; i < urls.size(); i++) {

                try {
                    httpURLConnection = (HttpURLConnection) urls.get(i).openConnection();
                    inputStream = httpURLConnection.getInputStream();
                    bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                    int responseCode = httpURLConnection.getResponseCode();

                    if (responseCode == 200) {
                        while ((inStr = bufferedReader.readLine()) != null) {
                            result = result + inStr;
                        }
                        break;
                    }
                } catch (Exception e) {
                    System.out.println("Error: While requesting data from server "+urls.get(i)+" " + e.getMessage() );

                }
            }

            System.out.println(">>>>Res: " + result);

        } catch (Exception e) {
            System.out.println("Error: While requesting data from server" + e.getMessage());

        }
    }
}

Happy Coding!

4 comments :

  1. It probably makes more sense to use a load-balancer with failover capabilities on the server side than to implement a client redirect that always tries the primary first. First of all, the secondary's resources remain idle and are wasted unless the primary goes down. Second, the load balancer allows you to add several servers for backup/load balancing without any additional configuration on the client side.

    ReplyDelete
    Replies
    1. Agreed, yes load balancing would have more advantages! But the main concern of this post was to how to serve client requests when there is a server failure.

      Delete
  2. I thought this is real server load balance implementation. But this is redundancy coded in java application. change the topic. java is nothing do with load balancing

    ReplyDelete
    Replies
    1. I agree with what Sarith has said. But what you have said is unclear to me. Honestly I don't see any reason to change the topic of this post. :)

      Delete