Almost every modern digital product has to incorporate some sort of pagination API. The main role of that API is to retrieve data from a large table c

Why Token-based Pagination Performs Better Than Offset Based?

submited by
Style Pass
2023-05-24 11:30:09

Almost every modern digital product has to incorporate some sort of pagination API. The main role of that API is to retrieve data from a large table chunk by chunk so that your Frontend application does not need to make an API call that has a huge payload. Smaller payload results in less latency.

A commonly found approach for developing a pagination API is offset-based. Here, the Frontend requested 10 rows after the 100th row of the products table. This is harmless when the size of the table is small. However, latency increases with the increasing value of the offset.

Why? Because the database (not all kinds of database) actually prepares offset+limit numbers of rows for this query. So, when the offset value becomes larger, the database has to go through offset+limit numbers of rows.

Let’s do a quick experiment. I have prepared a table named products with 110001 rows in a PostgreSQL database. It has 3 columns; namely id , name , andcreated_at .

Leave a Comment