Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 66 ยท Production Features

Performance Tuning

Make a Spring Boot app faster the right way: measure first, size pools and threads, use virtual threads, trim startup, set JVM flags, profile and cut JSON cost.

Most slow Spring Boot applications are not slow because of Spring. They are slow because of one missing database index, one query that runs a hundred times per request, a connection pool that is too small (or far too large), or a response that sends a megabyte of JSON nobody reads. Performance tuning is mostly finding which of those it is, and only then changing something.

This lesson gives you the order of work (measure, find the bottleneck, change one thing, measure again), then the main knobs: pools and threads, virtual threads, startup time, JVM flags, profiling tools, response compression and the cost of JSON. The Boot properties and tool commands are read-only; the TryIts run real Java so you can see sizes and the traps of micro-benchmarks.

The tuning loop

Measure Real numbers, realistic load.
Find the bottleneck One place usually dominates.
Change one thing So you know what helped.
Measure again Keep it only if the number moved.

Guessing is the slowest way to tune.

1 / 2

Measure before you tune

Start with the numbers that already exist. If you set up Observability, look at http.server.requests for the slow endpoint (is it slow always, or only the p99?), at hikaricp.connections.pending (are requests waiting for a database connection?) and at JVM memory and garbage-collection pauses. Then reproduce with a load test (k6, Gatling, JMeter or even wrk) against an environment that looks like production: same data volume, same database engine, same network. A benchmark on an empty H2 database on your laptop says almost nothing about production.

Decide what you are optimizing: latency (how long one request takes), throughput (requests per second) or cost (CPU and memory you pay for). They pull in different directions. And remember Amdahl's law: speeding up a part that is 5% of the request time can never give more than a 5% gain. Go where the time is.

Threads and pools: the usual bottlenecks

A request passes through several limited resources, and the smallest one sets your throughput.

Resource Boot property Default
Web server request threads (Tomcat) server.tomcat.threads.max 200
Database connections (HikariCP) spring.datasource.hikari.maximum-pool-size 10
Async task executor spring.task.execution.pool.core-size 8
Outgoing HTTP connections the client's own pool settings varies

The mismatch to notice: 200 request threads and 10 database connections. If every request needs the database, 190 threads wait in line at any moment. That is fine while queries take 5 ms; when they take 500 ms, requests queue up and latency explodes (you will see hikaricp.connections.pending rise and Connection is not available, request timed out errors).

The instinct is to raise the pool size, and it is usually wrong. A database can do real work on only about as many queries at once as it has CPU cores and disks, and extra connections add context switching and lock contention. A well-known rule of thumb for starting values is a pool of roughly (database cores x 2) plus a small number for disk waits, shared across all application instances (ten instances with a pool of 20 means 200 connections to the database). Then test. The fix for a pool that is too busy is often to make each connection busy for less time: faster queries, no network calls while holding a connection, and shorter transactions (Transactions covers keeping them small).

Two Boot-specific habits help:

  • Turn off open-in-view (spring.jpa.open-in-view=false) so database connections are not held while the view or JSON is being rendered.
  • Set sensible timeouts (spring.datasource.hikari.connection-timeout, HTTP client timeouts) so waiting is bounded (Resilience).

Virtual threads

With classic threads, concurrency is limited by the request thread pool, so blocking calls are expensive. On Java 21+ and Boot 3.2+, one property turns on virtual threads for request handling, @Async and scheduling:

PROPERTIES
spring.threads.virtual.enabled=true

Each request then gets its own cheap virtual thread, and a thread that waits for a database or HTTP response no longer blocks an expensive operating-system thread. This raises the number of concurrent requests you can have in flight for I/O-heavy services. It does not make a single request faster, and it does not help CPU-bound code. It also moves the bottleneck: if thousands of virtual threads wait for ten database connections, the pool is still the limit, so keep your pool size and rate limits in place (Scheduling and Asynchronous Work has the details). Measure with and without it; for a small app with little concurrency the difference may be zero.

Startup time

Startup matters for serverless functions, autoscaling (new instances should be useful quickly) and developer feedback. A typical Boot service starts in a few seconds; what dominates is class loading and creating beans, plus whatever your beans do while starting.

Things that help, from cheapest to most involved:

  • Start less. Remove unused starters, exclude auto-configurations you do not need (@SpringBootApplication(exclude = ...)), and narrow component scanning to your packages.
  • Do work later. Do not fill caches, call remote services or run heavy queries in constructors or @PostConstruct; use a background task after startup (Application Events and Runners).
  • Lazy initialization: spring.main.lazy-initialization=true creates beans only when first used. Startup gets faster and memory lower, but the cost moves to the first request that touches each bean (a slow first call), and configuration errors that used to stop startup now appear later, in production, at 2 a.m. It is helpful in local development and tests; in production, think twice, or mark just the expensive beans @Lazy.
  • Class Data Sharing (CDS) and the JVM's AOT cache let the JVM reuse preprocessed class metadata between runs, cutting startup noticeably with no code changes (Building and Running the Jar shows how).
  • GraalVM native images start in milliseconds, with trade-offs covered in Native Images with GraalVM.

To see where startup time goes, run with --debug or enable Actuator's startup endpoint: create the application with new SpringApplicationBuilder(App.class).applicationStartup(new BufferingApplicationStartup(2048)) and read /actuator/startup, which lists each startup step with its duration.

JVM flags that matter

Most defaults are good. The few worth knowing:

TEXT
-XX:MaxRAMPercentage=75           # heap as a share of the container's memory limit
-XX:+ExitOnOutOfMemoryError       # die and let the orchestrator restart, instead of limping
-XX:+HeapDumpOnOutOfMemoryError   # leave evidence for later analysis
-XX:HeapDumpPath=/dumps
-XX:+UseZGC                       # low-pause collector (generational by default on recent JDKs)
-Xlog:gc*:file=gc.log             # GC log, cheap and extremely useful
  • Container memory. The JVM sees the container limit and sizes the heap from it, but the heap is only part of the process: metaspace, thread stacks, direct buffers and the JIT also use memory. Setting -Xmx equal to the container limit gets the process killed by the kernel (OOMKilled). Leave headroom: 70 to 75 percent for the heap is a common start.
  • Garbage collector. G1 is the default and is a fine all-rounder. Try ZGC when you need very short pauses on big heaps, and measure; a flag copied from a blog post is not a tuning strategy.
  • Do not set what you cannot justify. Old advice (-XX:+UseParallelGC everywhere, tiny -Xss) can make modern JVMs slower. Change one flag at a time and compare p99 latency, not averages.

Profiling: look, do not guess

When the metrics say "this endpoint is slow" but not why, use a profiler.

  • Java Flight Recorder (JFR) is built into the JVM, has low overhead and is safe in production: java -XX:StartFlightRecording=duration=60s,filename=app.jfr -jar app.jar, or jcmd <pid> JFR.start. Open the file in JDK Mission Control or IntelliJ.
  • async-profiler produces flame graphs: wide blocks are where the CPU (or time) goes. A flame graph answers "which method?" in seconds.
  • jcmd <pid> Thread.print (or a thread dump from Actuator's threaddump endpoint) shows what every thread is doing. Take three dumps a few seconds apart: if most request threads sit in the same database or lock call, you found the bottleneck.
  • Hibernate statistics (spring.jpa.properties.hibernate.generate_statistics=true) and SQL logging in a test reveal N+1 queries (Entity Relationships and the N+1 Problem). A single endpoint issuing 300 queries is the most common big win in Boot applications.
  • Database tools: EXPLAIN on the slow query, the slow-query log, and missing-index reports often beat any JVM tuning.

Responses: compression and JSON cost

Bytes cost time, especially for mobile users. Two things to know.

Compression

PROPERTIES
server.compression.enabled=true
server.compression.mime-types=application/json,text/html,text/css,application/javascript
server.compression.min-response-size=2KB

Compression trades CPU for bandwidth; JSON and HTML typically shrink by 70 to 90 percent. Many teams let the reverse proxy or CDN (nginx, Cloudflare) compress instead, so the application stays lean; do it in one place, not both. Do not compress already compressed content such as images or archives.

JSON cost

Serialization is rarely the first bottleneck, but sending too much data is. Common fixes, in order of effect:

  1. Paginate. Never return an unbounded list (Designing a Clean API).
  2. Return DTOs with only the needed fields, not whole entities (this also prevents lazy-loading surprises and leaking columns).
  3. Omit nulls and defaults (spring.jackson.default-property-inclusion=non_null), which shrinks payloads of sparse objects.
  4. Stream large exports (write rows as you read them) rather than building a huge list in memory.
  5. Cache responses that do not change often, with Cache-Control and ETags (Caching).

The first TryIt measures what these choices do to payload size using real Jackson and real gzip. Sizes are exact and the same on every run, which is why this one makes a better lesson than timings.

Java Payload size: pagination, nulls and gzip

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.

Look at how the levers stack: omitting nulls helps, gzip helps a lot because JSON is repetitive, and a single page beats all of them because it avoids work on both sides: less to read from the database, to serialize, to send and for the client to parse.

Micro-benchmarks lie unless you are careful

It is tempting to settle a performance argument by timing two snippets in a loop. On the JVM that is full of traps. Code starts in the interpreter, hot methods are compiled by the JIT after thousands of calls (so the first runs are slow and later ones fast: warmup), the compiler may delete work whose result is unused (dead-code elimination), and the garbage collector pauses at random moments. A naive timing often measures the JIT, not your code.

For a decision that matters, use JMH (the Java Microbenchmark Harness), which handles warmup, forks fresh JVMs and prevents dead-code elimination. For a quick sanity check, follow the rules in the TryIt: warm up first, repeat several rounds, use the result (here, it is returned and summed), and compare differences that are large (an order of magnitude), not 5%.

The second TryIt compares building a string by repeated += with a StringBuilder. The two are not close, so the conclusion is robust. The per-round times are printed for you to see warmup; they depend on your machine, so do not expect these numbers to match anyone else's. Only the comparison lines are stable.

Java A careful (enough) micro-benchmark

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.

This is real Java, but a toy benchmark. Treat it as a demonstration of the method, not as measurement of your service: real services need a load test.

A tuning checklist

  1. Define the goal and measure the baseline with realistic data.
  2. Check the database first: indexes, N+1 queries, transaction scope, pool usage.
  3. Check calls to other services: timeouts, caching, parallelism where safe.
  4. Check payload size: pagination, DTOs, compression.
  5. Size pools and threads from measurements, not from guesses; consider virtual threads for I/O-heavy workloads.
  6. Tune the JVM last, with few flags, and keep GC logs.
  7. Re-measure, keep the change only if it helped, and write down why.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Your service has 200 request threads and a database pool of 10, and latency spikes under load. What is the most useful first check?

    With far more threads than connections, requests queue for a connection. Check pool metrics, then shorten query and transaction time before enlarging the pool.
  2. 2. What does spring.threads.virtual.enabled=true mainly improve?

    Virtual threads make waiting cheap, which helps services that mostly wait on databases and other services. They do not make CPU work faster.
  3. 3. What is the main risk of spring.main.lazy-initialization=true in production?

    Lazy initialization moves cost and failures from startup to runtime, where users meet them.
  4. 4. Why is a naive timing loop an unreliable benchmark on the JVM?

    Code runs slowly until the JIT compiles it, and unused results may be optimized away. Use JMH for decisions that matter.
  5. 5. Which change usually reduces response time and payload the most for a list endpoint returning thousands of rows?

    Pagination reduces work at every step, from the query to serialization to the network and the client.

Summary

  • Follow the loop: measure with realistic load, find the bottleneck, change one thing, measure again.
  • The database and slow calls to other services dominate most Boot applications; check pools (hikaricp.connections.pending), N+1 queries and transaction scope first.
  • Size pools from measurements; bigger is often slower. Virtual threads raise I/O concurrency, but not single-request speed.
  • Startup improves by starting less, deferring work, CDS/AOT caches or native images; lazy initialization shifts cost and errors to runtime.
  • Use few, justified JVM flags (heap as a percentage of container memory, OOM handling, GC logs) and profile with JFR, flame graphs and thread dumps.
  • Cut JSON cost with pagination, DTOs, omitted nulls and compression in one place; use JMH, not casual loops, for micro-benchmarks.

Next: Building and Running the Jar.