Active Record Pagination: Transitioning from Offset to Keyset for High-Concurrency Datasets
20K reputation · 17 Jun 2021, 16:55 UTC
In Rails 7.x, Active Record provides standard offset-based pagination. However, as datasets grow to millions of records, the linear performance degradation of OFFSET becomes a primary driver of database latency, especially when multiple concurrent requests compete for connection pool resources.
While keyset pagination (cursor-based) is a recognized alternative to avoid scanning thousands of rows, there is no native, unified Active Record API that abstracts keyset pagination across different database adapters. This forces a choice between custom SQL implementations or third-party gems, creating inconsistency in how large-scale data is retrieved under load.
Given the goal of maintaining low latency during concurrent access to deep pages of a dataset:
- What is the recommended architectural approach for implementing keyset pagination within the standard Active Record pattern?
- How should the implementation handle non-unique sort keys to ensure no records are skipped or duplicated?