The Deska blog

Cursor vs Offset Pagination: Pick by Access Pattern

Deep dive into cursor vs offset pagination for API design. Learn which strategy fits your dataset, performance requirements, and developer workflow.

· 10 min read

When building an API to handle large datasets, the choice between cursor vs offset pagination determines the scalability and reliability of your entire application. This architectural decision impacts database load, client side performance, and user experience. While offset pagination is often the default due to its simplicity in SQL environments, it introduces significant performance degradation as users navigate deeper into the data. Cursor pagination offers a more stable alternative for high frequency or massive datasets, yet it requires a shift in how you structure your queries and handle state.

Understanding Offset Pagination

Offset pagination is the traditional approach to breaking down record sets. It relies on two fundamental parameters: limit and offset. The limit tells the database how many records to return, while the offset tells it how many records to skip before starting the collection.

This method resembles human book reading. If you want to see the third page of results and each page has ten items, you skip twenty items and read the next ten. In SQL, this translates to a LIMIT 10 OFFSET 20 clause. The primary advantage of this approach is its simplicity. It allows for random access, meaning a user can jump directly from page one to page five without intermediate steps. This makes it suitable for internal tools or administrative dashboards where deep linking to a specific page number is a requirement.

However, offset pagination has a severe architectural flaw. Databases must actually scan and discard all the records specified in the offset before returning the requested page. If you request an offset of one million, the database engine reads one million and ten rows, sorts them, and then throws away the first million. This creates a linear performance degradation known as late row lookups. As your table grows, the cost of fetching late pages increases significantly, leading to slow response times and potential database timeouts.

The Case for Cursor Pagination

Cursor pagination, often referred to as keyset pagination, solves the performance bottlenecks of the offset method. Instead of skipping a set number of rows, the client provides a pointer, the cursor, which indicates the last seen record. The next query then fetches records that appear after that specific pointer.

In a typical implementation, the cursor is a unique, sequential identifier such as an ID or a timestamp. The SQL query uses a WHERE clause instead of an OFFSET. For example, if the last item received had an ID of 500, the next request would ask for items where id > 500 limited to ten results. Because the ID is indexed, the database finds the starting point instantly, regardless of whether there are five records or five million records before it.

The benefits of this approach include:

  • Consistent performance: Response times remain stable even at the tail end of a massive dataset.
  • Resilience to data drift: If a new item is inserted or deleted while a user is paginating, the cursor method prevents items from being skipped or duplicated.
  • Support for infinite scrolling: This pattern is ideal for modern feeds where the user moves forward through a stream of data.

The trade-off is the loss of random access. Users cannot jump to page forty two without knowing the cursor for the end of page forty one. This makes it less flexible for search interfaces where users expect to see a specific total page count.

Access Patterns and Tooling

Selecting the right strategy depends entirely on how your developers and users interact with the data. If you are building a tool for intensive monitoring, you likely need a different approach than a standard CRUD application.

For developers using Deska, access patterns often involve managing multiple streams of information at once. When you are looking at terminals and browser panels side by side, the stability of the data stream matters more than the ability to jump to a specific page number. In a distributed workspace, where you might be using coding agents to analyze logs or history, cursor pagination prevents the common "missing record" bug that happens when new logs are written while the agent is reading.

The local-first nature of development tools also changes the stakes. Since your local-first data and sessions stay on your machine, you have more control over the indexing strategy. Developers using the Deska desktop app for Mac, Windows, and Linux often prefer tools that handle large volumes of local files and session history without lagging. Implementing cursors in your local data layer ensures that the UI remains responsive even when thousands of entries are placed in the canvas workspace.

Technical Comparison Table

The following table summarizes the key differences between the two methods to help guide your architectural choice.

FeatureOffset PaginationCursor Pagination
Database PerformanceSlows down as offset increasesConstant time regardless of depth
Real Time StabilityProne to duplicates or skipsHighly stable with shifting data
Implementation ComplexityLow, native to most SQLMedium, requires unique sorting keys
Random AccessSupported (jump to page X)Not supported (sequential only)
Ideal Use CaseAdmin panels, small datasetsSocial feeds, logs, large datasets

Practical Implementation Tips

When implementing cursor pagination, ensure your cursors are opaque. Do not reveal the underlying column names or logic to the client. Instead, encode your cursor, perhaps using Base64, to prevent users from trying to guess or construct their own cursors. This gives you the flexibility to change the underlying implementation, such as switching from an ID to a timestamp plus an ID, without breaking the public API.

If you choose offset pagination, protect your database by implementing a maximum offset limit. Many public APIs allow users to paginate up to a certain point, such as 1,000 records, and then require a more specific search filter to narrow the results further. This prevents malicious or accidental queries from locking up database resources with massive offsets.

Within a workspace environment like Deska, managing these API responses efficiently is key. You can use the Ask Deska assistant to help write boilerplate for these patterns or to explore how different library implementations handle cursors in your local code editor. The ability to run these queries in a terminal panel while viewing the results in a browser widget side by side makes the debugging process for pagination logic much faster.

FAQ

When should I use offset pagination instead of cursors?

Offset pagination is appropriate when you need to provide a numbered navigation bar or allow users to jump to specific pages. It is ideal for datasets that are relatively small, rarely change, or where the performance hit of deep scanning is not a concern for the specific business context.

How do I handle multi column sorting with cursors?

To sort by a non unique column, like a timestamp, you must include a secondary unique column, like an ID, in your cursor. This creates a deterministic sort order. The cursor would then represent both values, allowing the query to filter for records that are "greater than" that specific combination of values.

Does cursor pagination work with search filters?

Yes, cursor pagination works effectively with search filters. The cursor simply becomes an additional filter in your WHERE clause alongside your search parameters. As long as your search criteria and the cursor column are supported by proper database indexing, the performance remains high even with complex filtering.

Optimize your Developer Workflow

Effective API design is just one part of the engineering puzzle. To stay productive while building and testing these systems, you need a workspace that adapts to your needs. Deska provides a free desktop app for Linux, Mac, and Windows that helps you manage terminals, editors, and AI agents in a single, infinite canvas.

Whether you are debugging pagination logic locally or monitoring deployments through the mobile app secure relay, Deska keeps your tools organized and your data private. You can download the app today to start building your next project in a local-first, AI integrated environment. For more information on how to structure your projects, check out the documentation on workspaces and panels.

💡 Ideas+🐛 BugsSuggest a feature or report a bug