Web services performance tuning

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

This tutorial focuses on design guidelines and techniques to improve the performance of your Web services. Enjoy it!  

web service performance tip #1
Use coarse grained web services

One of the biggest mistakes that many developers make when approaching web services is defining operations and messages  that are too fine-grained. By doing so, developers usually define more than they really need. You need to ensure that your web service is coarse-grained and that the messages being defined are business oriented and not programmer's oriented.

The reason why top-down webservices are the pure web services is really this: you define at first the business and then the programming interface.
Don't define a web service operation for every Java method you want to expose. Rather, you should define an operation for each action you need to expose. 


public void checkInventory(long itemId) {
 InventoryDAO.checkInventory(itemId);
}
public void checkAccount(long itemId) {
 CustomerDAO.checkAccount(clientId);
}
public void saveOrder(long itemId, long clientId) {
 OrderDAO.persisteOrder(itemId,clientId);
}

and this is a coarse grained web service:

public void makeOrder(long itemId, long clientId) {
 InventoryDAO.checkInventory(itemId);
 CustomerDAO.checkAccount(clientId);
 OrderDAO.persisteOrder(itemId,clientId);
}

for the sake of simplicity we don't have added any transaction commit/rollback however what we want to stress is that the first kind of web service is a bad design practice at first and then also will yield poor performance because of 3 round trips to the server carrying SOAP packets. 

web service performance tip #2
Use primitive types, String or simple POJO as parameters and return type

Web services have been defined from the grounds up to be interoperable components. That is you can return both primitive types and Objects. How do a C# client is able to retrieve a Java type returned by a Web Service or viceversa ? that's possible because the Objects moving from/to the Webservices are flattened into XML.

As you can imagine the size of the Object which is the parameter or the returntype has a huge impact on the performance. Be careful with Objects which contain large Collections as fields- they are usually responsible of web services bad performance. If you have collections or fields which are not useful in your response mark them as transient fields.

webservice performance tip #3

Evaluate carefully how much data your need to return

Consider returning only subsets of data if the Database queries are fast but the graph of Object is fairly complex. Example:

@PersistenceContext(unitName = "persistenceUnit")
private EntityManager em;
 
@WebMethod
public List  readTaskListByPage(@WebParam(name = "start") 
 int start,
 @WebParam(name = "stop")  
 int stop) {

 Query dbQuery = em.createQuery(query);    

 List   listObjects = dbQuery.setFirstResult(start)
 .setMaxResults(stop-start).getResultList();

 return listObjects;        

}

On the other hand if your bottleneck is the Database Connectivity reduce the trips to the Service and try to return as much data as possible.

web service performance tip #4

Examine the SOAP packet

This tip is the logic continuation of the previous one: if you are dealing with complex Object types always check what is actually moving around the network. There are many ways to sniff a SOAP packet, from a tcp-proxy software which analyze the packets between two destinations. For example Apache delivers a simple tcp-monitoring utility which can be used for this purpose :

http://ws.apache.org/commons/tcpmon/

Here's how to debug SOAP messages with jboss:
http://www.mastertheboss.com/en/jboss-howto/49-jboss-http/178-how-to-monitor-webservices-soap-messages-.html

web service performance tip #5

Cache on the client when possible

If your client application requests the same information repeatedly from the server, you'll eventually realize the server's performance, and consequently your application's response time, is not fast enough.
Depending on the frequency of the messaging and the type of data, it could be necessary to cache data on the client.

This approach however needs to address one important issue: how do we know that the data we are requesting is consistent ? the technical solution to this is inverting the order of elements: when the Data in our RDBMS has become inconsistent a Notification needs to be issued so that the Client cache is refreshed. In this article you'll find an elegant approach to this topic.

http://www.javaworld.com/javaworld/jw-03-2002/jw-0308-soap.html?page=2

web service performance tip #6

Evaluate asynchronous messaging model

In some situations, responses to Web service requests are not provided immediately, but rather sometime after the initial request transactions complete. This might be due to the fact that the transport is slow and/or unreliable, or the processing is complex and/or long-running. In this situation you should consider an asynchronous messaging model.

The new WebService framework, JAX-WS, supports two models for asynchronous request-response messaging: polling and callback. Polling enables client code to repeatedly check a response object to determine whether a response has been received. Alternatively, the callback approach defines a handler that processes the response asynchronously when it is available.

Consider using asynchronous Web methods is particularly useful if you perform I/O-bound operations such as:

  1. Accessing streams
  2. File I/O operations
  3. Calling another Web service

In the this article I showed how to achieve asynchronous web services using JAX-WS 2.0
http://www.mastertheboss.com/en/web-interfaces/111-asynchronous-web-services-with-jboss-ws.html

web service performance tip #6

Use JAX-WS web services

The Webservices based on JAX-WS perform much better then JAX-RPC earlier Web services.
This is due to the fact that JAX-WS use JAXB for data type marshalling/unmarshalling which has a significant performance advantage over DOM based parsers.

For a detailed comparison benchmark check here:

http://java.sun.com/developer/technicalArticles/WebServices/high_performance/

webservice performance tip #7

Do you need RESTful web services ?

RESTful web services are stateless client-server architecture in which the web services are viewed as resources and can be identified by their URLs.
REST as a protocol does not define any form of message envelope, while SOAP does have this standard. So at first you don't have the overhead of headers and additional layers of SOAP elements on the XML payload. That's not a big thing, even if for limited-profile devices such as PDAs and mobile phones it can make a difference.

However the real performance difference does not rely on wire speed but with cachability. REST suggests using the web's semantics instead of trying to tunnel over it via XML, so RESTful web services are generally designed to correctly use cache headers, so they work well with the web's standard infrastructure like caching proxies and even local browser caches.

RESTFUL web services are not a silver bullet, they are adviced in these kind of scenarios:

REST web services are a good choices when your web services are completely stateless.Since there is no formal way to describe the web services interface, it's required that the service producer and service consumer have a mutual understanding of the context and content being passed along.

On the other hand a SOAP-based design may be appropriate when a formal contract must be established to describe the interface that the web service offers. The Web Services Description Language (WSDL) describes the details such as messages, operations, bindings, and location of the web service. Another scenario where it's mandatory to use SOAP based web services is an architecture that needs to handle asynchronous processing and invocation.

web service performance tip #8

Eager initialization

JBossWS may perform differently during the first method invocation of each service and the following ones when huge wsdl contracts (with hundreds of imported xml schemas) are referenced. This is due to a bunch of operations performed internally during the first invocation whose resulting data is then cached and reused during the following ones. While this is usually not a problem, you might be interested in having almost the same performance on each invocation. This can be achieved setting the org.jboss.ws.eagerInitializeJAXBContextCache system property to true, both on server side (in the JBoss start script) and on client side (a convenient constant is available in org.jboss.ws.Constants). The JAXBContext creation is usually responsible for most of the time required by the stack during the first invocation; this feature makes JBossWS try to eagerly create and cache the JAXB contexts before the first invocation is handled.

web service performance tip #9

Use Literal Message Encoding for Parameter Formatting

The encoded formatting of the parameters in messages creates larger messages than literal message encoding (literal message encoding is the default). In general, you should use literal format unless you are forced to switch to SOAP encoding for interoperability with a Web services platform that does not support the literal format. Here's an example of an RPC/encoded SOAP message for helloWorld

<soap:envelope>
  <soap:body>
     <helloWorld>
       <a xsi:type="xsd:string">George</a>
       <b xsi:type="xsd:string">Michael</b>
     </helloWorld>
   </soap:body>
</soap:envelope> 

Here the operation name appears in the message, so the receiver has an easy time dispatching this message to the implementation of the operation.The type encoding info (such as xsi:type="xsd:int") is usually just overhead which degrades throughput performance. And this is the equivalent Document/literal wrapped SOAP message :

<soap:envelope>

  <soap:body>

    <helloWorld> <a>George</a> <b>Michael</b></helloWorld>

  </soap:body>

</soap:envelope>

Related articles available on mastertheboss.com

JBoss web services

JAX-WS simplifies the development model for a web service endpoin

What is a Web Service One Way invocation?

JBoss recipe of the day

Asynchronous web services with JBoss WS

Developing rigorous and responsive web service client application

How to change the default Web Service deployment Port ?

JBoss recipe of the day

Using Axis Web Services with JBoss

Still not ready for JBoss WS ? if you don't have a JDK 1.5 compli

Invoking JBoss Web Services with Flex

Flex® is a free, open source framework for building highly in