N+1 Performance
Detecting N+1
-- Enable SQL logging
spring.jpa.show-sql=true
-- Count queries per request
-- If 1 request triggers 100+ queries → N+1
Impact
| Queries | Response Time |
|---|---|
| 1 (JOIN FETCH) | 50ms |
| 101 (N+1) | 5000ms |
Key Points
- Understanding N+1 Performance is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Performance Optimization
Areas
- Database: Indexes, queries, connection pooling
- Caching: Multi-level, appropriate TTL
- Network: Compression, CDN, HTTP/2
- Code: Profiling, async, batch
Measurement
- Load testing
- Profiling
- APM tools
- Real user monitoring
Best Practices
- Set performance budgets
- Monitor in production
- Optimize hot paths
- Use appropriate data structures
Key Points
- Understanding N+1 Performance is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for N+1 Performance in a backend system. Consider scalability, error handling, and production readiness.
Solution
// N+1 Performance implementation
// Key aspects: validation, error handling, logging, testing
public class N1Performance {
// Production-ready implementation
}Identify and handle edge cases for N+1 Performance. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for N+1 Performance. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. How to detect N+1 in production?
2. N+1 fix reduces queries from?
3. What is the primary purpose of N+1 Performance?
4. What is a common mistake when implementing N+1 Performance?
Flashcards
Question
N+1 detection?
Click to reveal answer
Answer
Count queries per request
Question
N+1 fix reduces queries to?
Click to reveal answer
Answer
1 (single JOIN FETCH)
Question
What is N+1 Performance?
Click to reveal answer
Answer
N+1 Performance is a key concept in backend development.
Question
When to use N+1 Performance?
Click to reveal answer
Answer
Use N+1 Performance when building production systems that require reliability, scalability, and maintainability.
Question
N+1 Performance best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.N+1: 1 query for list + N queries for each item
- 2.Fix: JOIN FETCH, EntityGraph
- 3.Monitor query count per request
- 4.Huge performance impact
Interview Tips
- •Detect N+1 in production
- •Fix efficiently
Cheat Sheet
N+1 Performance
- Detect: count queries per request
- Fix: JOIN FETCH (1 query)
- Alt: EntityGraph, @BatchSize
- Impact: 100+ queries → 1 query