{"id":365,"date":"2010-08-23T13:45:56","date_gmt":"2010-08-23T11:45:56","guid":{"rendered":"http:\/\/touk.pl\/blog\/?p=365"},"modified":"2022-07-27T15:58:27","modified_gmt":"2022-07-27T13:58:27","slug":"glimpse-on-tomcat-performance-tuning","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2010\/08\/23\/glimpse-on-tomcat-performance-tuning\/","title":{"rendered":"<!--:en-->Glimpse on Tomcat performance tuning.<!--:-->"},"content":{"rendered":"<p><!--:en--><figure style=\"width: 161px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/9\/98\/LA2-Datasaab-CK37-frame.jpg\/800px-LA2-Datasaab-CK37-frame.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/9\/98\/LA2-Datasaab-CK37-frame.jpg\/800px-LA2-Datasaab-CK37-frame.jpg\" alt=\"\" width=\"161\" height=\"119\" \/><\/a><figcaption class=\"wp-caption-text\">Production environment ;-)<\/figcaption><\/figure>\n<p style=\"text-align: justify\">\n  <!-- p, li { white-space: pre-wrap; } --><\/p>\n<p>  <!--StartFragment-->Have You ever wondered about Tomcat configuration in production environment, or just let \u201cthis things\u201d to the admins, or even worse, don\u2019t care at all about it? If the answer is \u201cTomcat configuration ? I\/We\/Our client just installs tomcat and deploy our application. Why border about any additional configuration ?\u201d You should read this post.\n<\/p>\n<p style=\"text-align: justify\">\n  <!--:--><\/p>\n<p>  <!--more--><\/p>\n<p>  <!--:en-->I will not write about all Tomcat\u2019s configuration. It\u2019s pointless. I just want to show some problems with performance with default Tomcat\u2019s configuration in production enviroment. Especially if You are using Tomcat in as web server in internet, with many simultaneous clients and connections. In such cases performance and high responsivity is important.\n<\/p>\n<p style=\"text-align: justify\">\n  <strong>1<\/strong>. Let\u2019s start from logs. Standard Tomcat\u2019s logs are configured to appear in two places: file and console. In production it\u2019s pointless to have duplicate logs so first thing to gain some speed boost is to replace following line from logging.properties:\n<\/p>\n<p><code>.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler<\/code> with this one: <code>.handlers = 1catalina.org.apache.juli.FileHandler<\/code><\/p>\n<p style=\"text-align: justify\">\n  <strong>2.<\/strong> Second thing to do with logs is to set max file size and protection from overflow. It\u2019s also very easy. Just add new handler like following one: <code><\/p>\n<p>catalina.java.util.logging.FileHandler<\/code>\n<\/p>\n<p>and configure it like this (max 4 filesx10Mb):<\/p>\n<p><code>1catalina.java.util.logging.FileHandler.pattern = ${catalina.base}\/logs\/catalina.%g.log<\/code> 1catalina.java.util.logging.FileHandler.limit = 10000000 1catalina.java.util.logging.FileHandler.count = 4<\/p>\n<p style=\"text-align: justify\">\n  <strong>3.<\/strong> Last thing You have TO HAVE in production environment are asynchronous logs. Synchronous logging is far more time consuming then asynchronous one. Especially when You have numerous clients. Check if Your Tomcat is configured in proper way (I won\u2019t write about this. Just search in web about log4j configuration. It\u2019s lot of this there.)\n<\/p>\n<p style=\"text-align: justify\">\n  <strong>4.<\/strong> That\u2019s all about logging. Now something much more influent on connection speed-connectors. They are configured in server.xml under node.\n<\/p>\n<p style=\"text-align: justify\">\n  Tomcat have 3 main connectors:\n<\/p>\n<p style=\"text-align: justify\">\n  \u2013 <strong>BIO \u2013<\/strong> Blocking Java connector which is default one\n<\/p>\n<p style=\"text-align: justify\">\n  \u2013 <strong>APR<\/strong> \u2013 Uses native C code fo IO (very fast)\n<\/p>\n<p style=\"text-align: justify\">\n  \u2013 <strong>NIO<\/strong> \u2013 Non blocking connectror in Java (also faster than default)\n<\/p>\n<p style=\"text-align: justify\">\n  The first <strong>BIO<\/strong> connector (\u201corg.apache.coyote.http11.Http11Protocol\u201d) is set as default one. Why ? Becouse in many cases such configuration it\u2019s enough. Tomcat usually is used in intranets where it\u2019s not required to handle high traffic volume. Moreover BIO connector is very stable.\n<\/p>\n<p style=\"text-align: justify\">\n  But if our applications have to serve many http requests the blocking connector isn\u2019t the best choice. So here comes <strong>ARP<\/strong> and <strong>NIO<\/strong> connector.\n<\/p>\n<p style=\"text-align: justify\">\n  The first one (org.apache.coyote.http11.Http11AprProtocol) requires to compile native library (just search in google for <strong>ARP<\/strong>) and could be less stable than BIO connector. In exchange ARP connector is very fast, could handle requests simultanously in non blocking mode, have pooling of unlimited size and could handle unlimited threads (in theory, becouse threads are limited with CPU power)\n<\/p>\n<p style=\"text-align: justify\">\n  Last connector \u2013 <strong>NIO<\/strong> (org.apache.coyote.http11.Http11NioProtocol) is something between <strong>ARP<\/strong> and <strong>BIO<\/strong>. It\u2019s good choice if You don\u2019t want to compile native libraries. NIO connector is also non blocking, little slower in reading static content than ARP, but far more configurable (pool size, no of threads etc).\n<\/p>\n<p style=\"text-align: justify\">\n  <strong>5.<\/strong> Ok, so now We know, which connector should we choose, but every connector have to be set up in proper way. There are several parameters but the important ones are:\n<\/p>\n<p style=\"text-align: justify\">\n  \u2013 maxThreads \u2013 typical from 150-800 (For BIO this is max nr of open connections)\n<\/p>\n<p style=\"text-align: justify\">\n  \u2013 maxKeepAliveRequests \u2013 typical 1 or 100-250. For BIO this should be set to 1 to disable keep alive (only if we have high concurency and not using SSL). <strong>BIO<\/strong> connector automatically disables keep alive for high connection traffic\n<\/p>\n<p style=\"text-align: justify\">\n  \u2013 connectionTimeout \u2013 typical 2000-60000 WARNING: default Tomcat has it set to 20 000! It\u2019s to high for production environment. Good choice is to decrese it to 3000-5000 unless Your production env is working with slow clients. This parameters describes max time between TCP packets during blocking read\/write\n<\/p>\n<p style=\"text-align: justify\">\n  <strong>6.<\/strong> This is \u201calmost\u201d the end of tunning Tomcat for production. The last thing is to configure cache. Default cache is configured to 10 MB. You can set this a little more if You have a lot of static content. Also cache revalidation (standard 5 sec) should be tuned. How ? It\u2019s difficult to say. The best way is to tune this parameters by own during tests.\n<\/p>\n<p style=\"text-align: justify\">\n  That\u2019s all. I hope I realized to everyone why not rely on standard Tomcat configuration.\n<\/p>\n<p><!--:--><\/p>\n","protected":false},"excerpt":{"rendered":"Have You ever wondered about Tomcat configuration in production environment, or just let \u201cthis things\u201d to the admins,&hellip;\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,237],"tags":[68,107],"class_list":["post-365","post","type-post","status-publish","format-standard","category-development-design","category-processes-practices","tag-java","tag-xml"],"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":16,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":14623,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions\/14623"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}