DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Best Performance Practices for Hibernate 5 and Spring Boot 2 (Part 1)
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • Spring Data: Data Auditing Using JaVers and MongoDB
  • How to Build a Chat App With Spring Boot and DynamoDB

Trending

  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  1. DZone
  2. Data Engineering
  3. Databases
  4. Improving Backend Performance Part 1/3: Lazy Loading in Vaadin Apps

Improving Backend Performance Part 1/3: Lazy Loading in Vaadin Apps

In this article, I'll show you how easy it is to take advantage of Spring Boot data to easily implement lazy loading in Vaadin Flow applications.

By 
Alejandro Duarte user avatar
Alejandro Duarte
DZone Core CORE ·
Oct. 12, 21 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
6.9K Views

Join the DZone community and get the full member experience.

Join For Free

If you have a table or data grid with, say, more than a few hundred rows in it, you should be using lazy loading. This is especially true in the case of Vaadin's Grid component which makes it very easy to show data from an array or collection of POJOs. In this article, I'll show you how easy it is to take advantage of Spring Boot data to easily implement lazy loading in Vaadin Flow applications. You can find a video version of this topic if you prefer:


Understanding the Example Application

Let's start with a previous Vaadin Flow application that I developed from scratch in a previous article. In short, we have a Spring Boot application that connects to a MariaDB instance using Hibernate/JPA to read data about books. There is a JPA Entity called Book, a Spring repository called BookRepository, and a service class called BookService. This is a common pattern you might find in the industry. The web user interface (UI) is implemented with Vaadin Flow in a class called BooksView.

Understanding the Problem to Solve

Let's say the application has been in production for some time and there are 50.000 books in the database. With this rather modest number of rows in a table, we can easily run out of memory with the default JVM configuration of a Spring Boot application.

If we use the findAll() method to get all the books from the database and show them in a Grid, every time a view is requested, a new copy of the data is kept in memory. With 50.000 books in the example application, this means that once there are two views opened in a browser (two users or one user with two tabs showing the view), we'll get an out-of-memory error. Even if we increased the JVM memory to zillions of bytes, we'd find that loading the books takes too long for a good user experience.

Any time we think a table could contain more than, say, 100 rows, we should stop using the findAll() method and consider the lazy loading technique. Lazy loading allows us to query only part of the data set from a database. A screen will rarely be able to show 50.000 rows, so it's a waste of resources to load all the 50.000 books when we can show only a few.

Fixing the Backend

In Spring Data, the findAll() method is overloaded with a version that allows us to specify a slice or "page" of data. JPA uses this information to build the underlying SQL query using the LIMIT and OFFSET clauses. Here's the change we need in the service class:

Java
 
@Service
public class BookService {

    private final BookRepository repository;

    public BookService(BookRepository repository) {
        this.repository = repository;
    }

    public Stream<Book> findAll(int page, int pageSize) {
        return repository.findAll(PageRequest.of(page, pageSize)).stream();
    }

}


Fixing the Grid

The Grid component overloads the method setItems() with a version that allows us to pass the information of the page of the Grid that is currently visualized in the browser according to the position of the scroll bar. The change is simple:

Java
 
@Route("")
public class BooksView extends VerticalLayout {

    public BooksView(BookService service) {
        var grid = new Grid<Book>();
        ...

        grid.setItems(query ->
                service.findAll(query.getPage(), query.getPageSize()));

        ...
    }

}


Testing the Solution

With these simple changes, we can try the application and check that it certainly loads the data much faster and that we can open many tabs without crashing the application. We could also enable SQL query login by adding the following to the application.properties file:

Properties files
 
spring.jpa.show-sql=true


If we scroll through the data in the browser, we should see the queries in the server's log.

Lazy loading Database Spring Framework Vaadin application Data (computing) app Spring Boot sql Book

Opinions expressed by DZone contributors are their own.

Related

  • Best Performance Practices for Hibernate 5 and Spring Boot 2 (Part 1)
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • Spring Data: Data Auditing Using JaVers and MongoDB
  • How to Build a Chat App With Spring Boot and DynamoDB

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: