r/SpringBoot • u/Sea-Lettuce-255 • Aug 24 '26
How-To/Tutorial How I eliminated N+1 queries and scaled throughput by 20% on a production E-Commerce app
Just wanted to share a recent architecture win that might help someone dealing with latency spikes.
I was dealing with a severe bottleneck in a production E-Commerce platform. During peak traffic, the database was choking, and MTTR was creeping up.
The Culprit: Classic Hibernate N+1 query problems hidden deep inside the inventory/catalog mapping, combined with un-indexed foreign key lookups.
The Fix:
1. Ripped out the lazy-loading proxy faults and replaced them with explicit JOIN FETCH queries for the critical read paths.
2. Layered in Redis via Spring Cache (@Cacheable) specifically targeting the high-read/low-write catalog endpoints.
3. Configured request batching and API rate limiting on the gateway layer to prevent thundering herd problems during flash sales.
Result: We dropped sub-100ms latency across the board and prevented database lockups entirely.
If you're building in Spring Boot and relying entirely on default JPA repositories—run a SQL profiler right now. You probably have an N+1 hiding somewhere.
10
u/kowlown Aug 24 '26
For the point 1 there are many solutions good, or worse depending on your case and constraints. You could also use :
N+1 select is the number one case of bad performance imo.
5
u/Intrepid_Fox5414 Aug 24 '26
Since I do not use Hibernate by doing myself my PreparedStatment and mappings in my ResultSet, I feel way more better and confortable
8
u/StochasticTinkr Aug 24 '26
It's almost like using ORMs for production systems is a bad thing.
Oh, that's right, it is!
3
u/dallastelugu Aug 25 '26
oh man i just studied entitygraph today trying to understand what is n+1 query problem
2
u/rlrutherford Senior Dev Aug 25 '26
N+1 is an ORM issue on the level of not hitting indexes for your queries.
1
u/RedditForW Aug 24 '26
Following