Redis pagination strategy for endless page scrolling

TL DR: Which of the three options below is most effective for pagination using Redis?

I implement a website with several user-generated publications, which are stored in a relational database and then copied to Redis as key hashes , such as site:{site_id}:post:{post_id} .

I want to perform simple pagination requests to Redis to implement pagination with delayed loading (that is, when a user scrolls down a page, we send an Ajax request to the server requesting the next group of messages) in a Pinterest-style interface.

Then I created a set to track the identifiers of published posts with keys such as site:{site_id}:posts . I chose the sets because I donโ€™t want to have duplicate identifiers in the collection, and I can quickly do this with a simple SADD (no need to check if the identifier exists) with every database update.

Well, since the sets are not ordered, I am considering the pros and cons of the options I need to paginate:

1) Using the SSCAN command to break down my already implemented sets

In this case, I could save the returned scan cursor in a user session, and then send it back to the server at the next request (this does not seem reliable when several users access the database and update it: at some point, the cursor will be invalid and return strange results - unless there is some caution I am missing).

2) Refactoring my sets to use lists or sorted sets instead

Then I could paginate using LRANGE or ZRANGE . The list seems to be the most productive and natural option for my use case. It is ideal for pagination and ordering by date, but I just canโ€™t check for the existence of one item without looping through the whole list. Sorted sets seem to combine the benefits of both sets and lists, but consume more server resources.

3) Continue to use regular sets and save the page number as part of the key

It will be something like site:{site_id}:{page_number}:posts . This was the recommended way before scan commands were implemented.

So the question is, which approach is most effective / simple? Is there any other recommended option not listed here?

+15
source share
1 answer

Best is best served subjectively :)

I recommend you use the second approach, but be sure to use Sorted Sets over Lists. Not only does this make sense for this type of work (see ZRANGE ), they are also more effective in terms of complexity compared to LRANGE -ing a List.

+8
source

All Articles