Thursday 31 January 2013

Application Performance

Its impressive when customer realizes thousands of users are browsing still application is behaving fast. Performance is important and can be achieved if we focus on below topics. Having said that there could be multiple other factors depending on application architecture.

· Adhere best code practices
· Improve performance of database operation
· J2EE application server performance tuning

 Adhere best code practices:

1. Use proven frameworks like Struts, Spring, Hibernate, JSF etc

2. Apply appropriate proven design patterns to improve performance and minimize network communication cost (Use session facade pattern, value object pattern).

3. Handle and propagate exception properly. Managing and logging exception details help to analyze production issues same time avoids System.out rather use Log4J for logging.

4. Implement caching methodology whenever applicable. This is an excellent technique to avoid multiple expensive database call. Let me explain this with a nice example.
 Application uses thousand organizations which stored in database. To display organization data (like org name, address, phone) or generate report require multiple database interaction for fetching organization details. Even situation will be worse when hundreds of users accessing same organization but actually application is hitting database several times. In this scenario caching is very cost effective.


5. Never use hard coded value in code. Principle should be writing once and reuse everywhere and use java object orientation technique. Class, method and variable name should be self-explanatory. Avoid creating unnecessary too many new objects. Code should be properly indented to make it more readable. One method should not be more than 100 lines.

 6. Session persistence is costly and never use until and unless it’s very much required. Try to store object in session as minimum as possible. Always remove the object you are storing in session when you do not need them anymore otherwise it will create unnecessary load on the server.

 7. If you are using EJB then follow EJB best practices. In addition an EJB call is expensive and always comes at price. Check to see if you can consolidate several EJB methods into a single coarse-grained EJB method.


Improve performance of database operation:


1. Database connection should be release when not needed anymore. Otherwise there will be potential resource leakage problems. Always put resource clean up code (i.e. database connection, statement, etc.) in a finally{} block.

2. Use JDBC prepared statement for repeated read and batching for repetitive inserts and updates.

3. Always try to use parameterized hql query whenever possible rather than appending
parameters to hql. Persistence provider will be able to cache / better use the query resulting better query performance. 


4. Try to avoid using functions (like UPPER, distinct, max etc.) in the query. Functions can only be used when it is most needed. Consult with our DBA for any kind of assistance.

5. For tables having more than 20 columns, do not use "select *" in the query.
Select only those fields you need.



 
J2EE application server performance tuning:


1. Set the Web container threads, which will be used to process incoming HTTP requests. The minimum size should be tuned to handle handle average load of the container and maximum should be tuned to handle peak load. The maximum size should be less than or equal to the number of threads in your Web server.

2. Application servers maintain a pool of JDBC resources so that new connection does not need to be created for each transaction. Application server can also cache your prepared statements to improve performance. So you can tune minimum and maximum size of these pools.

3. Tune your initial heap size for the JVM so that garbage collector runs at a suitable interval so that it does not cause any unnecessary overhead.

4. Set the session manager settings appropriately based on following guidelines.
  • Set the appropriate value for in memory session count.
  • Try to store object in session as minimum as possible.
  • Don't enable session persistance unless required by your application.
  • Invalidate your session when you are finished with them by setting appropriate session timeout.
5. Turn the application server tracing off unless required for debugging.

6. Some application servers support lazy loading and dirty marker strategies with EJB to improve performance.


[ to be continued .................]