Java.next -- Four languages that represent the future of Java
Blogger Stuart Halloway has begun a series of posts on trends that point to the future of the Java platform. In his first post, he compares Clojure, Groovy, JRuby, and Scala -- four wildly different languages that nonetheless all play together in the JRE. Find out what unites these languages and what they can tell us about the future of Java-based development ...

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Improve availability in Java enterprise applications

Best practices for designing highly available Java enterprise applications

It is always the middle of the day in some part of the world. For an online business, operating hours are 24 hours, seven days a week. This means maintaining uninterrupted uptime is of primary importance to commercial Websites. Any application outage means customers lack access to information or services offered by the business. This results in potential loss of revenue and dissatisfied customers.

The most obvious step for improving the availability of applications running high-traffic Websites is to host them in environments designed for high availability. Availability in the infrastructure is typically achieved by building redundancies at all levels. But to fully leverage the availability built in an environment, application designers must design their applications with high availability in mind. It is important to be conscious of the fact that certain application design choices that would work well in a simple environment may not be as effective in a highly available environment. For applications that do not have the luxury of being deployed in a highly available environment, some design considerations can improve their availability.

This article sets the stage by briefly discussing the characteristics of a highly available (HA) environment. Then, it discusses special design considerations for applications deployed in HA environments. And finally, it introduces application-level best practices for improving availability even if an application is not being deployed in an HA environment.

Highly available environments

Below are two illustrations of hosting environments that represent the two ends of the HA infrastructures spectrum. Figure 1 shows a complete Java EE (Java Platform, Enterprise Edition) delivery environment hosted entirely on one server.

Figure 1. Single-server hosting environment

In this type of environment, the HTTP, Java EE, and RDBM (relational database management) servers, all coexist on the same physical machine. This is the most cost-effective Java EE environment possible, but also the least fault-tolerant because every infrastructure component is a potential single point of failure.

On the other end of the spectrum is an infrastructure as depicted in Figure 2.

Figure 2. High availability hosting environment. Click on thumbnail to view full-sized image.

This deployment environment consists of multiple data centers, typically in different geographic locations, and each identical in hardware and software levels. Almost everything about them is redundant down to their connections to different Internet backbones. In this environment, each infrastructure component is housed on a separate piece of fault-tolerant hardware. These geographically disparate data delivery environments rely on both data and cluster synchronization so each data center can field requests simultaneously.

The two most important characteristics of HA environments are:

  1. No single point of failure: Build redundancy in the environment at all tiers to remove single points of failure. This tenet should be applied to both hardware and the infrastructure's software components.
  2. Failover: In the case of a failure, use load balancing to dynamically restore service to application servers.


Design applications to leverage high availability in the infrastructure

Having an HA infrastructure does not by itself guarantee high availability for an application. If an application is not designed to leverage high availability in the infrastructure, it will not achieve its availability goals. Applications designed for these types of environments need special design and implementation considerations.

For instance, how do applications share session information across data delivery centers or how does writing to replicated databases affect your end-user's overall experience. The following design and implementation best practices help leverage HA infrastructure and increase application availability:

  • Use read-only JVM caches: In a typical HA configuration, several instances of an application run simultaneously on different application servers, each with its own Java virtual machine. Since each server has its own JVM, if you update data in a local cache, the effects of the update will only be seen by that JVM. Other instances of the application will not be aware of this update, which is why you should treat JVM-level caches as read-only. The exception is if there is a distributed cache that keeps the data synchronized in real time across the JVMs. Depending on your application server, you may have a custom solution for this problem or implement an RMI (remote method invocation) solution on your own.
  • Considerations for distributed HTTP sessions: To overcome the stateless nature of the HTTP protocol, Web applications rely on the HttpSession API to store session information. In a clustered environment, where several instances of the application run on different application servers, the following should be considered while using the HttpSession API:

    • Replicate session data in real time: Since you will not know which instance of your application will field the next request in a session, session data should be made available to all servers participating in a cluster in real time. This can be achieved by storing the session data in a database and then enabling replication at the database level. Some application servers, e.g., WebSphere, WebLogic, provide a faster—in memory—session data replication feature. If that feature is available in your application server, you should consider using it.
    • Serialize your session objects: Objects stored in distributed sessions need to implement the java.io.Serializable interface. Implementing this interface ensures the data can be transported over the "wire" to each server instance in the cluster.

      A good practice is to enforce the use of a custom method like the addObjectToSession(String key, Serializable value) method, instead of the default HttpSession.setAttribute (String key, Object value) method when adding session data. The distinction is, if you were to call the addObjectToSession() method with a non-serializable object, you would see a compile-time error. If you were to try and replicate a session object that had non-seriablizable objects placed into session with the put() method, you would see a runtime error and potentially, a broken user experience.

      Discuss

      Start a new discussion or jump into one of the threads below:

      Subject Replies Last post
      . Improve availability in Java enterprise applicati
      By JavaWorld
      10 04/20/08 07:06 PM
      by Anonymous


      Resources