What pagination method is better for rest api's (page, size) or (limit, offset)
Asked Answered
R

4

9

I searched many stack overflow answers,articles and could't get an concrete idea about this and that is why i'm asking this question,

References : 10 Best Practices for Better RESTful API

I am just wondering what should we need to use when we do pagination in rest apis, In spring framework they are providing (page,size) by default to implement paging in apis and i think using (page, size) is more human readable and make sense rather than (limit, offset), Is there any reason why spring is providing (page,size) by default rather than limit,offset and many answers are to justify that (limit,offset) is better than (page,size).

https://somewhere.com/results?page=1&size=20

https://somewhere.com/results?limit=20&offset=0

Rabbinical answered 9/5, 2019 at 11:55 Comment(1)
Is this a generic question or is it intended for a particular use-case? Because the answers will vary accordingly.Islek
F
11

In Page/PageSize vs Offset/Limit, using Page/PageSize is simpler, because PageSize is the same for all Pages ; but using Offset/Limit gives you more precise data if you are targeting specific range of items, and you cannot do this using Page/PageSize method. Image below shows you the relationship between the two:

enter image description here

On the first block if you are targeting for the item 6 to item 8, your PageSize is 3:

Limit = PageSize = 3
Page = 3 // from the first block above, item 6 to item 8 sits on Page 3
Offset = (Page * PageSize) - PageSize
Offset = (3 * 3) - 3
Offset = 6

On the second block if you are targeting for the item 7 to item 9, so your PageSize is 3 again:

Limit = PageSize = 3
Page = 3.3333 // from the 2nd block, item 7 to item 9 sits exactly in Page = 3.3333, and not in Page = 3
Offset = (Page * PageSize) - PageSize
Offset = (3.3333 * 3) - 3
Offset = 9.9999 - 3
Offset = 7 

On the 3rd block you are targeting for item 7 to item 10, so your PageSize = 4:

Limit = PageSize = 4
Page = 2.75 // from the 3rd block, item 7 to item 9 sits exactly in Page = 2.75, and not in Page 3, if the PageSize is 4
Offset = (Page * PageSize) - PageSize
Offset = (2.75 * 4) - 4
Offset = 11 - 4
Offset = 7 

Ovbiously the Offset is the starting index of your target items and the Limit is the count of the items you are targeting. I'm just showing you the relationship of the 2 methods.

Frazil answered 17/5, 2020 at 22:15 Comment(0)
G
4

Limit and offset are a little easier to use in code if you don't use spring for example, you can pass these values directly to the dbms.

In case of page and size you have to calculate the values offset and limit. In case of a framework it will do the work for you.

But in my opinion, there is no "best way" of doing it. Both solutions works fine for multiple cases.

Goddess answered 9/5, 2019 at 12:1 Comment(1)
I respect your answer and i think the answer for this is more related to opinion and culture @Goddess thank you for providing more descriptive answerRabbinical
R
1

I use the page&limit approach. IMHO those are the best names for params. So it would go like this:

https://somewhere.com/results?page=1&limit=10

I couldn't find any clear answers on 'how to do it' question.

Rosetta answered 9/5, 2019 at 12:3 Comment(2)
I respect your answer and i think the answer for this is more related to opinion and culture @Rosetta thank youRabbinical
"imho best names" doesn't give a rationale and it doesn't answer the question.Incoercible
S
1

I think limit/size is better because it gives the client more options. They can request any set of results. The downside is that the client needs to calculate the offset.

The page/size is tempting, because it is more natural for ppl, and easier for clients to implement. And as long as you don't need to get 50 results, but omit the first 25 of them it is fine. But if you do - that's a bummer.

Streusel answered 11/4, 2022 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.