I have a list of records on the server sorted by a key and use pagination API to return list of segments one by one. Since items can be inserted in the middle of the list, I return the first key of the next page as a pagination token that has to be passed to get the next page.
However, I've found that DynamoDB uses the last key of the current page instead for querying API, which is null if the next page does not exist.
What are pros and cons between using the last item of the current page and the first item of the next page as a pagination token?
As for me returning the first item is more intuitive since it's null only if the next page does not exist.
Using the "last item of the current page" (LICP) is better than using the "first item of the next page" (FINP) because it deals better with the possibility that, in the meantime, some item is inserted between these two items.
For example suppose the first page contains 3 alphabetically ordered names: Adam/Basil/Claude. And suppose the next page is Elon/Francis/Gilbert.
Then with LICP the token is Claude
, while with FINP the token is Elon
. If no new names are inserted, the result is the same when we get the next page.
However, suppose we insert the name Daniel
after getting the first page but before getting the second page. In this case, when we get the second page with LICP we get Daniel/Elon/Francis
, while with FINP we get Elon/Francis/Gilbert
. That is to say, FINP will miss Daniel
, while LICP will not.
Also, FINP may consume more computing resources than LICP, since you must retrieve one extra item (4 items, in the above example, instead of only 3).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With