Performance Boosters in Spring Boot: Lazy Loading, Caching & Dirty Checks

Table of contents
Let's break down Lazy Loading, Caching, and Dirty Tracking in simple terms with examples in Java Spring Boot.
1. Lazy Loading
What it is:
Lazy Loading means loading data only when it's needed, rather than loading everything upfront.
Example:
Imagine you're reading a book, and there are footnotes. Instead of loading all footnotes at once, you only check them when you see a reference (like a small number).
In Spring Boot (with JPA/Hibernate):@Entity public class Author { @Id private Long id; private String name;
@OneToMany(mappedBy = "author", fetch = FetchType.LAZY) // Lazy Loading private List books; // Books are NOT loaded until explicitly requested }
// When you fetch an Author, books won't load immediately Author author = authorRepository.findById(1L).get(); // Only author data is fetched
// Only when you access books, they are loaded (if needed) List books = author.getBooks(); // Now, a query runs to fetch books
Why use it?
Improves performance by avoiding unnecessary database queries.
Useful when dealing with large datasets.
2. Caching
What it is:
Storing frequently used data in memory (cache) so that future requests can be served faster without recomputing or fetching from the database.
Example:
Think of a waiter in a restaurant. Instead of going to the kitchen every time someone asks for the menu, he keeps a copy (cache) of the menu in his pocket for quick access.
In Spring Boot (using @Cacheable
):@Service public class ProductService {
@Cacheable("products") // Caches the result public Product getProductById(Long id) { // This method runs only if the product is not in cache return productRepository.findById(id).orElse(null); } }
// First call -> Fetches from DB and stores in cache Product p1 = productService.getProductById(1L);
// Second call -> Returns from cache (no DB call) Product p2 = productService.getProductById(1L);
Why use it?
Reduces database load.
Speeds up responses for repeated requests.
3. Dirty Tracking
What it is:
Automatically detecting changes in an object so that only the modified fields are updated in the database (instead of updating everything).
Example:
Imagine you're editing a document. Instead of rewriting the whole document, you only save the sentences that were changed.
In Spring Boot (JPA/Hibernate does this automatically):@Entity public class Employee { @Id private Long id; private String name; private String department; }
// Fetch an employee Employee emp = employeeRepository.findById(1L).get();
// Change only the department emp.setDepartment("HR");
// JPA tracks this change and updates ONLY the department in DB
employeeRepository.save
(emp); // Only "department" is updated (not "name")
Why use it?
Improves performance by updating only changed fields.
Reduces unnecessary database operations.
Subscribe to my newsletter
Read articles from Niharika Maruvada directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Niharika Maruvada
Niharika Maruvada
JAVA Developer Passionate about crafting innovative and efficient solutions in the FinTech industry, I thrive on building cutting-edge applications that solve real-world problems. With a strong focus on clean, scalable, and maintainable code, I aim to drive business success through technology. Always eager to embrace new challenges and expand my skill set, I am committed to staying at the forefront of emerging technologies and best practices. My dedication to continuous learning and professional growth fuels my ability to deliver impactful results. Let’s connect and build the future of FinTech together! #Java #FinTech #Innovation #CleanCode #TechEnthusiast #ContinuousLearning