Menu

1 / 2

Offset Pagination Step by Step (with Sharding)

DEV Community·Mohamed Idris·6 months ago
#7K6G6H5N
#why#comment#backend#shard#next
Reading 0:00
15s threshold

Why pagination exists

Imagine a workers table with 1,000,000 rows. A request like GET /workers that returns all of them would:

send a huge JSON payload over the wire

crash the browser trying to render it

hammer the database

The fix is simple: send a small slice at a time. That slice is called a page .

That is all pagination is. We will build the rest from this idea.

Cute mental model

Think of a deck of 100 cards. You cannot hand someone the whole deck and ask them to find one card fast. Instead you say:

"Take 10 cards at a time. When you finish, ask me for the next 10."

10 cards = page size

"which group of 10" = page number

"ask me for the next 10" = next page link

That is the whole game.

Step 1: the two numbers we always need

To grab a slice from a database, every ORM needs two things:

take — how many rows to return (the slice size)

skip — how many rows to ignore from the start

In SQL these are LIMIT and OFFSET .

SELECT * FROM workers ORDER BY id ASC LIMIT 10 OFFSET 20 ;

Enter fullscreen…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.