Stop celebrating “working code” that secretly makes 18 DB calls per request. Yes, it works. No, it won’t survive production scale. I keep seeing this pattern in backend systems—especially in Spring Boot + ORM-heavy apps: 👉 Loop over data 👉 Trigger queries inside each iteration 👉 Ship it because “it passes tests” Congrats. You just built a silent performance killer (N+1 problem). ⸻ 💥 What’s really happening? • Every component fires its own query • Network round trips explode • DB gets hammered under load • Latency spikes… and nobody knows why ⸻ 🚀 How real engineers fix it 1. Kill the N+1 problem at the root • Stop DB calls inside loops • Identify where queries multiply 2. Use JOINs like your system depends on it (because it does) • Let the database handle relationships • One optimized query > 18 scattered ones 3. Batch your queries • Replace repeated calls with IN clauses • Fetch data in chunks, not one-by-one 4. Push computation to the database • Use GROUP BY, COUNT, SUM • Avoid unnecessary processing in your app layer 5. Control ORM behavior • Lazy loading inside loops = disaster • Use eager fetching strategically, not blindly 6. Cache hot data • Frequently accessed data should NOT hit DB every time • Use Redis / in-memory caching smartly ⸻ 🔥 Production mindset shift This isn’t “optimization.” This is system design discipline. Because at scale: Your biggest bottleneck isn’t CPU… it’s how many times you talk to your database. ⸻ “Reduce multiple DB queries by eliminating N+1 using joins, batch fetching, aggregation, and caching—designing a single optimized query for production systems.” ⸻ If your API is making double-digit DB calls per request… you don’t have a backend problem. You have a design problem. ⸻ #Java #SpringBoot #SystemDesign #BackendEngineering #Scalability #PerformanceEngineering #MicroservicesArchitecture #SoftwareArchitecture #LowLatency #HighAvailability #DistributedSystems #APIPerformance #DatabaseOptimization #ORM #Hibernate #Caching #Redis #SRE #TechLeadership
Fixing the N+1 Problem in Spring Boot Apps
More Relevant Posts
-
We once deleted a user in production and watched half our database disappear in real time. 😅 Here's what happened: We were making live changes to our PostgreSQL database. One table was foreign-key linked to another. The moment we deleted a user record — CASCADE did exactly what it was told. All related data across linked tables: gone. Instantly. Panic mode. Everyone on a call. Hands shaking on keyboards. Then someone remembered — we had a background job quietly copying data into a separate, unlinked table. No one thought much of it at the time. That day, it saved us completely. We recovered everything. The users never knew. But that incident rewired how I think about backend engineering forever. 🧠 Here's what actually separates senior Node.js + Express engineers from the rest: 1. They don't just fix the bug — they ask why the system allowed the bug to exist in the first place. 2. They never make raw changes on a live database without a rollback plan. Never. 3. They design for failure first. Backups, redundancy, audit logs — before the happy path. 4. They understand the cost of abstractions. That ORM is hiding a CASCADE DELETE. They know it. 5. They treat Redis as a layer of safety too — cache the critical stuff, know what's ephemeral and what isn't. 6. They write code for the person debugging at 3am — who is probably future them. 7. They push back on "let's just do it quickly in prod." That's not being difficult. That's seniority. The real senior shift isn't knowing more frameworks. It's knowing that one overlooked FK constraint can ruin your entire afternoon — and building systems that survive human mistakes anyway. Senior engineers — what's your "we almost lost everything" story? Let's normalize talking about it. 👇 #NodeJS #ExpressJS #PostgreSQL #Redis #BackendDevelopment #JavaScript #DatabaseManagement #SystemDesign #ProductionIncident #DisasterRecovery #SoftwareArchitecture #SoftwareEngineering #SeniorDeveloper #BackendEngineer #TechLeadership #Programming #CodingLife #Developer #TechCommunity #LessonsLearned #100DaysOfCode
To view or add a comment, sign in
-
🚀Unlocking the Power of APIs in Spring Boot: REST vs. GraphQL vs. Reactive When we talk about building APIs with #SpringBoot, there isn’t a one-size-fits-all answer. Depending on your system’s architecture, data needs, and performance requirements, you have powerful options. I’ve put together a visualization (attached below) breaking down the three major API paradigms we work with most often in the Spring ecosystem. Here’s a quick overview: 1️⃣ REST APIs (REpresentational State Transfer) The standard for years. It’s stateless, resource-oriented, and uses HTTP verbs (GET, POST, etc.) for communication. Key Annotations: @RestController, @GetMapping, @PostMapping Use Case: When you need simplicity, caching, or standard protocol adherence (like microservices communication). 2️⃣ GraphQL A query language for APIs. It lets the client define exactly what data they need, avoiding over-fetching or under-fetching. It typically operates through a single endpoint. Key Annotations: @SchemaMapping, @QueryMapping Use Case: Ideal for front-end heavy apps, complex data relationships, and mobile clients with bandwidth constraints. 3️⃣ Reactive APIs (Spring WebFlux) Built for non-blocking, asynchronous communication. It operates on a smaller number of threads to handle a massive number of concurrent requests. Key Types: Mono<T> (0-1 result), Flux<T> (0-N results) Use Case: High-concurrency systems, streaming applications, and IO-bound tasks where thread efficiency is crucial. Which approach are you using for your current projects, and what made you choose it? Let’s discuss in the comments! 👇 #java #springboot #api #restapi #graphql #webflux #microservices #backend #softwareengineering #learncoding #linkedinlearning
To view or add a comment, sign in
-
-
Spring Boot Architecture — Explained Simply 🌿 Here’s a layered breakdown of the complete architecture 👇 🔵 Client Layer Handles incoming requests from: • Browser (UI) • REST Clients (Postman, APIs) • Mobile Apps • WebSockets 🟠 API Gateway Layer Acts as the entry point for microservices: • Routing (Spring Cloud Gateway) • Load Balancing • Circuit Breaker • Service Discovery 🔴 Security Layer Ensures protection & access control: • Spring Security • OAuth2 / JWT Authentication • Role-Based Access Control (RBAC) 🟢 Presentation Layer Handles request/response flow: • @RestController • DTOs & Validation • Swagger / API Docs 🟣 Service / Business Layer Core logic lives here: • @Service • @Transactional • Caching • Event-driven communication 🟡 Data Access Layer Interacts with the database: • Spring Data JPA • Flyway (DB Migration) • R2DBC (Reactive DB) • HikariCP (Connection Pooling) 🟣Persistence Layer Actual data storage systems: • PostgreSQL • MongoDB • Redis (Caching) • Elasticsearch (Search Engine) 💡 Flow in One Line: Client → Gateway → Security → Controller → Service → Repository → Database 🔥 Mastering this flow = cracking Spring Boot interviews + building scalable systems #SpringBoot #Java #Microservices #BackendDevelopment #SystemDesign #SoftwareEngineering #TechLearning
To view or add a comment, sign in
-
-
Stop exposing your Database Entities! 🛑 Why the DTO Pattern is non-negotiable for Spring Boot developers. Most developers learn this the hard way. Your database schema and your API contract are two completely different things. If you're returning @Document or @Entity classes directly from your REST controllers, you're opening a Pandora's box of security leaks, tight coupling, and maintenance pain. The fix is the DTO (Data Transfer Object) Pattern. 🛡️ Think of a DTO as a bouncer: it decides exactly what data gets in — and what gets out. 🚀 4 reasons you should care: 1️⃣ Security: Your User entity holds a passwordHash and sensitive internal fields. A DTO ensures you never accidentally expose them. 2️⃣ Decoupling: Renamed a MongoDB field? Update the mapper — your API contract stays untouched. No breaking changes for your clients. 3️⃣ Validation: Use @Email, @NotBlank, @Size on your DTOs to stop bad data before it ever reaches your service layer. 4️⃣ Performance: Stop serializing 50-field documents when the client only needs 3. Shape your payload. Ship less data. 🔄 Map with MapStruct — compile-time, type-safe, zero reflection and is widely used. The DTO pattern isn't overhead. It's discipline — and in production, it pays for itself fast. Still returning Entities directly? Or already using DTOs? Tell me in the comments 👇 #Java #SpringBoot #MongoDB #BackendDevelopment #SoftwareArchitecture #DTO #CleanCode #APIDesign #CodingTips
To view or add a comment, sign in
-
-
Just wrapped up a massive architectural sprint for my latest platform, CampusKey! 🚀 I spent the day architecting and hardening the backend infrastructure. My goal wasn't just to make it work, but to ensure it can handle high-traffic scale efficiently without bottlenecking the database. Here is a breakdown of the production-grade systems I integrated today: 🔐 Zero-Trust Security & Identity Access Engineered a completely stateless authentication flow using JSON Web Tokens (JWT). Implemented strict Role-Based Access Control (RBAC) to isolate Student, Landlord, and Admin privileges at the routing level. Secured all user credentials with BCrypt password hashing and integrated a secure, tokenized password reset flow via JavaMailSender. 🌍 Geospatial Engineering Built a location-aware search engine capable of finding properties within a specific radius of a user. Instead of relying on basic filters, I implemented the Haversine formula directly into native SQL queries via Spring Data JPA to calculate exact distances on the Earth's sphere, returning precisely paginated results to keep payload sizes lean. ⚡ High-Performance In-Memory Caching To prevent the Haversine mathematical queries from crushing the database under heavy concurrent user load, I containerized a Redis instance via Docker. Integrated Spring Cache to store those heavy coordinate calculations in memory, dropping response times from standard database latency down to milliseconds. 🗄️ Resilient Data Layer Migrated the core data layer to a containerized MySQL 8.0 instance. Configured HikariCP for optimal connection pooling to manage concurrent database requests efficiently. Automated all database schema changes and version control using Flyway, ensuring strict, reproducible migration environments. Building software is great, but building scalable, secure, and geographically aware architecture is where the real fun begins. Looking forward to wiring this up to the frontend next! (*this text is Ai generated*-Told it to explain what I wanted!) #BackendEngineering #SpringBoot #Java #SoftwareArchitecture #SystemDesign #Redis #Docker #Microservices #WebDevelopment
To view or add a comment, sign in
-
-
🚀 Reduced API Latency by ~40% — Here’s What Actually Works While going through performance optimization techniques for Spring Boot APIs, I came across a really practical PDF that shows how API latency was reduced from 800ms → 480ms using real-world backend strategies. Thought this was worth sharing 👇 📘 What this guide covers: ⚡ 1. Query Optimization (Biggest Bottleneck) • Fixed N+1 issues using proper joins (JOIN FETCH) • Added indexes → ~60% faster lookups • Selected only required fields instead of full entities • Query time improved. 🧠 2. Redis Caching • Cache-aside pattern (DB hit only on cache miss) • TTL + cache invalidation strategy • Cache warming for hot data • Result → ~70% fewer DB calls 🔌 3. Connection Pooling (HikariCP) • Reused DB connections instead of creating new ones • Tuned pool size & timeouts • Result → ~25% faster DB operations 📄 4. Smart Pagination • Avoid fetching massive datasets • Reduced response size from 500KB → 15KB (~97% less) • Used Spring Pageable for clean implementation ⚙️ 5. Async Processing (@Async) • Offloaded heavy tasks (emails, PDFs, external APIs) • Faster user response → backend continues work in background 📊 6. Monitoring & Observability • Logging + Actuator + slow query tracking • Faster debugging and performance insights 💡 Final Outcome: All combined → ~40% faster APIs in a real-world setup 📎 I’m sharing this PDF in the post for anyone building high-performance backend systems. If you’re working with Java, Spring Boot, Microservices, or System Design, these are the kind of optimizations that actually matter in production. #SpringBoot #Java #BackendDevelopment #SystemDesign #Microservices #Performance #Redis #APIDesign #SoftwareEngineering
To view or add a comment, sign in
-
𝗪𝗵𝗮𝘁 𝗵𝗮𝗽𝗽𝗲𝗻𝘀 𝘄𝗵𝗲𝗻 𝘆𝗼𝘂 𝗰𝗼𝗺𝗯𝗶𝗻𝗲 𝗥𝗲𝗱𝗶𝘀 𝗰𝗮𝗰𝗵𝗶𝗻𝗴, 𝗔𝗜, 𝗮𝗻𝗱 𝗮𝘂𝘁𝗼𝗺𝗮𝘁𝗲𝗱 𝗯𝗮𝗰𝗸𝗴𝗿𝗼𝘂𝗻𝗱 𝘀𝗰𝗵𝗲𝗱𝘂𝗹𝗶𝗻𝗴 𝗶𝗻𝘁𝗼 𝗮 𝘀𝗶𝗻𝗴𝗹𝗲 𝗦𝗽𝗿𝗶𝗻𝗴 𝗕𝗼𝗼𝘁 𝗮𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻? 𝗬𝗼𝘂 𝗴𝗲𝘁 𝗮 𝘀𝘆𝘀𝘁𝗲𝗺 𝘁𝗵𝗮𝘁 𝗮𝗰𝘁𝘂𝗮𝗹𝗹𝘆 𝘄𝗼𝗿𝗸𝘀 𝗳𝗼𝗿 𝘁𝗵𝗲 𝘂𝘀𝗲𝗿. 💡 I’m thrilled to share my latest architectural deep-dive: 𝐖𝐞𝐚𝐭𝐡𝐞𝐫𝐖𝐢𝐬𝐞! 🌤️ 🛑 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Relying on constant external API calls for weather data is slow and inefficient. Furthermore, raw temperature data doesn't tell a user how to plan their day, and users shouldn't have to manually open an app every morning to find out. 💡 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: I engineered an automated, AI-powered backend system that fetches, caches, analyzes, and delivers personalized daily weather briefings directly to users. 𝐖𝐡𝐚𝐭 𝐈 𝐋𝐞𝐚𝐫𝐧𝐞𝐝 & 𝐈𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭𝐞𝐝 (𝐀 𝐥𝐨𝐭 𝐨𝐟 "𝐅𝐢𝐫𝐬𝐭𝐬" 𝐟𝐨𝐫 𝐦𝐞!): Stepping up the complexity from my previous projects, I integrated several powerful tools into the Java Spring Boot ecosystem for the very first time: ⚡ 𝙍𝙚𝙙𝙞𝙨 𝘾𝙖𝙘𝙝𝙞𝙣𝙜: Implemented Redis to cache OpenWeather API responses. This drastically reduced latency and prevented unnecessary, repetitive external API calls. ⏱️ 𝘾𝙧𝙤𝙣 𝙅𝙤𝙗𝙨 (𝙎𝙥𝙧𝙞𝙣𝙜 𝙎𝙘𝙝𝙚𝙙𝙪𝙡𝙞𝙣𝙜): Configured automated background tasks to process user preferences and trigger data pipelines at specific times. 🧠 𝙎𝙥𝙧𝙞𝙣𝙜 𝘼𝙄: Integrated AI models to ingest raw weather metrics and generate natural, actionable insights (e.g., translating "80% humidity and rain" into "Pack an umbrella and expect heavier traffic today"). 📧 𝙅𝙖𝙫𝙖 𝙈𝙖𝙞𝙡 𝙎𝙚𝙣𝙙𝙚𝙧: Successfully orchestrated the asynchronous delivery of these AI-generated daily briefings straight to the users' inboxes. 𝗞𝗲𝘆 𝘁𝗮𝗸𝗲𝗮𝘄𝗮𝘆: Tying together background scheduling, caching layers, AI integration, and SMTP services into one seamless architecture taught me more about system design than any tutorial could. Getting all these independent moving parts to sync up flawlessly was a massive, but incredibly rewarding, challenge. 𝗚𝗶𝘁𝗵𝘂𝗯 𝗟𝗶𝗻𝗸 - https://lnkd.in/gQcpBURT #Java #SpringBoot #Redis #SpringAI #BackendEngineering #SystemDesign #SoftwareArchitecture #LearningInPublic #DeveloperJourney
To view or add a comment, sign in
-
Our API was slow, so we almost scaled everything. We had a simple GET API in a Spring Boot service handling just ~50 requests per second: Fetch orders for a certain user. Order → name OrderDetails → price, offers, discount Only ~100K records in Postgres DB and still the API took seconds. Grafana showed high latency. We were scratching our heads 😕 Everything seemed right: One-to-many relationships Clean design Code review approved Production: nice try 😏 So we did what engineers do: Thought about scaling Considered adding cache Increased DB connections and prayed 😌 But then we decided to dig deeper. And we found out where the problem really lied: 👉 N+1 queries 1 query → fetch orders N queries → fetch order details Our API kept querying the database like there was some kind of vendetta against it. And here’s how we solved it: 👉 @EntityGraph We told JPA upfront: 👉 Fetch both Order and OrderDetails in one join query The results: ❌ Hundreds of queries ✅ 1 clean JOIN query Now our API works fast. Grafana was not mad anymore. Database stopped hating us. Lesson: We didn't have a scaling problem. We had a query problem. 🔑 Takeaways: 👉 Use EntityGraph / NamedEntityGraph 👉 Use JOIN FETCH when needed 👉 Don’t trust default ORM behavior 💬 Do you have stories when you've chased the scaling issue, only to find out that it was a query problem instead? #java #springboot #hibernate #backendengineering #systemdesign #performanceoptimization #database #devlife #techhumor #engineeringhumor
To view or add a comment, sign in
-
-
Codebase three of three. The most complex one. My assignment was simple — investigate why the homepage was loading slowly. The first API call the frontend made on load was a GraphQL query for notifications. Next.js was waiting for that response before rendering anything. So I followed it. The code suggested the cache might be oversized. I disabled it to see what was underneath. What I found was a notifications table with 100 million timestamped rows being scanned in full to find notifications from the last 10 minutes. Every single time the homepage loaded. I enabled the cache again. The cache was broken. Each notification stored as a separate Redis key. A second key tracking all the other keys. To read anything you iterated through the index, then fetched each item individually. No batching. No sensible expiry. Just iteration, at scale, on every homepage load. Git blame told me this had been written by someone with five years on the same codebase. No seed data for local testing. No documentation. You had to invent scenarios by hand just to reproduce what production was doing every second. Ten nested resolvers, some calling parent or grandparent resolvers for no reason. Unnecessary list comprehensions adding queries across every query and mutation in the application. 47,000 queries → 32. I never found out whose decision it was to serve the same data simultaneously as GraphQL, REST, and XML to the same frontend. I was let go before I could ask enough questions. Apparently fixing things nobody asked you to fix is a problem in some organizations. Three codebases. Three different stacks. One pattern. Code that nobody owns drifts — until someone like me walks in. #backendengineering #graphql #django #python #softwaredevelopment #redis #caching #systemdesign #softwarearchitecture #engineeringculture
To view or add a comment, sign in
-
I just published a deep‑dive on one of the most confusing issues in backend development: Why “connection timeout” doesn’t always mean the same thing. In this article, I break down the real differences between: 🔹 Database Connection Timeouts 🔹 HTTP Connection Timeouts Both look similar in logs—but come from completely different layers of your system. If you’ve ever chased the wrong root cause, this one’s for you. #BackendDevelopment #SoftwareEngineering #Microservices #Java #SpringBoot #Performance #ConnectionTimeOut
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Insightful post, Thanks Damodar !!