Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use pagination tokens?

I am implementing pagination on a webservice. My first thought was to use query params page and size, like Spring Data.

However, we are basing some of our design on the google webservice apis. I notice that they use pagination tokens, with each page result containing a nextPageToken. What are the advantages to using this approach? Changing data? What kind of info would be encoded in such a token?

like image 785
xdhmoore Avatar asked Feb 12 '16 18:02

xdhmoore


People also ask

What is a pagination token?

Pagination token definitions previous_token - Opaque string returned within the meta object response on endpoints which support pagination. Indicates that there is a previous page of results available, and can be set as the pagination_token parameter in the next request to return the previous page of results.

Why do we need pagination in API?

Why pagination? A lot of the time, when you're making calls to the Confluence REST API, there'll be a lot of results to return. For that reason, we paginate the results to make sure responses are easier to handle.

Why do we Paginate data?

Thanks to pagination, we can split our large dataset into chunks ( or pages ) that we can gradually fetch and display to the user, thus reducing the load on the database. Pagination also solves a lot of performance issues both on the client and server-side!

When would you use API pagination?

API pagination is essential if you're dealing with a lot of data and endpoints. Pagination automatically implies adding order to the query result. The object ID is the default result, but results can be ordered in other ways as well.


2 Answers

When paginating with an offset, inserts and deletes into the data between your requests will cause rows to be skipped or included twice. If you are able to keep track in the token of what you returned previously (via a key or whatever), you can guarantee you don't return the same result twice, even when there are inserts/deletes between requests.

I'm a little uncertain of how you would encode a token, but for a single tables at least it seems that you could use the an encoded version of the primary key as a limit. "I just returned everything before key=200. Next time I'll only return things after 200." I guess this assumes a new item inserted between requests 1 and 2 will be given a key greater than existing keys.

https://use-the-index-luke.com/no-offset

like image 70
xdhmoore Avatar answered Oct 07 '22 02:10

xdhmoore


One reason opaque strings are used for pagination tokens is so that you can change how pagination is implemented without breaking your clients. A query param like a page(I assume you mean page number) is transparent to your client and allows them to make assumptions about it.

like image 30
Hyangelo Avatar answered Oct 07 '22 00:10

Hyangelo