How to Implement Cursor Pagination Like a Pro

Megan C.
The Startup
Published in
10 min readSep 13, 2019

So you’ve decided to implement cursor pagination on your website. Well, you’ve come to the right place! (If you’re not entirely convinced cursors is right for you, first, check out the benefits in this article to see if it is, indeed, the right option for you.)

First, you will need a unique, sequential column (e.g. timestamps, usernames, emails, etc.). Keep in mind that the data will be sorted by this column. So if, for instance, you wanted to sort a user list by last name, which is generally not considered a unique column, there will be additional work necessary. (If this is the case, never fear! Scroll to the bottom of this article for some tips to get around this limitation.)

But first, let’s discuss the ideal scenario.

Cursor Implementation

If offset pagination is an array, then cursor pagination is a linked list. Where offset grabs records based on where they are located in the table like an index, cursors use a pointer that points to a specific record and grabs the records following this specific record.

To better understand this concept, let’s take a look at the SQL statements used to generate the first 2 pages of the following user list.

║ Last Name ║ First Name ║
║ Bagshot ║ Bathilda ║
║ Black ║ Sirius ║
║ Brown ║ Lavender ║
║ Chang ║ Cho ║
║ Creevey ║ Colin ║
║ Crouch ║ Bartemius ║
║ Delacour ║ Fleur ║
║ Diggle ║ Dedalus ║
║ Diggory ║ Cedric ║
║ Dumbledore ║ Aberforth ║
║ Dumbledore ║ Albus ║
║ Dursley ║ Dudley ║
║ Dursley ║ Petunia ║
║ Dursley ║ Vernon ║
║ Filch ║ Argus ║
║ Finnigan ║ Seamus ║
║ Fletcher ║ Mundungus ║

With a limit of 5 per page, we would expect the first and second page to look like:

Megan C.
The Startup

Just a software girl livin’ in a startup world | Took the midnight train goin’ to Google | UCSB Master’s in Mechanical Engineering