0
0
Rest-apiConceptBeginner · 3 min read

What is Offset Pagination: Explanation and Example

Offset pagination is a technique in REST APIs to divide large sets of data into smaller chunks by specifying an offset (starting point) and a limit (number of items). It helps clients request data page by page, like flipping through pages in a book.
⚙️

How It Works

Offset pagination works like a bookmark in a book. Imagine you have a long list of items, but you want to see only a few at a time. You tell the system where to start (the offset) and how many items to show (the limit). For example, if you set offset to 10 and limit to 5, you get items 11 to 15.

This method is simple and easy to understand because you just count how many items to skip before starting to collect results. It’s like saying, "Skip the first 10 pages, then show me the next 5." This helps when you want to show data in pages on a website or app.

💻

Example

This example shows how to use offset pagination in a REST API request to get a slice of data from a list.

javascript
const data = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape', 'honeydew'];

function getPaginatedData(offset, limit) {
  return data.slice(offset, offset + limit);
}

// Get 3 items starting from index 2
const page = getPaginatedData(2, 3);
console.log(page);
Output
[ 'cherry', 'date', 'elderberry' ]
🎯

When to Use

Use offset pagination when you want a simple way to show data in pages, especially if the data set is not too large or changes slowly. It works well for user interfaces like product lists, message threads, or search results where users can jump to any page.

However, for very large or frequently changing data, offset pagination can be slower because the system must count and skip many items. In those cases, other methods like cursor pagination might be better.

Key Points

  • Offset pagination uses offset and limit to control which items to return.
  • It is easy to implement and understand, like flipping pages in a book.
  • Best for moderate-sized data and simple page navigation.
  • Can be inefficient for very large or rapidly changing data sets.

Key Takeaways

Offset pagination divides data by skipping a set number of items before returning results.
It is simple and intuitive, ideal for basic page-by-page navigation.
Use it when data size is moderate and stable for good performance.
Avoid offset pagination for very large or frequently updated data to prevent slow queries.