SlideShare une entreprise Scribd logo
1  sur  51
Télécharger pour lire hors ligne
Tomcat Expert Series

                                                  Performance Tuning
                                                                                    Filip Hanik
                                                                                   SpringSource
                                                                                        2009




Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
Agenda


 •   Performance Tuning Process
 •   Logging improvements
 •   TCP and HTTP
 •   Tuning your connectors
 •   Content delivery and caching
 •   Tuning the JVM




                                    2
The process


 • Understand the system architecture

 • Stabilise the system

 • Set Performance Targets
    – Web applications are easy
    – Only one consideration –
      request/response time



                                        3
The process


 • Measure current performance

 • Identify the current bottleneck
    – Focus on one item at a time

 • Fix the root cause
    – Easy to get side tracked




                                     4
The Process


 • When possible, tune pre-production
    – Hard to profile in production

 • Application tuning most important
    – 80% or more of request time is typically spent inside the
      application

 • Tomcat tuning is fairly limited
    – Divided between JVM tuning and Tomcat connectors
    – Requires lower level of understanding




                                                                  5
Apache Tomcat in Production


 • Out of the Box Tomcat
    – Tomcat is ready for production

 • JVM settings must be applied
    – Default memory settings usually too small for most web
      applications

 • Tuning is limited
    – So we can cover most of it




                                                               6
Logging


 • Tomcat logging is fairly good
    – Years of adjusting log levels pays off
    – Doesn't log what you don't need to see

 • A few gotcha's with the default configuration
    – Catch all logger creates duplicate logs
       • Standard out – often piped to catalina.out
       • Log file on the file system
    – Synchronized logging
    – No overflow protection




                                                      7
Logging


 • Tomcat's logger
   – Rotated based on date
   – Implements a per-class-loader logger
      • Simply drop logging.properties into your web application and
        logging is configured
   – Synchronous logging
   – No file limit




                                                                       8
Logging


 • Java Virtual Machine logger
    – Rotated based on size
    – One global configuration for entire JVM
    – Synchronous logging




                                                9
Logging


 Remove duplicate logging (logging.properties)


    .handlers = 1catalina.org.apache.juli.FileHandler,
                java.util.logging.ConsoleHandler

 Adjusted catch all logger

    .handlers = 1catalina.org.apache.juli.FileHandler




                                                         10
Logging


 • Overflow protection
    – Size based rotation using JVM logger

 handlers = 1catalina.java.util.logging.FileHandler,…

    – No more than 5x20mb files

 1catalina.java.util.logging.FileHandler.pattern =
    ${catalina.base}/logs/catalina.%g.log
 1catalina.java.util.logging.FileHandler.limit = 20000000
 1catalina.java.util.logging.FileHandler.count = 5



                                                            11
Apache Tomcat in Production


 • Tuning Tomcat connectors
   – server.xml
   – <Connector>


 • To properly tune one must
   – Understand the TCP protocol
   – Understand how the CPU works
   – Understand load balancers and their algorithms




                                                      12
TCP


 •   Layer 4 in the OSI stack
 •   Session based
 •   TCP stack implementation keeps state
 •   Flow control
 •   Delivery guarantee




                                            13
TCP: Session based


 • Setup and break down handshakes
 • Client response time
   – Handshakes add to HTTP transaction time
   – HTTP keep alive improves client response time
   – HTTP keep alive takes up server resources




                                                     14
TCP: Stateful protocol


 • Each connection represented by
   –   source address
   –   source port
   –   destination address
   –   destination port
 • This is all the information a layer 4 load
   balancer has for load balancing




                                                15
TCP: Flow control


 • Prevents buffer overflow and lost data
 • Server must adjust write speed to client’s
   ability to read
 • Servlet specification is blocking IO
   – Utilize a thread for the entire HTTP transaction
   – For static content, Tomcat offers SEND_FILE with
     APR and NIO connectors




                                                        16
TCP: No Keep-Alive


         User Agent                         Server

                      TCP Setup (syn)
                      TCP Setup (syn/ack)
                      TCP Setup (ack)

                       HTTP request                    servlet
     single
     HTTP              HTTP response                   thread
  transaction                                          in use

                      TCP Close (fin)
                                                Close can be
                  TCP Close (fin,ack)
                                               done in different
                      TCP Close(ack)                ways


                                                                 17
TCP: Keep-Alive


               User Agent                         Server

                            TCP Setup (syn)
                            TCP Setup (syn/ack)
                            TCP Setup (ack)

                             HTTP request                  servlet
   multiple
    HTTP                     HTTP response                 thread
transactions                                               in use


                            TCP Abort(ack)
                            TCP Abort (rst)



                                                                     18
TCP: Summary


 • How does TCP affect our system?
   – Traffic patterns
      • High concurrency/short requests
      • Low concurrency/long requests
      • Static content
      • Dynamic content
      • Combination
      • Variations
      • Average size of request
   – It’s these patterns that decide how to tune our
     system


                                                       19
HTTP


 • Layer 7 in the OSI stack
 • Stateless protocol




                              20
HTTPS


 • HTTP over SSL
 • Expensive handshake
   – Keep alive makes a big difference
 • Encryption hides HTTP from routing devices
 • For any appliances, such as LB, this means
   – Fallback to layer 4




                                                21
Load Balancing: TCP/HTTP


 • TCP
   – Based on destination address/port
   – Connection centric – 1:1
   – Can lead to uneven loads
 • HTTP
   – Based on HTTP headers
   – Can reuse server connections
   – Can drain sessions




                                         22
Load Balancing: Algorithms


 • Load balancing
   – Connection limits
   – Reusing connections
   – Traffic shaping


 • Load balancing algorithm drive Tomcat
   configuration choices




                                           23
Apache Tomcat: HTTP/S


 • Our tuning options
   –   Threads
   –   Keep alive requests
   –   TCP Backlog (acceptCount)
   –   connectionTimeout
   –   Socket buffers
 • Different connectors
   – BIO – Blocking Java connector, default
   – APR – Uses native C code for IO
   – NIO – Non blocking Java connectors

                                              24
Apache Tomcat: HTTP/S


 • Disclaimer
   – Tuning options are meant for working and high
     performing applications
   – Options will not fix bad application behavior
   – If application is not tuned
      • Situation can worsen




                                                     25
Which connector?


 • Use BIO if:
   – Stability is the highest priority
      • APR and NIO are more recent
   – Most content is dynamic
   – Keep alive is not a determining factor



 protocol=“org.apache.coyote.http11.Http11Protocol”




                                                      26
Which connector?


 • Use APR if:
    –   SSL is terminated at Tomcat
    –   Keep alive is important
    –   Lots of static content
    –   Using Comet feature
    –   Requires compilation of native library

  protocol=“org.apache.coyote.http11.Http11AprProtocol”




                                                          27
Which connector?


 • Use NIO if:
    –   Compiling APR is not an option
    –   Keep alive is important
    –   Using SSL
    –   Lots of static content
    –   Using Comet features

  protocol=“org.apache.coyote.http11.Http11NioProtocol”




                                                          28
Which connector?


 • If uncertain:
   –   Use BIO connector
   –   Most mature code, both in Tomcat and JVM
   –   Will not break down
   –   Auto tune feature to disable keep alive
        • When hitting 75% if maxThreads in connection count


 protocol=“org.apache.coyote.http11.Http11Protocol”




                                                               29
Which connector?


Comparison Chart Java BIO        Java NIO            APR
          Class Http11Protocol   Http11NioProtocol   Http11AprProtocol
        Version 3.x+             6.x+                5.5.x+
        Polling NO               YES                 YES
   Polling Size N/A              Unlimited           Configurable
  HTTP Req Read Blocking         Non blocking        Blocking
 HTTP Body Read Blocking         Sim Blocking        Blocking
     HTTP Write Blocking         Sim Blocking        Blocking
            SSL JSSE             JSSE                OpenSSL
  SSL Handshake Blocking         Non blocking        Blocking
Max Connections maxThreads       Unlimited           Configurable


                                                                    30
Which connector?


 • If uncertain:
   –   Use BIO connector
   –   Most mature code, both in Tomcat and JVM
   –   Will not break down
   –   Auto tune feature to disable keep alive
        • When hitting 75% if maxThreads in connection count


 protocol=“org.apache.coyote.http11.Http11Protocol”




                                                               31
Which connector?


 • If uncertain:
   –   Use BIO connector
   –   Most mature code, both in Tomcat and JVM
   –   Will not break down
   –   Auto tune feature to disable keep alive
        • When hitting 75% if maxThreads in connection count


 protocol=“org.apache.coyote.http11.Http11Protocol”




                                                               32
Tuning threads


 • maxThreads
   –   Typical range 200-800
   –   Maximum nr of concurrent requests
   –   For BIO, max nr of open/active connections
   –   Good starting value 400




                                                    33
Tuning threads


 • maxThreads=“400”
   – Decrease if you see heavy CPU usage
      • Application might be CPU bound instead of IO bound
      • Find out what is causing CPU usage
   – Increase if you don’t see much CPU usage
      • Applications could be synchronized -> no gain
      • Take into account other resources, such as database
        connections




                                                              34
Tuning keep alive


 • maxKeepAliveRequests
   – Typical values 1, 100-200
   – Represents the number of requests Tomcat will
     handle on a TCP connection
   – Set to 1 disables keep alive
   – connectionTimeout/keepAliveTimeout controls
     the timeout in between requests




                                                     35
Tuning keep alive


 • maxKeepAliveRequests
   – Set to 1 if
      • Very high concurrency
      • Not using SSL in Tomcat
      • Using layer 4 load balancer
      • Using BIO connector
   – Set to >1 if
      • Using SSL or low concurrency
      • Layer 7 load balancer with advanced features
      • Using APR or NIO connector
   – BIO connector automatically disables keep alive
     for high connection counts
                                                       36
Tuning TCP backlog


 • acceptCount
   – Typical ranges 50-300
   – Represents nr of additional connections the OS
     should accept on your behalf
   – Useful when Tomcat can’t accept connections
     fast enough




                                                      37
Tuning TCP backlog


 • acceptCount=“100”
   – Increase if
      • Very high concurrency (nr of connections)
      • Connections getting rejected during peak traffic
      • Keep alive should be off


   – Decrease if
      • Keep alive is on
      • Connections getting accepted but never serviced




                                                           38
Tuning timeouts


 • connectionTimeout
   – Values from 2000-60000
   – Represents the SO_TIMEOUT value
   – Essentially, max time between TCP packets
     during a blocking read or write
   – Critical to a stable system
   – Also used for keep alive timeout




                                                 39
Tuning timeouts


 • connectionTimeout=“3000”
   – Increase if
      • Working with slow clients (dial up)
      • Using a layer 7 load balancer with connection
        limit/pool and keep alive on
   – Decrease if
      • Need faster timeouts


   – Default value of 20,000 (20secs) is too high for
     a web server


                                                        40
Content Delivery


 • Dynamic content
   – No caching done
   – Tomcat has to deliver it blocking mode
   – Worker thread is not released until all content
     has been delivered
   – Fast dynamic content can piggy back on send
     file
       •Simply write to file, set request attribute and
       hand off to Tomcat's poller threads



                                                          41
Content Delivery


 • Static content
   – Size based cache, default 10mb
   – BIO - Tomcat has to deliver it blocking mode
   – NIO/APR
      • Tomcat can use SEND_FILE
      • Release worker thread, deliver the content using a
        background thread when the client is ready to receive




                                                                42
Content Delivery


 • Configured in <Context> element
 • 40MB cache (default 10MB)
 • cache revalidation every 60 seconds (default 5
   seconds)
 • caching enabled (default true)
 <Context cacheMaxSize=”40960” cacheTTL=”60000”
          cachingAllowed=”true”>
 </Context>




                                                    43
JVM Tuning


 • Key parameters for JVM tuning
   – Memory
   – Garbage collection
 • They are not independent




                                   44
JVM Tuning: The ideal


 • Short lived objects never reach the Old
   Generation
 • Short lived objects cleaned up by short
   minor garbage collections
 • Long lived objects promoted to Old
   Generation
 • Long lived objects cleaned up by (rare) full
   garbage collection



                                                  45
JVM Tuning: Memory


 • -Xms/-Xmx
   – Used to define size of Java heap
   – Aim to set as low as possible
   – Setting too high can cause wasted memory and
     long GC cycles
 • -XX:NewSize/-XX:NewRatio
   – Set to 25-33% of total Java heap
   – Setting too high or too low leads to inefficient
     GC
   – Often these are not tuned, GC log will reveal

                                                        46
JVM Tuning: GC


 • GC pauses the application
   – Regardless of GC algorithm
 • Pause can range from milliseconds to
   seconds
 • The pause will impact your response time
   – How much does this matter?




                                              47
JVM Tuning: GC


 • -XX:MaxGCPauseMillis
   –   Set GC pause time goal
   –   More frequent GC
   –   Shorter pauses
   –   Goal is for major collections
 • -XX:MaxGCMinorPauseMillis
   – Applies to young generation




                                       48
JVM Tuning: Try it out


 • GC Settings – JDK 1.5 and 1.6
   -XX:+UseConcMarkSweepGC
   -XX:+CMSIncrementalMode
   -XX:+CMSIncrementalPacing
   -XX:CMSIncrementalDutyCycleMin=0
   -XX:CMSIncrementalDutyCycle=10
   -XX:+UseParNewGC
   -XX:+CMSPermGenSweepingEnabled
   -XX:+CMSClassUnloadingEnabled
   -XX:MaxGCPauseMillis=250
   -XX:MaxGCMinorPauseMillis=100


                                      49
JVM Tuning


 • Much bigger topic
 • Same tuning rules apply
    – Doesn’t compensate for bad, slow or poorly
      written applications
 • Sun JDK options
 http://blogs.sun.com/watt/resource/jvm-options-list.html




                                                            50
Questions...




               and answers!




                              51

Contenu connexe

Tendances

Distributed Multi-GPU Computing with Dask, CuPy and RAPIDS
Distributed Multi-GPU Computing with Dask, CuPy and RAPIDSDistributed Multi-GPU Computing with Dask, CuPy and RAPIDS
Distributed Multi-GPU Computing with Dask, CuPy and RAPIDS
PeterAndreasEntschev
 
ProxySQL Tutorial - PLAM 2016
ProxySQL Tutorial - PLAM 2016ProxySQL Tutorial - PLAM 2016
ProxySQL Tutorial - PLAM 2016
Derek Downey
 

Tendances (20)

HTTP2 and gRPC
HTTP2 and gRPCHTTP2 and gRPC
HTTP2 and gRPC
 
Introduction to Apache Kafka
Introduction to Apache KafkaIntroduction to Apache Kafka
Introduction to Apache Kafka
 
Introduction to Apache ZooKeeper
Introduction to Apache ZooKeeperIntroduction to Apache ZooKeeper
Introduction to Apache ZooKeeper
 
LinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking WalkthroughLinuxCon 2015 Linux Kernel Networking Walkthrough
LinuxCon 2015 Linux Kernel Networking Walkthrough
 
MariaDB Galera Cluster - Simple, Transparent, Highly Available
MariaDB Galera Cluster - Simple, Transparent, Highly AvailableMariaDB Galera Cluster - Simple, Transparent, Highly Available
MariaDB Galera Cluster - Simple, Transparent, Highly Available
 
Cilium - Container Networking with BPF & XDP
Cilium - Container Networking with BPF & XDPCilium - Container Networking with BPF & XDP
Cilium - Container Networking with BPF & XDP
 
Apache Spark Core
Apache Spark CoreApache Spark Core
Apache Spark Core
 
Uber: Kafka Consumer Proxy
Uber: Kafka Consumer ProxyUber: Kafka Consumer Proxy
Uber: Kafka Consumer Proxy
 
Kafka 101
Kafka 101Kafka 101
Kafka 101
 
Working with JSON Data in PostgreSQL vs. MongoDB
Working with JSON Data in PostgreSQL vs. MongoDBWorking with JSON Data in PostgreSQL vs. MongoDB
Working with JSON Data in PostgreSQL vs. MongoDB
 
MySQL with DRBD/Pacemaker/Corosync on Linux
 MySQL with DRBD/Pacemaker/Corosync on Linux MySQL with DRBD/Pacemaker/Corosync on Linux
MySQL with DRBD/Pacemaker/Corosync on Linux
 
Introduction to Spark Internals
Introduction to Spark InternalsIntroduction to Spark Internals
Introduction to Spark Internals
 
Distributed Multi-GPU Computing with Dask, CuPy and RAPIDS
Distributed Multi-GPU Computing with Dask, CuPy and RAPIDSDistributed Multi-GPU Computing with Dask, CuPy and RAPIDS
Distributed Multi-GPU Computing with Dask, CuPy and RAPIDS
 
Flink history, roadmap and vision
Flink history, roadmap and visionFlink history, roadmap and vision
Flink history, roadmap and vision
 
Héberger son site web
Héberger son site webHéberger son site web
Héberger son site web
 
ProxySQL Tutorial - PLAM 2016
ProxySQL Tutorial - PLAM 2016ProxySQL Tutorial - PLAM 2016
ProxySQL Tutorial - PLAM 2016
 
Alphorm.com Formation Elastic : Maitriser les fondamentaux
Alphorm.com Formation Elastic : Maitriser les fondamentauxAlphorm.com Formation Elastic : Maitriser les fondamentaux
Alphorm.com Formation Elastic : Maitriser les fondamentaux
 
Apache Spark Architecture
Apache Spark ArchitectureApache Spark Architecture
Apache Spark Architecture
 
gRPC Design and Implementation
gRPC Design and ImplementationgRPC Design and Implementation
gRPC Design and Implementation
 
Tuning kafka pipelines
Tuning kafka pipelinesTuning kafka pipelines
Tuning kafka pipelines
 

En vedette

En vedette (20)

Tomcat Optimisation & Performance Tuning
Tomcat Optimisation & Performance TuningTomcat Optimisation & Performance Tuning
Tomcat Optimisation & Performance Tuning
 
Apache Performance Tuning: Scaling Out
Apache Performance Tuning: Scaling OutApache Performance Tuning: Scaling Out
Apache Performance Tuning: Scaling Out
 
25 Apache Performance Tips
25 Apache Performance Tips25 Apache Performance Tips
25 Apache Performance Tips
 
Autostima in 140 caratteri: la recensione a 5 stelle di Egidio su Amazon!
Autostima in 140 caratteri: la recensione a 5 stelle di Egidio su Amazon!Autostima in 140 caratteri: la recensione a 5 stelle di Egidio su Amazon!
Autostima in 140 caratteri: la recensione a 5 stelle di Egidio su Amazon!
 
入門系の本を一冊も読まずにデータサイエンスに入門してみる
入門系の本を一冊も読まずにデータサイエンスに入門してみる入門系の本を一冊も読まずにデータサイエンスに入門してみる
入門系の本を一冊も読まずにデータサイエンスに入門してみる
 
Why Smart Brands use Characters
Why Smart Brands use CharactersWhy Smart Brands use Characters
Why Smart Brands use Characters
 
Appear.in premium walkthrough
Appear.in premium walkthroughAppear.in premium walkthrough
Appear.in premium walkthrough
 
Si案件でGo言語を使ってみた!
Si案件でGo言語を使ってみた!Si案件でGo言語を使ってみた!
Si案件でGo言語を使ってみた!
 
Protocolo de estado epileptico
Protocolo de estado epilepticoProtocolo de estado epileptico
Protocolo de estado epileptico
 
Goをカンストさせる話
Goをカンストさせる話Goをカンストさせる話
Goをカンストさせる話
 
Drupal Developer Days Keynote
Drupal Developer Days KeynoteDrupal Developer Days Keynote
Drupal Developer Days Keynote
 
JavaCro'15 - Spring @Async - Dragan Juričić
JavaCro'15 - Spring @Async - Dragan JuričićJavaCro'15 - Spring @Async - Dragan Juričić
JavaCro'15 - Spring @Async - Dragan Juričić
 
条件式評価器の実装による管理ツールの抽象化
条件式評価器の実装による管理ツールの抽象化条件式評価器の実装による管理ツールの抽象化
条件式評価器の実装による管理ツールの抽象化
 
HoloLens x Graphics 入門
HoloLens x Graphics 入門HoloLens x Graphics 入門
HoloLens x Graphics 入門
 
The Marketer's Guide To Customer Interviews
The Marketer's Guide To Customer InterviewsThe Marketer's Guide To Customer Interviews
The Marketer's Guide To Customer Interviews
 
ELSA France "Teaching is us!"
ELSA France "Teaching is us!" ELSA France "Teaching is us!"
ELSA France "Teaching is us!"
 
The Be-All, End-All List of Small Business Tax Deductions
The Be-All, End-All List of Small Business Tax DeductionsThe Be-All, End-All List of Small Business Tax Deductions
The Be-All, End-All List of Small Business Tax Deductions
 
Digital in 2017 Global Overview
Digital in 2017 Global OverviewDigital in 2017 Global Overview
Digital in 2017 Global Overview
 
Lefebvre Nare Afm170908
Lefebvre Nare Afm170908Lefebvre Nare Afm170908
Lefebvre Nare Afm170908
 
Pepsi Gravitational Field
Pepsi Gravitational FieldPepsi Gravitational Field
Pepsi Gravitational Field
 

Similaire à Tomcatx performance-tuning

Web Server/App Server Connectivity
Web Server/App Server ConnectivityWeb Server/App Server Connectivity
Web Server/App Server Connectivity
webhostingguy
 
Debugging applications with network security tools
Debugging applications with network security toolsDebugging applications with network security tools
Debugging applications with network security tools
ConFoo
 
Enterprise(d) Tomcat & httpd
Enterprise(d) Tomcat & httpdEnterprise(d) Tomcat & httpd
Enterprise(d) Tomcat & httpd
Vaclav Tunka
 
Deploying flash storage for Ceph without compromising performance
Deploying flash storage for Ceph without compromising performance Deploying flash storage for Ceph without compromising performance
Deploying flash storage for Ceph without compromising performance
Ceph Community
 
Apache Kafka
Apache KafkaApache Kafka
Apache Kafka
Joe Stein
 

Similaire à Tomcatx performance-tuning (20)

Web Server/App Server Connectivity
Web Server/App Server ConnectivityWeb Server/App Server Connectivity
Web Server/App Server Connectivity
 
High perf-networking
High perf-networkingHigh perf-networking
High perf-networking
 
Fastest Servlets in the West
Fastest Servlets in the WestFastest Servlets in the West
Fastest Servlets in the West
 
Introduction to Apache Tomcat 7 Presentation
Introduction to Apache Tomcat 7 PresentationIntroduction to Apache Tomcat 7 Presentation
Introduction to Apache Tomcat 7 Presentation
 
(ATS4-PLAT01) Core Architecture Changes in AEP 9.0 and their Impact on Admini...
(ATS4-PLAT01) Core Architecture Changes in AEP 9.0 and their Impact on Admini...(ATS4-PLAT01) Core Architecture Changes in AEP 9.0 and their Impact on Admini...
(ATS4-PLAT01) Core Architecture Changes in AEP 9.0 and their Impact on Admini...
 
Debugging applications with network security tools
Debugging applications with network security toolsDebugging applications with network security tools
Debugging applications with network security tools
 
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...
 
Flink Streaming @BudapestData
Flink Streaming @BudapestDataFlink Streaming @BudapestData
Flink Streaming @BudapestData
 
Enterprise(d) Tomcat & httpd
Enterprise(d) Tomcat & httpdEnterprise(d) Tomcat & httpd
Enterprise(d) Tomcat & httpd
 
RINA overview and ongoing research in EC-funded projects, ISO SC6 WG7
RINA overview and ongoing research in EC-funded projects, ISO SC6 WG7RINA overview and ongoing research in EC-funded projects, ISO SC6 WG7
RINA overview and ongoing research in EC-funded projects, ISO SC6 WG7
 
Deploying flash storage for Ceph without compromising performance
Deploying flash storage for Ceph without compromising performance Deploying flash storage for Ceph without compromising performance
Deploying flash storage for Ceph without compromising performance
 
HPC Controls Future
HPC Controls FutureHPC Controls Future
HPC Controls Future
 
Scale your Alfresco Solutions
Scale your Alfresco Solutions Scale your Alfresco Solutions
Scale your Alfresco Solutions
 
Towards constrained semantic web
Towards constrained semantic webTowards constrained semantic web
Towards constrained semantic web
 
Apache Kafka
Apache KafkaApache Kafka
Apache Kafka
 
ECS19 - Ingo Gegenwarth - Running Exchange in large environment
ECS19 - Ingo Gegenwarth -  Running Exchangein large environmentECS19 - Ingo Gegenwarth -  Running Exchangein large environment
ECS19 - Ingo Gegenwarth - Running Exchange in large environment
 
RxNetty vs Tomcat Performance Results
RxNetty vs Tomcat Performance ResultsRxNetty vs Tomcat Performance Results
RxNetty vs Tomcat Performance Results
 
CPN302 your-linux-ami-optimization-and-performance
CPN302 your-linux-ami-optimization-and-performanceCPN302 your-linux-ami-optimization-and-performance
CPN302 your-linux-ami-optimization-and-performance
 
Apache Performance Tuning: Scaling Up
Apache Performance Tuning: Scaling UpApache Performance Tuning: Scaling Up
Apache Performance Tuning: Scaling Up
 
SF-TAP: Scalable and Flexible Traffic Analysis Platform (USENIX LISA 2015)
SF-TAP: Scalable and Flexible Traffic Analysis Platform (USENIX LISA 2015)SF-TAP: Scalable and Flexible Traffic Analysis Platform (USENIX LISA 2015)
SF-TAP: Scalable and Flexible Traffic Analysis Platform (USENIX LISA 2015)
 

Plus de Vladimir Khokhryakov

Tomcatx troubleshooting-production
Tomcatx troubleshooting-productionTomcatx troubleshooting-production
Tomcatx troubleshooting-production
Vladimir Khokhryakov
 

Plus de Vladimir Khokhryakov (11)

Application specialist in Riga
Application specialist in RigaApplication specialist in Riga
Application specialist in Riga
 
Software 2001
Software 2001Software 2001
Software 2001
 
33 mhz
33 mhz33 mhz
33 mhz
 
Quake I
Quake IQuake I
Quake I
 
Tomcatx troubleshooting-production
Tomcatx troubleshooting-productionTomcatx troubleshooting-production
Tomcatx troubleshooting-production
 
Dos5.0
Dos5.0Dos5.0
Dos5.0
 
Windows3.1
Windows3.1Windows3.1
Windows3.1
 
Windows NT
Windows NTWindows NT
Windows NT
 
Antivirus93
Antivirus93Antivirus93
Antivirus93
 
Macintosh против PC.1991
Macintosh против PC.1991Macintosh против PC.1991
Macintosh против PC.1991
 
Next 1989
Next 1989Next 1989
Next 1989
 

Tomcatx performance-tuning

  • 1. Tomcat Expert Series Performance Tuning Filip Hanik SpringSource 2009 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
  • 2. Agenda • Performance Tuning Process • Logging improvements • TCP and HTTP • Tuning your connectors • Content delivery and caching • Tuning the JVM 2
  • 3. The process • Understand the system architecture • Stabilise the system • Set Performance Targets – Web applications are easy – Only one consideration – request/response time 3
  • 4. The process • Measure current performance • Identify the current bottleneck – Focus on one item at a time • Fix the root cause – Easy to get side tracked 4
  • 5. The Process • When possible, tune pre-production – Hard to profile in production • Application tuning most important – 80% or more of request time is typically spent inside the application • Tomcat tuning is fairly limited – Divided between JVM tuning and Tomcat connectors – Requires lower level of understanding 5
  • 6. Apache Tomcat in Production • Out of the Box Tomcat – Tomcat is ready for production • JVM settings must be applied – Default memory settings usually too small for most web applications • Tuning is limited – So we can cover most of it 6
  • 7. Logging • Tomcat logging is fairly good – Years of adjusting log levels pays off – Doesn't log what you don't need to see • A few gotcha's with the default configuration – Catch all logger creates duplicate logs • Standard out – often piped to catalina.out • Log file on the file system – Synchronized logging – No overflow protection 7
  • 8. Logging • Tomcat's logger – Rotated based on date – Implements a per-class-loader logger • Simply drop logging.properties into your web application and logging is configured – Synchronous logging – No file limit 8
  • 9. Logging • Java Virtual Machine logger – Rotated based on size – One global configuration for entire JVM – Synchronous logging 9
  • 10. Logging Remove duplicate logging (logging.properties) .handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler Adjusted catch all logger .handlers = 1catalina.org.apache.juli.FileHandler 10
  • 11. Logging • Overflow protection – Size based rotation using JVM logger handlers = 1catalina.java.util.logging.FileHandler,… – No more than 5x20mb files 1catalina.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/catalina.%g.log 1catalina.java.util.logging.FileHandler.limit = 20000000 1catalina.java.util.logging.FileHandler.count = 5 11
  • 12. Apache Tomcat in Production • Tuning Tomcat connectors – server.xml – <Connector> • To properly tune one must – Understand the TCP protocol – Understand how the CPU works – Understand load balancers and their algorithms 12
  • 13. TCP • Layer 4 in the OSI stack • Session based • TCP stack implementation keeps state • Flow control • Delivery guarantee 13
  • 14. TCP: Session based • Setup and break down handshakes • Client response time – Handshakes add to HTTP transaction time – HTTP keep alive improves client response time – HTTP keep alive takes up server resources 14
  • 15. TCP: Stateful protocol • Each connection represented by – source address – source port – destination address – destination port • This is all the information a layer 4 load balancer has for load balancing 15
  • 16. TCP: Flow control • Prevents buffer overflow and lost data • Server must adjust write speed to client’s ability to read • Servlet specification is blocking IO – Utilize a thread for the entire HTTP transaction – For static content, Tomcat offers SEND_FILE with APR and NIO connectors 16
  • 17. TCP: No Keep-Alive User Agent Server TCP Setup (syn) TCP Setup (syn/ack) TCP Setup (ack) HTTP request servlet single HTTP HTTP response thread transaction in use TCP Close (fin) Close can be TCP Close (fin,ack) done in different TCP Close(ack) ways 17
  • 18. TCP: Keep-Alive User Agent Server TCP Setup (syn) TCP Setup (syn/ack) TCP Setup (ack) HTTP request servlet multiple HTTP HTTP response thread transactions in use TCP Abort(ack) TCP Abort (rst) 18
  • 19. TCP: Summary • How does TCP affect our system? – Traffic patterns • High concurrency/short requests • Low concurrency/long requests • Static content • Dynamic content • Combination • Variations • Average size of request – It’s these patterns that decide how to tune our system 19
  • 20. HTTP • Layer 7 in the OSI stack • Stateless protocol 20
  • 21. HTTPS • HTTP over SSL • Expensive handshake – Keep alive makes a big difference • Encryption hides HTTP from routing devices • For any appliances, such as LB, this means – Fallback to layer 4 21
  • 22. Load Balancing: TCP/HTTP • TCP – Based on destination address/port – Connection centric – 1:1 – Can lead to uneven loads • HTTP – Based on HTTP headers – Can reuse server connections – Can drain sessions 22
  • 23. Load Balancing: Algorithms • Load balancing – Connection limits – Reusing connections – Traffic shaping • Load balancing algorithm drive Tomcat configuration choices 23
  • 24. Apache Tomcat: HTTP/S • Our tuning options – Threads – Keep alive requests – TCP Backlog (acceptCount) – connectionTimeout – Socket buffers • Different connectors – BIO – Blocking Java connector, default – APR – Uses native C code for IO – NIO – Non blocking Java connectors 24
  • 25. Apache Tomcat: HTTP/S • Disclaimer – Tuning options are meant for working and high performing applications – Options will not fix bad application behavior – If application is not tuned • Situation can worsen 25
  • 26. Which connector? • Use BIO if: – Stability is the highest priority • APR and NIO are more recent – Most content is dynamic – Keep alive is not a determining factor protocol=“org.apache.coyote.http11.Http11Protocol” 26
  • 27. Which connector? • Use APR if: – SSL is terminated at Tomcat – Keep alive is important – Lots of static content – Using Comet feature – Requires compilation of native library protocol=“org.apache.coyote.http11.Http11AprProtocol” 27
  • 28. Which connector? • Use NIO if: – Compiling APR is not an option – Keep alive is important – Using SSL – Lots of static content – Using Comet features protocol=“org.apache.coyote.http11.Http11NioProtocol” 28
  • 29. Which connector? • If uncertain: – Use BIO connector – Most mature code, both in Tomcat and JVM – Will not break down – Auto tune feature to disable keep alive • When hitting 75% if maxThreads in connection count protocol=“org.apache.coyote.http11.Http11Protocol” 29
  • 30. Which connector? Comparison Chart Java BIO Java NIO APR Class Http11Protocol Http11NioProtocol Http11AprProtocol Version 3.x+ 6.x+ 5.5.x+ Polling NO YES YES Polling Size N/A Unlimited Configurable HTTP Req Read Blocking Non blocking Blocking HTTP Body Read Blocking Sim Blocking Blocking HTTP Write Blocking Sim Blocking Blocking SSL JSSE JSSE OpenSSL SSL Handshake Blocking Non blocking Blocking Max Connections maxThreads Unlimited Configurable 30
  • 31. Which connector? • If uncertain: – Use BIO connector – Most mature code, both in Tomcat and JVM – Will not break down – Auto tune feature to disable keep alive • When hitting 75% if maxThreads in connection count protocol=“org.apache.coyote.http11.Http11Protocol” 31
  • 32. Which connector? • If uncertain: – Use BIO connector – Most mature code, both in Tomcat and JVM – Will not break down – Auto tune feature to disable keep alive • When hitting 75% if maxThreads in connection count protocol=“org.apache.coyote.http11.Http11Protocol” 32
  • 33. Tuning threads • maxThreads – Typical range 200-800 – Maximum nr of concurrent requests – For BIO, max nr of open/active connections – Good starting value 400 33
  • 34. Tuning threads • maxThreads=“400” – Decrease if you see heavy CPU usage • Application might be CPU bound instead of IO bound • Find out what is causing CPU usage – Increase if you don’t see much CPU usage • Applications could be synchronized -> no gain • Take into account other resources, such as database connections 34
  • 35. Tuning keep alive • maxKeepAliveRequests – Typical values 1, 100-200 – Represents the number of requests Tomcat will handle on a TCP connection – Set to 1 disables keep alive – connectionTimeout/keepAliveTimeout controls the timeout in between requests 35
  • 36. Tuning keep alive • maxKeepAliveRequests – Set to 1 if • Very high concurrency • Not using SSL in Tomcat • Using layer 4 load balancer • Using BIO connector – Set to >1 if • Using SSL or low concurrency • Layer 7 load balancer with advanced features • Using APR or NIO connector – BIO connector automatically disables keep alive for high connection counts 36
  • 37. Tuning TCP backlog • acceptCount – Typical ranges 50-300 – Represents nr of additional connections the OS should accept on your behalf – Useful when Tomcat can’t accept connections fast enough 37
  • 38. Tuning TCP backlog • acceptCount=“100” – Increase if • Very high concurrency (nr of connections) • Connections getting rejected during peak traffic • Keep alive should be off – Decrease if • Keep alive is on • Connections getting accepted but never serviced 38
  • 39. Tuning timeouts • connectionTimeout – Values from 2000-60000 – Represents the SO_TIMEOUT value – Essentially, max time between TCP packets during a blocking read or write – Critical to a stable system – Also used for keep alive timeout 39
  • 40. Tuning timeouts • connectionTimeout=“3000” – Increase if • Working with slow clients (dial up) • Using a layer 7 load balancer with connection limit/pool and keep alive on – Decrease if • Need faster timeouts – Default value of 20,000 (20secs) is too high for a web server 40
  • 41. Content Delivery • Dynamic content – No caching done – Tomcat has to deliver it blocking mode – Worker thread is not released until all content has been delivered – Fast dynamic content can piggy back on send file •Simply write to file, set request attribute and hand off to Tomcat's poller threads 41
  • 42. Content Delivery • Static content – Size based cache, default 10mb – BIO - Tomcat has to deliver it blocking mode – NIO/APR • Tomcat can use SEND_FILE • Release worker thread, deliver the content using a background thread when the client is ready to receive 42
  • 43. Content Delivery • Configured in <Context> element • 40MB cache (default 10MB) • cache revalidation every 60 seconds (default 5 seconds) • caching enabled (default true) <Context cacheMaxSize=”40960” cacheTTL=”60000” cachingAllowed=”true”> </Context> 43
  • 44. JVM Tuning • Key parameters for JVM tuning – Memory – Garbage collection • They are not independent 44
  • 45. JVM Tuning: The ideal • Short lived objects never reach the Old Generation • Short lived objects cleaned up by short minor garbage collections • Long lived objects promoted to Old Generation • Long lived objects cleaned up by (rare) full garbage collection 45
  • 46. JVM Tuning: Memory • -Xms/-Xmx – Used to define size of Java heap – Aim to set as low as possible – Setting too high can cause wasted memory and long GC cycles • -XX:NewSize/-XX:NewRatio – Set to 25-33% of total Java heap – Setting too high or too low leads to inefficient GC – Often these are not tuned, GC log will reveal 46
  • 47. JVM Tuning: GC • GC pauses the application – Regardless of GC algorithm • Pause can range from milliseconds to seconds • The pause will impact your response time – How much does this matter? 47
  • 48. JVM Tuning: GC • -XX:MaxGCPauseMillis – Set GC pause time goal – More frequent GC – Shorter pauses – Goal is for major collections • -XX:MaxGCMinorPauseMillis – Applies to young generation 48
  • 49. JVM Tuning: Try it out • GC Settings – JDK 1.5 and 1.6 -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:+CMSIncrementalPacing -XX:CMSIncrementalDutyCycleMin=0 -XX:CMSIncrementalDutyCycle=10 -XX:+UseParNewGC -XX:+CMSPermGenSweepingEnabled -XX:+CMSClassUnloadingEnabled -XX:MaxGCPauseMillis=250 -XX:MaxGCMinorPauseMillis=100 49
  • 50. JVM Tuning • Much bigger topic • Same tuning rules apply – Doesn’t compensate for bad, slow or poorly written applications • Sun JDK options http://blogs.sun.com/watt/resource/jvm-options-list.html 50
  • 51. Questions... and answers! 51