Java Performance Tuning

A measurement-first method for tuning Java services: load testing, profiling, database and connection pools, caching, serialization and JVM settings, with a checklist.

Method

  1. Define the target: throughput and latency percentiles at a given load, resource budget.
  2. Reproduce with a realistic load test (k6, Gatling) and production-like data volumes.
  3. Observe: traces to find slow hops; JFR/async-profiler for CPU, allocation and lock profiles; database query stats.
  4. Fix the biggest bottleneck first; re-measure.
  5. Guard: add performance tests to CI for critical endpoints.

Where time usually goes

Area Typical issue Fix
Database N+1 queries, missing indexes, large result sets, lock contention Fetch joins/batch fetching, indexes from EXPLAIN ANALYZE, pagination, shorter transactions
Connection pools Pool too small (waiting) or too large (DB overload) Size from Little's Law; HikariCP defaults are good; monitor wait time
Remote calls Sequential calls, no timeouts, chatty APIs Parallelize, batch, cache, set timeouts
Serialization Reflection-heavy JSON on large payloads Smaller payloads, streaming, Jackson afterburner/blackbird or binary formats
Logging Synchronous logging, string building at DEBUG Async appenders, parameterized logging, sampling
Allocation Excessive temporary objects Profile allocations; reuse buffers; avoid boxing in hot paths
GC Wrong collector or heap size See JVM and GC

HikariCP sizing example

spring:
  datasource:
    hikari:
      maximum-pool-size: 20        # per instance; total = instances × 20 must fit DB max_connections
      minimum-idle: 5
      connection-timeout: 2000     # fail fast instead of piling up requests
      max-lifetime: 1800000

With 30 instances × 20 connections = 600 connections: check the database can handle it, or add a connection pooler (PgBouncer/RDS Proxy).

Startup and memory for cloud-native Java

  • AppCDS / AOT cache (Java 24/25) and Spring Boot's CDS support reduce startup time.
  • GraalVM native images: very fast startup and low memory, at the cost of build time and reflection configuration; good for serverless and CLIs.
  • CRaC (Coordinated Restore at Checkpoint) and AWS Lambda SnapStart restore initialized JVMs.

Checklist

  • Timeouts on every external call.
  • No N+1 queries on hot endpoints (verify with SQL logs or Hibernate statistics).
  • Indexes for all frequent query predicates.
  • Response compression and pagination.
  • Caching with measured hit rates.
  • Bounded queues and pools; metrics on saturation.
  • GC and heap settings reviewed under load.

Interview questions and answers

An endpoint takes 2 seconds. How do you find out why?

Look at a distributed trace to see whether time is in the service, the database or a downstream call. If in the service, profile with JFR/async-profiler under load. If in the database, check the query plan, row counts and locks. Fix, then load test to confirm.

How do you size a thread or connection pool?

Concurrency ≈ throughput × latency (Little's Law), bounded by downstream capacity. For CPU-bound pools, roughly the number of cores; for I/O, the downstream limits (DB connections) decide.