CodexBloom - Programming Q&A Platform

High Latency in Spring Boot Application with Hibernate and PostgreSQL under Load

šŸ‘€ Views: 784 šŸ’¬ Answers: 1 šŸ“… Created: 2025-06-30
spring-boot hibernate postgresql performance Java

I'm prototyping a solution and I've been banging my head against this for hours... Can someone help me understand I'm experiencing significant latency in my Spring Boot application when it handles a high number of concurrent requests. The application uses Hibernate for ORM and PostgreSQL as the database. When I simulate heavy load using Apache JMeter, the response times increase dramatically, especially for endpoints that require complex joins across multiple tables. For example, I have a query that retrieves user data along with their associated orders like this: ```java @Query("SELECT u FROM User u JOIN FETCH u.orders o WHERE u.status = :status") List<User> findUsersByStatus(@Param("status") String status); ``` With around 100 concurrent requests, I notice response times climbing above 5 seconds, and logs indicate that the database query itself is taking a long time to execute. I've already tried several optimization strategies, including: - Adding proper indexes to the tables involved in the query. I have indexes on `user_id` and `status`, but it doesn't seem to help much. - Configuring Hibernate to use a batch size in my entity mappings: `@BatchSize(size = 50)`. - Tuning PostgreSQL settings like `work_mem` to accommodate larger queries. However, I still see warnings in the logs about `slow query time` and the database server occasionally shows high CPU usage during these tests. I’m using Spring Boot version 2.6.4 and Hibernate 5.6.3, with PostgreSQL 13.3. Any suggestions on further improving performance or diagnosing bottlenecks would be greatly appreciated. This is part of a larger mobile app I'm building. I'm working on a CLI tool that needs to handle this. Any feedback is welcome! The stack includes Java and several other technologies. Am I missing something obvious?