Sunday 7 April 2013

Optimizing tomcat 7 for best performance

I just had to optimize a Tomcat 7 server to handle a big load, so like any other developer i started to search google for result, and it was a big disaster but i had to do this task and so i started, and after a lot of work i (thanks to Allah) succeeded to gain some knowledge about this topic, so lets start

First of all we are speaking about optimizing tomcat 7, and it has 2 major issues to work with
  1. Optimizing Tomcat server.
  2. Optimizing your application.
So lets start with number 2, whatever how optimized was the server without an optimized code you will get a bad performance, so my advice before optimizing your server start with optimizing your application, not saying that you do not need to optimize your server but it's much more important to write an optimized code to work in an optimized server.

So let's start tuning our server 

How To optimize your Tomcat 7
  1. [Optimizing tomcat memory consumption] Set tomcat mode to be in a server mode, by default tomcat is in client mode, to set the server mode just add -server to your CATALINA_OPTS to be like
    CATALINA_OPTS="-server -Xms2G -Xmx2G $CATALINA_OPTS -XX:+UseConcMarkSweepGC -XX:NewSize=1G -XX:+UseParNewGC"
    also you need to set Xms and Xmx and XX:MaxPermSize to a suitable values depends on your machine, you can edit this line on catalina.(sh|bat).
    source : http://tomcat.apache.org/articles/performance.pdf
  2. [Optimizing JSP behaviour ]Set tomcat parameters for production on conf/web.xml,  on the servlet name jsp you can add <init-param>with each value of the following:
    1. development - To disable on access checks for JSP pages compilation set this to false. 
    2. genStringAsCharArray - To generate slightly more efficient char arrays, set this to true.
    3. modificationTestInterval - If development has to be set to true for any reason (such as dynamic generation of JSPs), setting this to a high value will improve performance a lot.
    4. trimSpaces - To remove useless bytes from the response, set this to true.Source :http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html#Production_Configuration
  3. [Optimizing server behaviour] Knowing that tomcat server is a server built over java so it has some    of java problems regardless performance when comparing it with apache server (which is by default a native server), don't worry Tomcat is an Apache product and people in Apache also concerning with performance so they give us the power of APR, which allow tomcat to work with native library to achieve better performance, so this is the steps to make tomcat works with APR on linux (my server was a centos os ... for any other linux distribution you may edit some of the below commands to achieve the same result)
    1. Make sure that these packages are installed on the server machine gcc, openssl-devel, openssl, apr, apr-devel ..... if not search for them on yum and install them, and Sun JDK to be downloaded and installed 
    2. Go to CATALINA_HOME/bin and run this command "tar -zxf tomcat-native.tar.gz" you will find a new dir created with name tomcat-native-X.X.XX-src, so "cd tomcat-native-X.X.XX-src/jni/native"
    3. run the config command "./configure  --with-apr=/usr/bin/apr-1-config  --with-java-home=path-to-tomcat  --with-ssl=yes   --prefix=where-to-put-the-lib (i used /usr/java/jdk1.6.0_38/jre/lib/i386/)".
    4.  Run this command "make && make install".
    5.  After finishing the make install command change the connector protocol to "org.apache.coyote.http11.Http11AprProtocol".
    6.  restart your server and check that the line of missing APR lib is not on the log.
      Source :http://www.jroller.com/agileanswers/entry/configuring_apr_for_tomcat
  4. [Tune the read write buffer] edit this properties in conf/server.xml for tuning the app threads and buffer size this value are configurable base on your application nature 
    1. <Connector port="8080" connectionTimeout="200000" redirectPort="8443" maxThreads=" up to 32000" socket.appReadBufSize="1024" protocol="org.apache.coyote.http11.Http11NioProtocol" socket.appWriteBufSize="1024" bufferSize="1024"/>
      Source : http://blog.krecan.net/2010/05/02/cool-tomcat-is-able-to-handle-more-than-13000-concurrent-connections/comment-page-1/#comment-9636


    No comments:

    Post a Comment