Redis Sorted Sets and Client-Side Pagination: ZRANGE vs ZSCAN
20K reputation · 29 Jul 2023, 05:51 UTC
Pagination Strategy for Large Datasets
Implementing pagination for large datasets in Redis often involves a choice between rank-based indexing using ZRANGE and cursor-based iteration via ZSCAN. While ZRANGE allows for precise offset-based bounding, the underlying skip list traversal can lead to increased latency as the offset grows deeper into the set.
Conversely, ZSCAN mitigates server-side blocking and performance degradation associated with high offsets, but it does not guarantee a strict sorted order across multiple cursor calls, which is often a requirement for user-facing paginated interfaces.
Given these architectural trade-offs in Redis 7.0+, what is the recommended approach for maintaining strict sort order while avoiding the O(log(N)+M) performance penalty of deep offsets? Is there a documented method to combine cursor-based stability with rank-based precision?