8

I was following this Redis tutorial http://redis.io/topics/twitter-clone

At the end of the page they stated

Note: LRANGE is not very efficient if the list of posts start to be very big, and we want to access elements which are in the middle of the list, since Redis Lists are backed by linked lists. If a system is designed for deep pagination of million of items, it is better to resort to Sorted Sets instead.

That is my biggest worry because we're designing a system that should be capable of handling millions if not billions of post, and we estimated that an item will be searched in the middle of the list 20-30% of the time the web application is running.

There is a problem with a sorted set though.. You can add a sorted set like the following, taking in account that it's the first (1) post

zadd zset 1 "We are happy to announce a new member in our team"

But what if we need to add a content field too?

zadd zset 1 "We are happy to announce a new member in our team" "Please welcome James Sullivan, our new member team! He had..."

This is the error you get

(error) ERR syntax error

Other than the odd fact that you have to access the fields like post[0] and post[1] instead of post['title'] and post['content'] (in theory, I've yet not tried it) there is also this fact that we can't get over as of now.

Practically the structure of a post object is like this and it should be findable in O(1) time even in the middle of million post objects

+-----------+
+- 1 <- ID -+
+-----------+
+------------------------+
+- title "the title..." -+
+----------------------------+
+- content "the content..." -+
+-----------------------------+
+- tags "1st tag, 2nd tag.." -+
+-----------------------+
+- author "the_author" -+
+-------------------------+
+- date "unix_timestamp" -+
+-------------------------+

I'm at a loss if a Sorted Set is the right data structure to use and if not what one (sets, sorted sets, lists, hashes..) should be used..

What would you suggest in this case?

user3710273
  • 323
  • 3
  • 8

1 Answers1

3

Sorted sets are the ideal data structure for stuff you want to sort but, as you noted, the set's members are strings. That being the case, you have two possible options:

  1. Serialization: you can implement your own or use something ready (e.g. JSON) to store multiple elements in each member of the set.

  2. Have your set members' contents be the names of other keys and store your objects in these referenced keys - the HASH data type looks like a good match for your needs.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117