Efficient Data Pagination Keyset vs. Offset
Lukas Schneider
DevOps Engineer · Leapcell

Introduction
In the world of web applications and data-driven systems, efficiently displaying large datasets is a fundamental challenge. Imagine a social media feed, an e-commerce product catalog, or a log file viewer – all these scenarios involve retrieving and presenting potentially millions of records to users. While simply fetching all data at once is impractical and resource-intensive, pagination emerges as the de-facto solution. Pagination allows us to break down vast datasets into manageable chunks, improving performance, reducing load times, and enhancing user experience. This article will delve into two prevalent pagination strategies: Offset Pagination and the increasingly favored Keyset Pagination, also known as Cursor Pagination, analyzing their underlying principles, implementation details, and practical implications for building scalable and responsive applications.
Core Concepts
Before we dive into the specifics of each pagination method, let's define some key terms that will be relevant throughout our discussion:
- Page Size (Limit): The maximum number of records to retrieve for a single page.
- Page Number: An integer indicating the sequential order of the current page (e.g., page 1, page 2, page 3). This is typically associated with Offset Pagination.
- Offset: An integer representing the number of records to skip from the beginning of the dataset before fetching the desired page. This is also linked to Offset Pagination.
- Cursor: A unique identifier (often a primary key, timestamp, or a combination of columns) of the last record retrieved in the previous page. This is central to Keyset Pagination.
- Stable Sort Order: A consistent and predictable order in which data is retrieved. This is crucial for both methods, but especially for Keyset Pagination, to ensure correct and non-repetitive results across pages. Usually defined by an
ORDER BY
clause.
Offset Pagination
Offset Pagination is perhaps the most intuitive and widely understood pagination technique. It works by skipping a certain number of records (OFFSET
) and then retrieving a fixed number of records (LIMIT
).
Principle
The principle is straightforward: to get the Nth page, you skip the first (N-1) * LIMIT
records and then fetch the next LIMIT
records.
Implementation
Consider a products
table with columns id
, name
, price
, created_at
.
SELECT id, name, price FROM products ORDER BY id ASC LIMIT 10 OFFSET 0; -- First page (page 1, page_size = 10) SELECT id, name, price FROM products ORDER BY id ASC LIMIT 10 OFFSET 10; -- Second page (page 2, page_size = 10) SELECT id, name, price FROM products ORDER BY id ASC LIMIT 10 OFFSET 20; -- Third page (page 3, page_size = 10)
In an API context, the request might look like: GET /products?page=2&limit=10
. The backend would then calculate the OFFSET
as (page - 1) * limit
.
Advantages
- Simplicity: Easy to understand and implement.
- Random Access: Allows users to jump directly to any page number (e.g., go to page 100).
- Total Count Display: If a total count of items is available, it's easy to display "Page X of Y" or a pagination UI with numbered pages.
Disadvantages
- Performance Degradation with Large Offsets: As the
OFFSET
value increases, the database has to scan and discard more and more records, leading to significantly slower query times. This is the primary drawback for large datasets. - Skipping/Duplicate Records: If records are inserted or deleted before the current offset while a user is paginating, the user might see duplicate records or miss some records entirely. This is known as the "phantom problem" and can lead to an inconsistent user experience.
Use Cases
- Admin dashboards with relatively small datasets.
- Situations where occasional inconsistencies due to data changes are acceptable.
- Applications where random page access is a critical requirement and the total number of records is not excessively large.
Keyset Pagination (Cursor Pagination)
Keyset Pagination, also known as Cursor Pagination, offers a more robust solution to the performance and consistency issues faced by Offset Pagination, especially for very large datasets.
Principle
Instead of skipping a fixed number of rows, Keyset Pagination uses the values of the last record from the previous page as a "cursor" to determine where to start fetching the next page. It relies on a unique, ordered set of columns (the "keyset") to define the next starting point.
Implementation
Let's continue with our products
table example, assuming id
is a unique and auto-incrementing primary key.
To get the first page:
SELECT id, name, price FROM products ORDER BY id ASC LIMIT 10; -- Assume the last record's id in this result set is 10.
To get the second page (after id = 10
):
SELECT id, name, price FROM products WHERE id > 10 -- The "cursor" is the last ID from the previous page ORDER BY id ASC LIMIT 10;
If we need more complex sorting, such as by price
and then id
for ties:
SELECT id, name, price FROM products ORDER BY price ASC, id ASC LIMIT 10; -- Assume the last record of this page is {id: 7, price: 9.99}.
To get the next page:
SELECT id, name, price FROM products WHERE (price > 9.99) OR (price = 9.99 AND id > 7) -- Cursor based on price AND id ORDER BY price ASC, id ASC LIMIT 10;
In an API context, the request for the next page might look like: GET /products?limit=10&last_id=10
or GET /products?limit=10&last_price=9.99&last_id=7
. The last_id
or combination of last_price
and last_id
acts as the cursor.
Advantages
- Consistent Performance: Does not degrade with increasing "page" numbers. The
WHERE
clause with an indexed column (likeid
or a combination for the keyset) allows the database to quickly jump to the starting point, making performance largely independent of the depth of pagination. - Robustness to Data Changes: Inserts or deletes before the current page do not affect the integrity of the current page or subsequent pages. Users will not see duplicate records or miss records they were supposed to see, as the cursor points to a specific point in the sorted dataset.
- Scalability: Highly suitable for very large datasets and high-traffic applications.
Disadvantages
- No Random Access: Users cannot directly jump to an arbitrary page number. They can only navigate "next" or "previous" (by reversing the
WHERE
clause and sort order). - No Total Count: It's difficult to display "Page X of Y" without a separate, potentially expensive,
COUNT(*)
query. - Requires Stable Sort Order: A consistent and unique sort order (the keyset) is mandatory. If the sort order isn't unique, you'll need to add a tie-breaking column (like the primary key) to the keyset.
- More Complex Implementation: Can be trickier to implement, especially with composite keysets or when handling "previous page" functionality.
Use Cases
- Social media feeds (e.g., Twitter, Facebook) where users scroll infinitely.
- Log exploration tools where new data is constantly being added.
- E-commerce product listings where performance and consistency are paramount.
- Any application dealing with very large, frequently updated datasets where users primarily navigate sequentially.
Conclusion
Both Offset Pagination and Keyset Pagination serve the purpose of breaking down large datasets into manageable chunks, but they excel in different scenarios. Offset Pagination offers simplicity and direct access to pages, making it suitable for smaller datasets or specific administrative interfaces where performance at deep pages is not critical. However, its performance degrades with increasing offsets, and it's susceptible to data inconsistencies during concurrent modifications.
Keyset Pagination, on the other hand, provides superior performance consistency and robustness against data changes, making it the preferred choice for large, dynamic datasets and user-facing applications requiring high scalability and a seamless user experience. While it sacrifices random page access and simplicity, its advantages in efficiency and data integrity often outweigh these drawbacks in modern, data-intensive environments. Ultimately, the choice between these two methods hinges on the specific project requirements, dataset size, and anticipated user interaction patterns. For most modern web applications dealing with significant data, Keyset Pagination leads to a far more performant and reliable user experience.