0
0
Spring Bootframework~8 mins

Fetch types (LAZY vs EAGER) in Spring Boot - Performance Comparison

Choose your learning style9 modes available
Performance: Fetch types (LAZY vs EAGER)
HIGH IMPACT
This affects how and when related data is loaded from the database, impacting page load speed and responsiveness.
Loading related entities in a Spring Boot application
Spring Boot
class User {
  @OneToMany(fetch = FetchType.LAZY)
  Set<Order> orders;
}

// Orders load only when accessed explicitly
LAZY fetch delays loading related data until accessed, reducing initial query size and speeding up page load.
📈 Performance GainReduces initial DB load and memory use; improves LCP by loading only needed data.
Loading related entities in a Spring Boot application
Spring Boot
class User {
  @OneToMany(fetch = FetchType.EAGER)
  Set<Order> orders;
}

// Fetching User loads all Orders immediately
EAGER fetch loads all related entities immediately, even if not needed, causing slower initial queries and higher memory use.
📉 Performance CostBlocks rendering longer due to heavy DB queries; increases LCP by loading unnecessary data upfront.
Performance Comparison
PatternDB QueriesMemory UsageInitial Load TimeVerdict
EAGER FetchLoads all related data immediatelyHigh due to all data in memorySlower initial load[X] Bad
LAZY FetchLoads related data on demandLower memory footprintFaster initial load[OK] Good
Rendering Pipeline
Fetch type choice affects when data is retrieved from the database, influencing the backend response time and how fast the frontend can render content.
Data Fetching
Backend Processing
Frontend Rendering
⚠️ BottleneckData Fetching stage due to large or unnecessary queries with EAGER fetch.
Core Web Vital Affected
LCP
This affects how and when related data is loaded from the database, impacting page load speed and responsiveness.
Optimization Tips
1Use LAZY fetch to load related data only when needed.
2Avoid EAGER fetch for large or deeply nested relationships.
3Monitor backend response times to detect heavy eager loading.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance drawback of using EAGER fetch in Spring Boot?
AIt delays loading related data until accessed.
BIt loads all related data immediately, increasing initial load time.
CIt reduces memory usage by loading less data.
DIt improves backend response time by batching queries.
DevTools: Network and Performance panels
How to check: Use Network panel to observe backend API response times; use Performance panel to see how long rendering waits for data.
What to look for: Long backend response times or large payloads indicate EAGER fetch; faster responses with smaller payloads indicate LAZY fetch.