Understanding why cursor pagination can be a better choice for large, frequently changing datasets.
Pagination is one of those features developers implement early and rarely revisit.
In many Laravel applications, the first implementation looks like this:
$users = User::paginate(20);
It is clean, convenient, and often perfectly adequate.
But as datasets grow and records are frequently inserted or deleted, traditional offset-based pagination can become less efficient and sometimes less consistent for users navigating through changing data.
Laravel provides another option: cursor pagination.
Understanding when to use it can make a meaningful difference in how you design database-backed interfaces.
1. How Offset Pagination Works
When you use Laravel's regular paginate() method, the underlying query uses limit and offset pagination.
For example:
$users = User::query()
->orderBy('id')
->paginate(20);
A query for a later page may look conceptually like this:
SELECT *
FROM users
ORDER BY id ASC
LIMIT 20 OFFSET 10000;
The database needs to work through the offset before returning the requested records.
As the offset becomes larger, this approach can become less efficient, depending on the database, query plan, indexes, and workload.
This is not a reason to avoid paginate() everywhere.
For administrative interfaces where users need page numbers, total counts, and manageable datasets, offset pagination remains a practical choice.
The problem is using it indiscriminately.
2. What Cursor Pagination Changes
Cursor pagination does not rely on a page number and a large offset.
Instead, it uses the values of the ordered columns to determine where the next page begins.
For example:
$users = User::query()
->orderBy('id')
->cursorPaginate(20);
Conceptually, the next page might use a query similar to:
SELECT *
FROM users
WHERE id > 10000
ORDER BY id ASC
LIMIT 20;
Instead of skipping thousands of rows, the database continues from the position represented by the cursor.
Laravel encodes the cursor into the pagination URL.
This is especially useful for large datasets and interfaces that primarily need Next and Previous navigation.
3. Why Cursor Pagination Can Be More Efficient
The performance advantage comes from how the database navigates the dataset.
With offset pagination:
LIMIT 20 OFFSET 10000
The database may need to scan through or otherwise account for a substantial number of preceding rows.
With cursor pagination:
WHERE id > 10000
ORDER BY id
LIMIT 20
The database can use an appropriate index to seek into the dataset and retrieve the next records.
The actual performance depends on the database engine, query structure, indexes, and ordering columns.
Cursor pagination is not automatically faster for every query, but it is often a better fit for large datasets where indexed ordered traversal is possible.
4. A Practical Laravel Example
Consider an activity feed.
use App\Models\Activity;
public function index()
{
return Activity::query()
->orderByDesc('id')
->cursorPaginate(30);
}
This is a simple implementation for a feed that displays the latest activities and lets users navigate through older records.
The id column provides a unique ordering value.
In a real application, you may also need filtering, authorization, and additional query constraints.
The important part is that the query has a stable ordering.
Laravel cursor pagination requires an order by clause, and the ordered columns must belong to the table being paginated. The ordering must also be unique, either through a single unique column or a combination of columns that uniquely identifies the position.
5. Ordering Matters More Than You Might Think
This query is potentially problematic:
Activity::query()
->orderBy('created_at')
->cursorPaginate(30);
If several records have exactly the same timestamp, the ordering may not uniquely identify a position in the dataset.
A more reliable approach is to use a unique tie-breaker:
Activity::query()
->orderBy('created_at')
->orderBy('id')
->cursorPaginate(30);
Here, the combination of created_at and id can establish a unique ordering when id is unique.
This is important for consistent cursor navigation.
Cursor pagination works best when the ordering is deliberate and stable.
6. Offset Pagination Can Behave Unexpectedly With New Records
Imagine a dataset ordered by ID:
Page 1:
1, 2, 3, 4, 5
Page 2:
6, 7, 8, 9, 10
A user loads page one.
Before the user navigates to page two, a new record is inserted at the beginning of the dataset.
The dataset now looks like:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Depending on the ordering and offset, the user may encounter records already seen or miss records during navigation.
Cursor pagination avoids some of these offset-related inconsistencies by continuing from a cursor position rather than a numeric offset.
It does not provide a complete snapshot of the dataset, however. Records can still change, and the exact behavior depends on the ordering and concurrent modifications.
7. Cursor Pagination Has Limitations
Cursor pagination is not a replacement for every pagination requirement.
It has some important limitations.
- It does not provide numbered pages like traditional pagination.
- It does not naturally provide a total number of pages.
- The ordering must be suitable for cursor traversal.
- Ordered columns must satisfy Laravel's cursor requirements.
- Null values in ordered columns are not supported.
- Complex query expressions may require additional care.
If the interface requires page numbers, a traditional paginator may be the better choice.
If the interface is an infinite scrolling feed or a large sequential listing, cursor pagination may be a better fit.
8. When I Would Choose Each Method
| Use case | Pagination method |
|---|---|
| Small administrative listing | paginate() |
| Page numbers and total pages | paginate() |
| Next/Previous only | simplePaginate() |
| Large sequential dataset | cursorPaginate() |
| Infinite scrolling | cursorPaginate() |
| Frequently changing feed | Consider cursor pagination |
These are starting points, not universal rules.
The actual decision should depend on the query, database, indexing strategy, and UI requirements.
9. Don't Forget Indexes
Cursor pagination is most useful when the database can efficiently traverse the ordering columns.
For example, if the query orders by id, the primary key index is generally a natural fit.
For a more complex ordering:
Activity::query()
->orderByDesc('created_at')
->orderByDesc('id')
->cursorPaginate(30);
The appropriate indexing strategy should be evaluated against the actual query and database engine.
An index that supports filtering and ordering may improve performance, but index design should be based on query patterns rather than blindly adding indexes to every column.
Use your database's query analysis tools to validate the approach.
Final Thoughts
Laravel's paginate() method is convenient and remains useful in many applications.
But when working with large datasets, frequently changing records, feeds, or sequential navigation, cursor pagination deserves serious consideration.
The decision is not about replacing one pagination method with another everywhere.
It's about understanding how the database retrieves records and choosing the approach that matches the application's requirements.
A small decision in the query layer can make a significant difference as the dataset and usage patterns evolve.

Top comments (0)