← homeProgramming (Програмування)

Offset vs Cursor Pagination in Rails: what to choose and why

Offset and cursor pagination in Rails are two approaches to splitting data into pages. Offset is simpler but slower with large volumes. Cursor provides stability and speed. Find out which method to choose and how to i...

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
In web applications with large datasets, the issue of pagination arises sooner or later. Displaying thousands of records in a table or list at once is a bad idea for both performance and user experience. The most common approaches in Rails are offset pagination and cursor pagination. Let's explore what they are, their advantages and disadvantages, and look at implementation examples.

Offset pagination

What is it
The offset approach uses SQL operators OFFSET and LIMIT. It tells the database: "Skip N records and take the next M."
Example in Rails
# Get the second page with 20 records
users = User.order(:id).offset(20).limit(20)
The SQL that will be executed:
SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 20 OFFSET 20;
Advantages
  • Simple to implement.
  • Well integrated with existing gems (e.g., kaminari, will_paginate).
  • User-friendly: you can quickly jump to any page (e.g., page #50).
Disadvantages
  • Performance issue: the larger the offset, the slower the query works. The database still has to go through N records to skip them.
  • Result instability: if records are added or removed from the table during browsing, pages "shift." Users may skip or see the same data twice.

Cursor pagination

What is it
The cursor approach uses a "pointer" (cursor) to the last element of the previous page. Instead of counting from the beginning, we say: "Show the next 20 records after ID = X."
Example in Rails
Let's assume we have users sorted by id.
# Get the first 20 records
users = User.order(:id).limit(20)

# Get the next after the last id
last_id = users.last.id
next_users = User.where("id > ?", last_id).order(:id).limit(20)
The SQL will look like this:
SELECT "users".* FROM "users" WHERE (id > 20) ORDER BY "users"."id" ASC LIMIT 20;
Advantages
  • High performance: the database does not go through thousands of records to find the right place; it jumps directly to it.
  • Stable results: when adding or removing records, the cursor ensures that you won't see duplicates or skips.
Disadvantages
  • No "arbitrary pages." You can only move forward or backward.
  • More complex to implement, especially if the cursor must be based on multiple fields (id) rather than simple sorting conditions.

When to use which approach?

Use offset pagination if:
  • your tables are relatively small;
  • it's important to have the ability to jump to a specific page (e.g., "Page 50");
  • users rarely work with very deep lists.
Use cursor pagination if:
  • you are working with large volumes of data;
  • performance is critically important;
  • you are fine with "Back/Next" buttons instead of specific page numbers;
  • you need display stability with frequent data updates.
Offset pagination is easier to implement, but it is slow and less reliable for large datasets. Cursor pagination is faster and more stable, but it limits navigation.
The choice depends on the task: if you are building a blog or a catalog with thousands of records - offset is suitable. If you are creating an API for a mobile application or working with constantly changing data (chats, news feeds) — choose cursor.

🔥 More posts

All posts
What is integer overflow?
Programming (Програмування)Aug 15, '25 08:28

What is integer overflow?

Integer overflow is the overflow of an integer when the value exceeds the limit of the variable t...

What is a HAR file (HTTP Archive)?
Programming (Програмування)Aug 25, '25 18:23

What is a HAR file (HTTP Archive)?

HAR file (HTTP Archive) is a .har format that stores a log of the browser's interaction with the ...

What is exponential growth?
Sep 16, '25 18:57

What is exponential growth?

Exponential growth is a rapid increase in magnitude, where each subsequent step multiplies the re...

What is factorial complexity?
Programming (Програмування)Sep 16, '25 19:03

What is factorial complexity?

Factorial complexity is the rapid increase in the number of options, where for n elements the pos...

What is NP-complexity?
Programming (Програмування)Sep 16, '25 19:31

What is NP-complexity?

NP-completeness is a class of problems where finding a solution is extremely difficult, but verif...

What is ivar in Ruby / Rails?
Programming (Програмування)Oct 19, '25 20:12

What is ivar in Ruby / Rails?

ivar in Ruby is an instance variable, denoted by @. In Rails, it passes data from the controller ...

Main methods of authentication in API
Programming (Програмування)Oct 19, '25 20:26

Main methods of authentication in API

The main methods of authentication in the API on Ruby on Rails: Basic Auth, Token, JWT, and OAuth...

What is ORM and why is it needed?
Programming (Програмування)Oct 26, '25 14:00

What is ORM and why is it needed?

ORM is a technology that allows working with databases through code objects, simplifying developm...