DEV Community

Cover image for 🐌 “My Spring Boot API Became Slow… Until I Learned Pagination & Sorting”
Shashwath S H
Shashwath S H

Posted on

🐌 “My Spring Boot API Became Slow… Until I Learned Pagination & Sorting”

At first, my Spring Boot APIs worked perfectly.

Data was returned.
UI was happy.
Everything looked fine.

Then the database grew.

Hundreds of records became thousands.
Responses slowed down.
Memory usage increased.

That’s when I realized I was missing something critical:
Sorting and Pagination.


🧠 Why Sorting & Pagination Matter

In real applications:

  • Databases grow fast
  • Fetching everything at once is expensive
  • Clients rarely need all data

Sorting and pagination help you:

  • Improve performance
  • Reduce memory usage
  • Deliver faster APIs
  • Build scalable systems

This is not optional in production backends.


🔀 Sorting Using Method Query Names

Spring Data JPA allows sorting directly in method names.

Example:

List<Employee> findAllByOrderByNameAsc();
List<Employee> findAllByOrderByNameDesc();
Enter fullscreen mode Exit fullscreen mode

This works well when:

  • Sorting logic is fixed
  • Requirements are simple

But real applications need dynamic sorting.


🧭 Dynamic Sorting with the Sort Class

Spring Data JPA provides the Sort class for flexible sorting.

Sorting with repository methods

List<Employee> findByDepartment(String department, Sort sort);
Enter fullscreen mode Exit fullscreen mode

This allows clients to decide:

  • Which field to sort
  • Ascending or descending order

⚙️ Creating Sort Objects

Examples:

Sort sort = Sort.by(Sort.Direction.ASC, sortField);
Enter fullscreen mode Exit fullscreen mode

Multiple sorting fields:

Sort sort = Sort.by(
    Sort.Order.asc("name"),
    Sort.Order.desc("salary")
);
Enter fullscreen mode Exit fullscreen mode

This gives full control without complex queries.


📚 Why Sorting Alone Is Not Enough

Even with sorting:

  • Returning thousands of rows is inefficient
  • APIs become slower
  • Clients struggle to handle large responses

That’s where Pagination comes in.


📄 Understanding Pagination (Simple Terms)

Pagination breaks large datasets into smaller chunks.

Key Pagination Concepts

🔹 Page

Represents a single chunk of data.
It also contains:

  • Total elements
  • Total pages
  • Current page data

🔹 Pageable

Defines:

  • Page number
  • Page size
  • Sorting rules

🔹 PageRequest

A concrete implementation of Pageable used to create pagination objects.


🧩 Using Pageable in Repositories

Spring Data JPA makes pagination extremely simple.

Page<User> findAll(Pageable pageable);
Page<User> findByLastName(String lastName, Pageable pageable);
Enter fullscreen mode Exit fullscreen mode

No SQL.
No complex logic.
Just clean method signatures.


🛠️ Creating a Pageable Instance

Pageable pageable = PageRequest.of(
    pageNumber,
    size,
    Sort.by("lastName").ascending()
);
Enter fullscreen mode Exit fullscreen mode

With this:

  • You control page size
  • You control page number
  • You control sorting

All in one object.


⚠️ The Mistake I Was Making

I used to:

  • Fetch all records
  • Sort in memory
  • Ignore scalability

It worked… until data increased.

After using pagination and sorting:

  • APIs became faster
  • Memory usage dropped
  • Backend felt production-ready

🚀 Final Thoughts

Sorting and Pagination are not “extra features”.

They are core backend fundamentals.

If your Spring Boot APIs feel:

  • Slow
  • Heavy
  • Hard to scale

Start here.

This post is part of my learning-in-public journey while exploring Spring Boot and real-world backend development.

Top comments (0)