BlogTomcat

How to Run Tomcat in Production: Threads, Timeouts, Sessions

Scott Sternloff

By Scott Sternloff, Senior Enterprise Architect

LinkedIn · Updated · 5 min read

How to Run Tomcat in Production: Threads, Timeouts, Sessions
On this page

Short answer

Running Tomcat in production comes down to five settings you should choose on purpose: the connector thread pool (200 threads by default), the accept queue (100), the timeouts, the database pool behind the application, and where sessions live. Set each one from measured load, add a timeout to every outbound call, and keep a thread dump command ready.

Running Tomcat in production comes down to five settings you should choose on purpose: the connector thread pool (200 threads by default), the accept queue (100), the timeouts, the database pool behind the application, and where sessions live. Set each one from measured load, add a timeout to every outbound call, and keep a thread dump command ready.

The defaults below come from the Apache Tomcat 10.1 documentation. Tomcat 9.0 matches unless a section says otherwise. If a node is already hanging or failing, our tomcat consulting team starts from the same thread dump described in step 7.

What are the Tomcat connector defaults, and which ones matter?

The HTTP connector serves requests from one pool of up to 200 threads (maxThreads). When all threads are busy, Tomcat accepts connections until it reaches maxConnections (8192 for NIO and NIO2), and the operating system queues the overflow up to acceptCount (100).

Setting Default What it controls
maxThreads 200 Simultaneous requests processed
minSpareThreads (Executor) 25 Threads always kept alive
maxConnections 8192 (NIO, NIO2) Connections accepted while threads are busy
acceptCount 100 OS queue once maxConnections is reached
connectionTimeout 60000 ms in the docs, 20000 ms in the shipped server.xml Wait for the request line after accept
keepAliveTimeout Same as connectionTimeout Idle wait between requests on one connection
maxKeepAliveRequests 100 Requests per keep-alive connection

If you define an <Executor>, the connector ignores its own maxThreads and uses the executor's, which also defaults to 200.

What are the best practices for running Tomcat in a production environment?

These are the steps we check on a Tomcat server before it carries traffic for its web applications.

  1. Separate the install from the configuration. RUNNING.txt recommends setting CATALINA_HOME explicitly and using a different CATALINA_BASE for your configuration and web applications, with JAVA_HOME (or JRE_HOME) set to the Java you intend to run.
  2. Define one shared Executor. Name it, set maxThreads from a load test and minSpareThreads above the 25 default if traffic arrives in bursts, and point every connector at it with executor="...".
  3. Do not raise maxThreads to fix slowness. A larger pool only lets more requests wait on the same slow dependency. Raise it when threads are busy doing CPU or I/O work and the database can take the extra load.
  4. Set the timeouts on purpose. The shipped server.xml already sets connectionTimeout to 20000 ms, so check that your own server.xml kept it. Because keepAliveTimeout inherits it, a long connectionTimeout also holds idle keep-alive connections open for a long time.
  5. Size the database pool from measured concurrency. Apache Commons DBCP 2 defaults to maxTotal 8 and maxWaitMillis of -1, which waits forever. A pool of 8 under 200 request threads means 192 threads can sit waiting. Set maxWaitMillis to a few seconds so a pool problem becomes an error in the log, not a hang.
  6. Add a stuck thread valve. StuckThreadDetectionValve logs requests running longer than threshold (600 seconds by default). Set it far lower than that, and note that its threshold should be higher than the backgroundProcessorDelay of the container that declares it.
  7. Keep a thread dump command ready. Run jcmd <pid> Thread.print > dump.txt three times, ten seconds apart. The jcmd documentation rates Thread.print as medium impact, depending on the number of threads, so take the dumps one at a time.
  8. Disable what you do not use. Set the Server port to -1 to disable the shutdown port (or set a strong shutdown password). Remove the AJP connector unless a proxy needs it, and give it a secret if you keep it.
  9. Put JVM options in setenv.sh. RUNNING.txt names a setenv script ($CATALINA_BASE/bin/setenv.sh on Linux) as the recommended place for CATALINA_OPTS and similar variables, and it is read only when you start Tomcat with the standard scripts.

How do I read a Tomcat thread dump?

Count the threads named http-nio-8080-exec-N (the name follows your connector port) and group them by the frame they are waiting in. If 190 of 200 wait in the same place, that place is your problem.

Where the threads wait Likely cause First fix
Pool borrowObject or getConnection Database pool too small or connections leaked Set maxWaitMillis, check pool size and abandoned-connection settings
SocketInputStream.read or similar Outbound HTTP call with no read timeout Add connect and read timeouts to the client
Object.wait on one lock Contended synchronized block in the application Fix the code path, not the thread count
Few exec threads, mostly idle Problem is upstream of Tomcat Check the proxy, load balancer and DNS

What happens to sessions when Tomcat restarts?

It depends on the version. In Tomcat 9.0 the standard Manager's pathname defaults to SESSIONS.ser, so sessions are written to that file on a clean shutdown and read back on startup. In Tomcat 10.1 the pathname default is null, so sessions are not preserved across a restart unless you set it. Check this before you move a 9.0 application to 10.1, because users who stayed logged in through restarts will start being logged out.

For failover rather than restarts, you need replication, an external session store, or sticky routing at the load balancer. PersistentManager can swap idle sessions out to a file or database store. maxActiveSessions defaults to -1 (no limit), so cap it if memory is tight.

How do I run Tomcat in a container?

Size the JVM against the container limit, not the host, and leave room above the heap for Metaspace, thread stacks and direct buffers. Set a readiness probe that checks an application URL, not just the port.

For redeploy leaks, clearReferencesThreadLocals is already true by default, and clearReferencesRmiTargets needs an extra JVM option, so the usual fix is in the application or the library that holds the reference, not in Tomcat. If you want a second pair of eyes on any of this, ask a tomcat consultant to review your server.xml, executor and pool settings against a real thread dump.

Sources

Frequently Asked Questions

What is the default maxThreads in Tomcat?

200 for the HTTP connector, or the Executor's own `maxThreads` (also 200) if the connector uses a shared Executor. The `acceptCount` queue defaults to 100 and `maxConnections` to 8192 for NIO and NIO2.

Why is Tomcat slow when CPU is idle?

Request threads are usually waiting on something outside Tomcat, most often a database pool or an outbound call with no timeout. Take three thread dumps with `jcmd <pid> Thread.print` and look for many `exec` threads waiting in the same frame.

Does Tomcat keep sessions after a restart?

Tomcat 9.0 saves them to `SESSIONS.ser` by default on a clean shutdown. Tomcat 10.1 does not unless you set the Manager `pathname`. Neither helps after a crash or when a load balancer moves users to another node.

Scott Sternloff

Author

Scott SternloffSenior Enterprise Architect

Senior Enterprise Architect at AceMQ. Writes the technical coverage here: RabbitMQ, Kafka and Redis architecture, clustering and migrations.

Talk to an engineer

Need a hand with Tomcat?

Senior engineers who run it in production. Get a quote or a free consultation, no sales pitch.

Free Consultation

Get Expert Eyes on Your Tomcat Environment

Whether you're troubleshooting a production incident, planning a migration, or want a second opinion on your architecture — our team is ready. No pitch, just answers.

Email Us