Search code examples
redisordered-map

REDIS: List with random access


What's the best way to keep a large list (e.g. 10K items) in Redis, where I also want to efficiently retrieve items by key.

It seems Redis has no data structure equivalent to Java's OrderedHashMap, which accomplishes this, so maybe it's necessary to maintain a set and a list and ensure they stay in sync.


Solution

  • Use a sorted set;

    Add some bookmarks; use current time for score to sort chronologically:

    > zadd bookmarks 123 "bk1"
    > zadd bookmarks 456 "bk2"
    > zadd bookmarks 789 "bk3"
    > zadd bookmarks 999 "bk4"
    

    To get a bookmark, you need the index first:

    > zrank bookmarks "bk3"
    > "3"
    

    ...then pull the bookmark by index:

    > zrevrange bookmarks 3 3
    > "bk3"
    

    If you don't want to use timestamps, you can sort bookmarks lexicographically using "1" for score:

    > zadd bookmarks 1 "link_xyz"
    > zadd bookmarks 1 "link_abc"
    > zadd bookmarks 1 "link_foo"
    
    > zrange bookmarks 0 -1
    
    1) "link_abc"
    2) "link_foo"
    3) "link_xyz"
    

    The index lookup is O(log(n)), add to that O(log(n)+1) to pull a single member by index; better than O(n) for lists.

    Also, if you add the same bookmark twice, redis automatically replaces the previous member, so you avoid duplicates.

    Hope it helps,