Pagination Strategies Explained
Database
19/12/2022
There are two main approaches to pagination on a database-level:
Offset pagination: retrieves the content of a page by traversing items in a list (i.e. the offset). For instance, in a list of 20 items with a page consisting of 5 items each, the database would have to traverse the first 15 items before reaching the contents of page 4.
Cursor-based pagination: retrieves the content of a page by using a marker (i.e. cursor) where the last page ended. This allows the database to skip any items to reach the next page. Using the previous example, to retrieve page 4 one would reference the last item of page 3.
Which one to use
The main advantage of the offset strategy is that it enables users to jump to any page of content. However, it's biggest downside is its slow performance on large datasets as it needs to traverse the entire list to retrieve a page.
On the other hand, cursor-based pagination scales well at a database-level, which makes it the preferred choice for (large) applications. However, a downside of this choice is the inability to skip to any pages. Naturally, this makes it more suitable for apps that use infinite scrolling.
/> Next part