'Offset pagination vs Cursor pagination

I am studying about pagination and I have some questions.

  1. What is the difference between two approches?
  2. Best use-case for a cursor based pagination?
  3. Can cursor based pagination go to a specific page?
  4. Can cursor based pagination go back to the previous page?
  5. Are there any performance differences between the two?

My thoughts

I think cursor based is much more complex which makes offset based pagination more desirable. Only real-time data centric system needs a cursor based pagination.



Solution 1:[1]

Cursor pagination is most often used for real-time data due to the frequency new records are added and because when reading data you often see the latest results first. There different scenarios in which offset and cursor pagination make the most sense so it will depend on the data itself and how often new records are added. When querying static data, the performance cost alone may not be enough for you to use a cursor, as the added complexity that comes with it may be more than you need.

Quoted from this awesome blog post, happy coding!

Also, check this out:

Pagination is a solution to this problem that ensures that the server only sends data in small chunks. Cursor-based pagination is our recommended approach over numbered pages, because it eliminates the possibility of skipping items and displaying the same item more than once. In cursor-based pagination, a constant pointer (or cursor) is used to keep track of where in the data set the next items should be fetched from.

This explanation is from Appolo GraphQL docs.

Solution 2:[2]

This post explains the difference between them.

What is the difference between two approaches?

The difference is big. One paginates using offset, and the other paginates using cursors. There are multiple pros/cons of both approaches. For example, offset pagination allows you to jump to any page, while in Cursor-based pagination, you can only jump into the next/previous page.

There is also a substantial difference in the implementation underneath. The Offset will very likely have to load all the records from the first page to the page you want to get. There are techniques to avoid this.

For more pros and cons I suggest reading the article.

Best use-case for a cursor based pagination?

If you're using a relational Database and have millions of records. Querying high Offset will probably take a lot of time/timeout, while cursor pagination will be more performant.

Can cursor based pagination go to a specific page?

No, this is one of the disadvantages of the approach.

Can cursor based pagination go back to the previous page?

It's a very common technique to provide two cursors as a response, one containing the previous page and the other with the next page. If that's the case, you can go to the previous page. Otherwise, you cannot.

Are there any performance differences between the two?

Yes! see my comment above.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jakša Mališi?
Solution 2 Ignacio Chiazzo