Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Tomcat Practice Exercises and Interview Questions

Practice Tomcat operations with sizing exercises and runnable Java calculators, then review common Tomcat interview questions on security, clustering, tuning and troubleshooting.

You have now covered Tomcat's security, clustering, monitoring and troubleshooting. This last lesson helps you lock it in. It has three parts: sizing exercises you can solve with small runnable calculators, scenario questions for deciding what to do, and a set of interview questions with short model answers. Try each exercise before you read the answer.

Tomcat cannot run on this site, so the calculators work with numbers you would normally read from JMX, logs or a load test. They model the arithmetic behind operational decisions.

Exercise 1: how many threads?

A service receives 300 requests per second at peak. The average request takes 120 ms, and the p99 takes 900 ms. The database pool has 40 connections and each request holds one connection for about 60% of its time. What maxThreads is reasonable, and what limits you first?

Use Little's Law (requests in flight equal arrival rate times time in system) from thread pools. The calculator below does the arithmetic and compares it to the database pool.

Java Exercise 1: thread pool sizing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Result: about 36 requests in flight, so around 72 threads with headroom, which is far below the default 200. The database needs about 22 connections on average and up to 43 at the busy end, which touches the pool size of 40, so the database pool is the first limit. Raising maxThreads would not help; the p99 of 900 ms suggests some requests wait for connections, so fix that first.

Exercise 2: heap for sessions

You expect 15,000 concurrent sessions of about 40 KB each, with a 30-minute timeout. Other heap use is about 600 MB. How big should -Xmx be, and how can you shrink it?

Java Exercise 2: session memory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Result: roughly 586 MB of sessions plus 600 MB gives a live set near 1.2 GB, and a heap of about 2 GB keeps it near 60%. Trimming each session to 10 KB cuts the sessions to about 146 MB. If sessions are replicated, smaller sessions also mean less network traffic.

Scenario questions

Think about each, then compare with the notes.

  1. Users report being logged out at random after you add a second Tomcat behind a load balancer. Likely cause: sessions live in one node's memory and requests alternate between nodes. Fix with sticky sessions (unique jvmRoute per node), replication with <distributable/> and serializable attributes, or external session storage. See load balancing.
  2. After every redeploy the server needs a restart eventually. OutOfMemoryError: Metaspace means classloaders are leaking: look for threads, drivers and ThreadLocals from the old application (memory leaks).
  3. The server is up, CPU is idle, but requests time out. Take three thread dumps; worker threads are probably blocked on a database or remote call with no timeout.
  4. Your security scan flags TLS 1.0 and the Server header. Set protocols="TLSv1.3,TLSv1.2" and override server on the connector (TLS, hardening).
  5. The manager returns 403 from your office network. The RemoteCIDRValve in the manager's context.xml only allows localhost; add your network deliberately (valves).

Interview questions with short answers

What is the difference between Tomcat and a full Jakarta EE application server? Tomcat implements the web-tier specifications (Servlet, JSP, WebSocket, Expression Language), not the whole platform (no EJB or JMS out of the box). Frameworks like Spring add the rest.

What are Catalina, Coyote and Jasper? Catalina is the servlet container, Coyote the connector that speaks HTTP, and Jasper the JSP engine.

What is the difference between CATALINA_HOME and CATALINA_BASE? HOME is the Tomcat installation (binaries); BASE is the instance directory with conf, logs, webapps and work. Several instances can share one HOME.

How does Tomcat find the session for a request? From the JSESSIONID cookie, whose value is looked up in the session manager's map. With jvmRoute the id carries a node suffix for sticky routing.

Valve or filter? A valve is Tomcat-specific, runs in the container pipeline and is configured in server.xml/context.xml. A filter is a portable Servlet component in the WAR.

How do you secure the manager application? Strong or hashed credentials, only the roles needed, a RemoteCIDRValve restricting addresses, HTTPS, or removing it.

What does unpackWARs do? Extracts the WAR into a directory so files are served from disk. With false Tomcat reads from the archive, which is slower for some content and unsuitable for apps that need real paths.

How do you do zero-downtime deployment? Parallel deployment with versioned WAR names, or rolling upgrades behind a load balancer with draining (deployment project).

Explain maxThreads, maxConnections and acceptCount. Parallel request processing limit, accepted open connection limit and OS backlog length once connections are at the limit.

What changed from Tomcat 9 to 10? The move from javax.* to jakarta.* packages; applications and libraries must match.

How do you find a memory leak? Watch heap after GC, capture a heap dump, open it in MAT and follow the path to GC roots; check ThreadLocals and classloaders after redeploys.

How do you make a Tomcat cluster resilient? Distributable app, serializable session data, replication or externalized sessions, health checks, sticky routing and tested failover.

A self-check script

The last stand-in is a small self-test you can extend with your own flash cards. It asks a question and checks an answer keyword, mimicking revision practice:

Java Revision flash cards

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

What to study next

Return to the chapters for the topics you answered least confidently, and then try building the mini servlet container extensions, or run a real Tomcat locally and test every command from this chapter. Compare your server.xml with the review project. The best preparation is breaking a test installation on purpose (fill a port, leak a ThreadLocal, redeploy 20 times) and fixing it with logs and dumps.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A service has 250 requests per second at 200 ms each. About how many requests are in flight on average?

    250 times 0.2 is 50 requests, so allow some headroom above that for maxThreads.
  2. 2. What is CATALINA_BASE?

    BASE separates instance files from the shared Tomcat binaries in CATALINA_HOME.
  3. 3. Which component is Tomcat's JSP engine?

    Jasper compiles JSPs into servlets; Catalina is the container and Coyote the connector.
  4. 4. Tomcat is up and the CPU is idle, yet requests hang. What is the best first diagnostic?

    Idle CPU with hanging requests usually means threads are blocked or waiting; dumps show where.
  5. 5. Why did applications break moving from Tomcat 9 to Tomcat 10?

    Jakarta EE 9 renamed the packages, so code and libraries must use jakarta.* names.

Summary

  • Size pools and heaps with simple arithmetic, then verify with load tests and JMX.
  • Match symptoms to causes: random logouts mean sessions and routing, growing Metaspace means classloader leaks, idle CPU with hangs means blocked threads.
  • Know the vocabulary: Catalina, Coyote, Jasper, valves, filters, realms, CATALINA_BASE, jvmRoute.
  • Practice by breaking and fixing a test installation.

Next: back to the Apache Tomcat course page