Dynamo DB Pagination

Contributor - 12 September 2017 - 12min
Contributor - 12 September 2017 - 12min
In this article, let us look at pagination and how pagination is done with DynamoDB. Pagination is essential when we have a lot of items to display on the UI and we don’t want the user to wait till all the items are loaded.
DynamoDB is a fully managed NoSQL database service from Amazon that provides fast and predictable performance with seamless scalability. See more over here
Basic understanding of DynamoDB tables, keys, indexes, and operations.
Pagination is splitting a database output into manageable chunks or pages. To do this:
Pagination Area
Amazon DynamoDB documentation says that DynamoDB paginates the results from scan/query operations. With pagination, the scan results are divided into “pages” of data that are 1 MB in size (or less). An application processes the first page of results, then the second page, and so on.
A single scan will only return a result set that fits within the 1 MB size limit. To determine whether there are more results, and to retrieve them one page at a time, your application should do the following:
In other words, the LastEvaluatedKey from a Scan response should be used as the ExclusiveStartKey for the next Scan request.
If there is not a LastEvaluatedKey element in a Scan response, then you have retrieved the final page of results. (The absence of LastEvaluatedKey is the only way to know that you have reached the end of the result set.)
Scan Query response contains the following elements:
Note:
If you do not use a filter expression, then ScannedCount and Count will have the same value.
To find more about DynamoDB pagination click here
DynamoDB does not provide the support to get the total items count for a scan/query operation. It provides the fetched items count for a single scan/query. The scan/query operation can fetch a maximum of 1MB data at a time. With DynamoDB, data for a particular page like page 4, 8 cannot be directly fetched as LastEvaluatedKey for that page is not known. LastEvaluatedKey is known only in sequence.
So, DynamoDB cannot help us achieve page sequencing, landing to a random page, jumping to the last page etc. What can be done in pagination with DynamoDB is, having previous or next or load more button.
Here, I have implemented blog search by title functionality with the following:
NOTE: In DynamoDB, LastEvaluatedKey for table is an object that contains table keys as properties. LastEvaluatedKey for an index is an object that contains tables and index keys as properties.
DynamoDB Bug: In dynamoDB pagination, the lastEvaluatedKey should be undefined when we access the last page as there are no more items. But when we access the last page and itemsPerPage is equal to the items left in DB, then instead of giving LastEvaluatedKey as undefined, DynamoDB give it as an object.
Pagination is very different from SQL databases, and ensuring that you receive complete data is important.
"Limit is not necessarily matched items its a limit on the scanned items, the solution seems to be misguiding.".
"My question is same as @Sajjad Haider said. What about previous button? How can we go to the back page from 2nd to previous OR Last page to Previous? Waiting for response. Thanks in Advance. I don't think that Only receive complete data is sufficient for a good project. Is there any further improvement coming in this DB with multiple features and flexibility like Pagination?".
"how Last and previous button will work here ? i think we can only move next in pagination in dynamo on the basis of lastEvaluatedKey and can not come back through previouse button. is it ??".
"It's a nice article, crisp and easy to understand. I was able to clear many things in my mind on which I was confused earlier. Thanks!".
"That was a good tutorial on implementing DynamoDB pagination. liked it. Hope it will help me to implement pagination on my side".