performance tuning for apache tomcat

42
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Performance Tuning for Apache Tomcat Mark Thomas April 2009

Upload: best-tech-videos

Post on 12-Nov-2014

5.818 views

Category:

Documents


14 download

DESCRIPTION

Mark Thomas, a member of the Apache Tomcat PMC, explains the tuning process for Tomcat, JVM and the applications running on them considering different usage patterns, hardware and network configurations. Watch a video at http://www.bestechvideos.com/2009/08/19/performance-tuning-for-apache-tomcat

TRANSCRIPT

Page 1: Performance Tuning for Apache Tomcat

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

Performance Tuning for

Apache Tomcat

Mark Thomas

April 2009

Page 2: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2

Who am I?

Apache Tomcat committer

Resolved 1,500+ Tomcat bugs

Apache Tomcat PMC member

Member of the Apache Software Foundation

Member of the ASF security committee

Created the Tomcat security pages

Senior Software Engineer and Consultant at SpringSource

Page 3: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 4: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 5: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5

The process

Understand the system architecture

Stabilise the system

Set the performance target(s)�

Measure current performance

Identify the current bottleneck

Fix the root cause of the bottleneck

Repeat until you meet the target

Page 6: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6

Common errors

Optimising code that doesn't need it

Insufficient testing

realistic data volumes

realistic user load

Lack of clear performance targets

Guessing where the bottleneck is

Fixing the symptom rather than the cause

Page 7: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 8: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8

Tomcat tuning

Applications typically account for >80% of request processing time

Remember the tuning process

Focus your efforts on the bottlenecks

Page 9: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 10: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10

Production logging

Default configuration is generic

Some settings not ideal for production

catch-all logger logs to file and stdout

no overflow protection

Page 11: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11

Production logging

Remove duplicate logging (logging.properties)�

becomes

To add rotation

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

java.util.logging.ConsoleHandler

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

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

Page 12: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 13: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13

Connector tuning

Depends on

your application usage patterns

your network

TCP connections

HTTP transactions

HTTP Keep-Alive

SSL

Additional considerations for load balancing

Layer 4 or Layer 7

Connection pools

Page 14: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14

Connector tuning

HTTP/1.0 has no keep alive

Client creates TCP connection to server

Client sends request

Server sends response

Connection closed

Modern web pages can require >100 requests to completely display the page

Creating TCP connections can be expensive

Unlikely to be an issue on a LAN

May well be an issue for mobile devices

Page 15: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15

Connector tuning

HTTP/1.1 introduced keep alive

Client creates TCP connection to server

Client sends first request

Server sends first response

Client sends second request

Server sends second response

...

Connection closed

Connectors that use blocking IO use a thread to maintain the keep alive connection

Page 16: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16

Connector tuning

Layer 4 load balancer

Does not understand HTTP

Makes decisions based on client IP and port

Layer 7 load balancer

Understands HTTP

Can use HTTP headers to make decisions

Page 17: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17

Which connector?

Tomcat has a choice of three

Java Blocking IO

Oldest – most stable

JSSE based SSL

Native (APR)�

Non-blocking

Uses OpenSSL

Java Non-blocking IO

JSSE based SSL

Page 18: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18

Which connector?

Requirement Connectors in preference order

Stability BIO APR/NIO

SSL APR NIO BIO

Low concurrency BIO APR NIO

High concurrencyNo Keep-Alive

BIO APR NIO

High concurrencyKeep-Alive

APR NIO BIO

Page 19: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19

Which connector?

Why would you use the NIO connector?

The Native (APR) connector is unstable on Solaris

NIO is a pure Java solution

It is easy to switch between NIO and BIO with SSL

Page 20: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20

Connector tuning

maxThreads

maximum number of concurrent requests

for BIO, maximum number of open/active connections

typical values 200 to 800

400 is a good starting value

heavy CPU usage → decrease

light CPU usage → increase

Page 21: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21

Connector tuning

maxKeepAliveRequests

typical values 1 or 100

maximum number of HTTP requests per TCP

connection

set to 1 to disable keep alive

disable for BIO with very high concurrency, layer 4

load balancer, no SSL

enable for SSL, APR/NIO, layer 7 load balancer

Note BIO connector automatically disables keep alive

when concurrent connections reach 75% of maxThreads

Page 22: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22

Connector tuning

connectionTimeout

typical value 3000

default of 20000 is too high for production use

also used for keep alive time-out

increase for slow clients

increase for layer 7 load balancer with connection pool

and keep alive on

decrease for faster time-outs

Page 23: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 24: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24

Content cache tuning

Dynamic content is not cached

Static content is cached

Configured using the <Context .../> element

CacheMaxSize (KB)�

10240

CacheTTL (ms)�

5000

CacheMaxFileSize (KB) (6.0.19 onwards)�

512

NIO/APR can use SEND_FILE

Page 25: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 26: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26

JVM tuning

Two key areas

Memory

Garbage collection

They are related

Remember to follow the tuning process

Page 27: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27

JVM tuning

Java heap (Xmx, Xms) is not the same as the process heap

Process heap includes

Java Heap

Permanent Generation

Thread stacks

Native code

Directly allocated memory

Code generation

Garbage collection

TCP buffers

Read OutOfMemory exception messages carefully

Page 28: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28

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

Page 29: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29

JVM tuning: ideal garbage collection

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

Page 30: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 30

JVM tuning: garbage collection

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?

-XX:MaxGCPauseMillis -XX:MaxGCMinorPauseMillis

Set GC pause time goals

More frequent GC, shorter pauses

Page 31: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 31

JVM tuning: garbage collection

There are many more options

Useful reference

http://blogs.sun.com/watt/resource/jvm-options-

list.html

Newer GC algorithms may not behave the way you expect

Concurrent Mark Sweep

-XX:+UseConcMarkSweepGC

Does not use survivor spaces

Can be forced to; not recommended

Page 32: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 32

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 33: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 33

Scaling Tomcat

Load balancing

Routing requests to multiple Tomcat instances

Clustering

Sharing state between Tomcat instances for fail-over

Page 34: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 34

Scaling Tomcat

Simplest configuration

1 * httpd

2 * Tomcat instances

mod_proxy_http

Considerations

state management

fail over

Page 35: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 35

Scaling Tomcat

Stateless load-balancing using httpd

In httpd.conf:

# Cluster definition

<Proxy balancer://devcluster>

BalancerMember http://192.168.0.31:8080 disablereuse=On

BalancerMember http://192.168.0.32:8080 disablereuse=On

</Proxy>

# Route all requests to the cluster

ProxyPass / balancer://devcluster/

Page 36: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 36

Scaling Tomcat

Enabling the manager

In httpd.conf

# Pass all requests except the manager to the cluster

ProxyPass /balancer-manager !

ProxyPass / balancer://devcluster/

# Configure the manager

<Location /balancer-manager>

SetHandler balancer-manager

Order Deny,Allow

Deny from all

Allow from 127.0.0.1

</Location>

Page 37: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 37

Scaling Tomcat

Add sticky session support

Tomcat configuration

server.xml

jvmRoute must be unique for each instance

httpd configuration

Add route to each balancer member

Configure sticky sessions on ProxyPass

BalancerMember http://192.168.0.31:8080 disablereuse=On route=tc01

ProxyPass / balancer://devcluster/

nofailover=On stickysession=JSESSIONID|jsessionid

<Engine jvmRoute=”tc01”... />

Page 38: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 38

Scaling Tomcat

Add session replication

Application configuration (WEB-INF/web.xml)�

Add <distributable/>

Keep the session as small as possible

Session attributes must implement Serializable

Tomcat configuration (server.xml)�

Uncomment <Cluster ... /> element under <Engine ...

>

Defaults to get you started

Overview: /docs/cluster-howto.html

Details: /docs/config/cluster.html

Page 39: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 39

Scaling Tomcat

httpd configuration (httpd.conf)�

Fail over

Session replication asynchronous by default so usually used with sticky sessions

Single line configuration for defaults

Uses multicast for node discovery

Will need additional configuration for production use

ProxyPass / balancer://devcluster/

nofailover=Off stickysession=JSESSIONID|jsessionid

Page 40: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 40

Agenda

The optimisation / tuning process

Tomcat tuning options

logging

connectors

content cache

JVM

Scaling Tomcat

Hints and tips

Page 41: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 41

Hints and tips

Load balancing / clustering

use a minimum of 3 Tomcat instances

use load balancing and clustering in your development

environment

Redeployment can expose memory leaks

include this in your testing

Remember to follow the process

Page 42: Performance Tuning for Apache Tomcat

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 42

Questions?

[email protected]

http://www.springsource.com/webinars

[email protected]

[email protected]