Recap: the other way
In the previous post we built offset pagination ( ?page=2 ). The server runs LIMIT 10 OFFSET 10 . Simple, but it has two pains:
Deep pages are slow. OFFSET 1000000 makes the database walk and throw away a million rows.
New rows shift the pages. If someone inserts a row while you scroll, page 2 might repeat a row from page 1 or skip one.
Cursor pagination fixes both. Twitter, Instagram, GitHub, Slack — almost every infinite-scroll feed uses it.
Cute mental model
You are reading a long book. You do not say "give me page 47". You put a bookmark on the last word you read. Next time you open the book, you flip straight to the bookmark and keep going.
The bookmark = the cursor
"Keep going" = WHERE id > bookmark
You never count pages. You just move forward from where you stopped.
That is the whole idea.
Step 1: what exactly is a cursor?
A cursor is just a value that points to a specific row.…