65

This is similar to to this question which doesn't have any answers. I've read all about how to use cursors with the twitter, facebook, and disqus api's and also this article about how disqus generally built their cursors, but I still cannot seem to grok the concept of how they work and how to implement a similar solution in my own projects. Can someone explain specifically the different techniques and concepts behind them?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Micah
  • 111,873
  • 86
  • 233
  • 325

5 Answers5

83

Lets first understand why offset pagination fails for large data sets with an example.

Clients provide two parameters limit for number of results and offset and for page offset. For example, with offset = 40, limit = 20, we can tell the database to return the next 20 items, skipping the first 40.

Drawbacks:

  • Using LIMIT OFFSET doesn’t scale well for large datasets. As the offset increases the farther you go within the dataset, the database still has to read up to offset + count rows from disk, before discarding the offset and only returning count rows.
  • If items are being written to the dataset at a high frequency, the page window becomes unreliable, potentially skipping or returning duplicate results.

How Cursors solve this ?

Cursor-based pagination works by returning a pointer to a specific item in the dataset. On subsequent requests, the server returns results after the given pointer.

We will use parameters next_cursor along with limit as the parameters provided by client in this case.

Let’s assume we want to paginate from the most recent user to the oldest user.When client request for the first time , suppose we select the first page through query:

SELECT * FROM users
WHERE team_id = %team_id
ORDER BY id DESC
LIMIT %limit

Where limit is equal to limit plus one, to fetch one more result than the count specified by the client. The extra result isn’t returned in the result set, but we use the ID of the value as the next_cursor.

The response from the server would be:

{
   "users": [...],
   "next_cursor": "1234",  # the user id of the extra result
}

The client would then provide next_cursor as cursor in the second request.

SELECT * FROM users
WHERE team_id = %team_id
AND id <= %cursor
ORDER BY id DESC
LIMIT %limit

With this, we’ve addressed the drawbacks of offset based pagination:

  • Instead of the window being calculated from scratch on each request based on the total number of items, we’re always fetching the next count rows after a specific reference point. If items are being written to the dataset at a high frequency, the overall position of the cursor in the set might change, but the pagination window adjusts accordingly.
  • This will scale well for large datasets. We’re using a WHERE clause to fetch rows with id values less than the last id from the previous page. This lets us leverage the index on the column and the database doesn’t have to read any rows that we’ve already seen.

For detailed explanation you can visit this wonderful engineering article from slack!

Anonymous
  • 2,184
  • 15
  • 23
  • 11
    How to bite it with sorting? – siwymilek Mar 26 '19 at 07:47
  • 13
    Maybe citing the source would have be nice : https://slack.engineering/evolving-api-pagination-at-slack-1c1f644f8e12 – Aleanar Jun 18 '19 at 09:53
  • 2
    @Aleanar Thanks for pointing that out. Already had mentioned this in https://stackoverflow.com/questions/13872273/api-pagination-best-practices/49615901#49615901 one, but forgot here. Have edited! – Anonymous Jun 19 '19 at 04:41
  • @ShubhamSrivastava you copy and pasted the a significant part of the article - in the future please start your answer by citing the original author. – linstantnoodles Aug 02 '19 at 20:09
  • @linstantnoodles As mentioned above it was my fault not to mention the source which i rectified. The answer is to serve the purpose of not going through the whole article, so yeah I tried to separate the piece which can be relevant to the question. Anyways thanks for the suggestion. – Anonymous Aug 13 '19 at 07:28
  • 2
    It doesn't work with sorting options other than by id. Here's explanation why - https://gist.github.com/MarkMurphy/170e776940566f96e444adc2c54c6315#gistcomment-3039067 – Jerry Green Sep 27 '19 at 11:27
  • 3
    How are you supposed to go backwards? How can you test if a next set of results exists? – 425nesp Feb 29 '20 at 02:12
  • You change to id >= %cursor and use ASC. If +1 record comes from the query then next/previous exists – smentek Jan 19 '21 at 22:22
  • copy and paste, I guess he really has no idea – jcarlosweb Mar 31 '22 at 22:00
  • @Aleanar - I did not understand what the author meant in the slack post. Copying from the post, can you elaborate on this please? - "Requiring the cursor to be a string value promotes the use of an opaque cursor value. The javascript implementation of the Relay spec for instance uses Base64 encoded IDs as cursor values. This discourages the client from implying what value goes in this field and gives the server the ability to encode additional information within the cursor." – serah Aug 02 '22 at 13:02
3

Here is an article about pagination: paginating-real-time-data-cursor-based-pagination

Cursors – we need to have at least one column with unique sequential values to implement cursor based pagination. This can be similar to Twitter’s max_id parameter or Facebook’s after parameter.

Goo Hong
  • 31
  • 3
1

In general you should pass the current item or page number in the request as a param. Other usual param is the batch size of the page. Then on the server side backend you select and return the proper dataset, with an SQL query for example.

Ho Zong
  • 523
  • 1
  • 4
  • 22
0

enter image description hereHere's what I am Done with. The cursor is working as a pointer and it points to that index. and limit will pick that many rows from that pointer. Let's say we have given id 10 and limit 5 then it will go to id 10 and pick 5 elements from there.

  • 1
    Please post your code directly to the answer, no need of adding extra URLs that can become invalid in future. – Tyler2P Oct 23 '22 at 16:27
-8

Some Graph API connections uses cursors by default. You can use 'limit' and 'before'/'after' parameters in your call. If you are still not clear, you can post your code here and I can explain with it.

Muktadir
  • 115
  • 8
  • 3
    I still don't understand how cursors are built for pagination. What cursors mean? How do you relate them with pagination results? – flyer88 Oct 29 '13 at 21:30