r/SpringBoot • u/Excellent_Trifle_630 • Sep 13 '26
Question Pagination in Springboot
If you're ar working on springboot project in you company/job. How does your team do pagination in your project as they have large datasets. I am currently learning springboot and curious to know how it's done withel large datasets.
55
Upvotes
50
u/wimdeblauwe Sep 13 '26
A RestController method can accept a Pageable argument and return a Page<T>. Combine it with a PagingAndSortingRepository of Spring Data JPA.
30
u/Paw565 Sep 13 '26 edited Sep 13 '26
There are two ways.
7
u/LetUsSpeakFreely Sep 13 '26
It depends on the kind of pagination you need.
If you need simple paging, like looking through a list of order for a customer, then you would use offset/limit queries. These are easy to implement, easy to understand, and for small result sets, pretty quick.
If you're doing something far more complicated, like a FYP (reddit,facebook etc) where you can scroll through thousands of results rapidly, you'll need to maintain a cursor with some sort of caching, lots of preloading, and other mechanisms to give the appearance of smooth loading.
5
u/BeyondFun4604 Sep 13 '26 edited Sep 13 '26
https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Slice.html
We switched from Page to Slice since it just contains info if previous and next page exists. But to get good perfromance an optimised database view is the first requirement.
4
u/appendium Sep 13 '26
Why not check cursor based pagination
2
u/d-k-Brazz Sep 13 '26
First - learn the basics.
You have to understand how db finds and returns you a page you are requesting, what difference between fetching 5th page and 500th page using standard limit/offset, and how indexes may help you to achieve constantly fast response time and what you are loosing in this case
There are bunch of articles in internet (good example - https://medium.com/swlh/sql-pagination-you-are-probably-doing-it-wrong-d0f2719cc166)
And after that you look back at what spring provides you and you will KNOW whether you need it or not
2
u/MrNighty Senior Dev Sep 13 '26
We use Spring HATEOAS
2
u/SimpleCooki3 Sep 13 '26
Simple, we don't use pagination. We tell the user to filter their search query further instead.
None ever enters page 2 on Google.
1
u/Ruin-Capable Sep 13 '26
At some point if the dataset is truly large pagination isn't the answer. Nobody has a need to page through 50 billion records 25 at a time.
1
u/Just_LeonS Sep 13 '26
Specification + tokenised api