Java and Spring Level 3 This level · 42 of 56
Hard java spring

What are the most common Spring Boot performance mistakes?

The 30 second answer

I'd first look at database access and resource limits rather than blame Spring Boot itself. Common problems are N+1 queries, unintended database access through Open Session in View, badly sized connection pools, missing HTTP timeouts and loading unbounded result sets. Blocking reactive threads or other limited executors can also stall requests. Excessive logging and unnecessary starters add overhead too. I'd use Actuator metrics, traces and profiling to find the bottleneck, then change one thing and measure again.

Could you say this in an interview?

1. N+1 queries. A loop over 100 orders that touches order.getItems() runs 101 queries. Fix with fetch joins, @EntityGraph, batch fetching (hibernate.default_batch_fetch_size), or DTO projections. Detect it by turning on Hibernate statistics or counting queries in tests.

2. Open Session in View (OSIV). Spring Boot registers OpenEntityManagerInViewInterceptor by default for web apps and logs a warning at startup. It keeps a database connection for the whole request, including view rendering and slow outbound calls, and hides lazy-loading N+1 issues. Set spring.jpa.open-in-view=false and load what you need in the service layer.

3. Connection pool sizing. A pool that is too small makes threads wait (hikaricp.connections.pending grows); one that is too large overloads the database. Watch hikaricp.connections.usage and .pending metrics. HikariCP's own guidance is that a small pool often performs better than a large one.

4. No timeouts. An outbound HTTP call without connect and read timeouts can hold a request thread forever when a dependency is slow, and then the whole service stalls. Set timeouts on RestClient/WebClient (Spring Boot has HTTP client timeout properties, such as connect and read timeouts, under spring.http.client) and add a circuit breaker for critical dependencies.

5. Blocking in the wrong place. Blocking JDBC calls inside WebFlux event-loop threads freeze the server. In Spring MVC, a few slow endpoints can use up the Tomcat thread pool (server.tomcat.threads.max, default 200). Virtual threads (spring.threads.virtual.enabled=true, Java 21+) help with blocking I/O but not with pool limits on the database.

6. Too much data. findAll() on a large table, no pagination, large JSON responses. Use Pageable, projections, and response compression (server.compression.enabled=true).

7. Logging. DEBUG level or spring.jpa.show-sql=true in production costs CPU and I/O. Use async appenders for heavy logging.

8. Startup cost. Unused starters add auto-configurations and classpath scanning. Check the condition report; consider lazy initialization (spring.main.lazy-initialization=true) for dev, and CDS or AOT for production startup time.

9. Caching without thinking. @Cacheable on a self-called method does nothing (proxy bypass), and an unbounded cache causes memory problems. Use a bounded cache like Caffeine.

10. Tuning without data. Use Actuator metrics (http.server.requests with percentiles), tracing, a profiler, and load tests. Fix the biggest measured bottleneck first.

Editor's note: this list is practical experience, not a single official list. In an interview, pick two or three, explain how you detected them (metrics, traces, query logs) and what changed after the fix.

Alternate interview scenarios

"In a code review, how do you spot performance bottlenecks before they reach production?"

It is the same list, but read from the diff instead of from metrics. I look for queries inside loops or lazy collections touched in a loop, because that is N+1. I look for repository calls with no paging, and for new outbound calls without a timeout or inside a @Transactional method. I also check for blocking calls on reactive or limited executors, and for caches with no size limit or a key that changes on every request. I ask for evidence where the risk is real, such as a test that counts queries, or an EXPLAIN for a new query on a large table. I don't argue about micro-optimisations a profiler has not justified.

Sources (5)
  1. Spring Boot reference: Open EntityManager in View (spring.jpa.open-in-view)docs.spring.io
  2. Spring Boot reference: Supported connection poolsdocs.spring.io
  3. HikariCP wiki: About pool sizinggithub.com
  4. Hibernate ORM user guide: Fetching and batch fetchingdocs.jboss.org
  5. Spring Boot reference: Metrics, Spring MVCdocs.spring.io
Previous
Next