Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis add more than one item to Sorted Set

Tags:

redis

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?

like image 623
user3710273 Avatar asked Jun 05 '14 08:06

user3710273


People also ask

Can members be added to a sorted set with the same score?

While the same element can't be repeated in a sorted set since every element is unique, it is possible to add multiple different elements having the same score.

How do I create a sorted set in Redis?

To create a sorted set, use the zadd command. zadd accepts as arguments the name of the key that will hold the sorted set, followed by the score of the member you're adding and the value of the member itself.

What does Redis use to sort the elements of a sorted set?

A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically. Some use cases for sorted sets include: Leaderboards.

How do you find the intersection of two sorted sets in Redis?

Redis - Sorted Set Zinterstore Command Redis ZINTERSTORE command computes the intersection of numkeys sorted sets given by the specified keys, and stores the result in the destination. It is mandatory to provide the number of input keys (numkeys) before passing the input keys and the other (optional) arguments.


1 Answers

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.

like image 69
Itamar Haber Avatar answered Oct 21 '22 07:10

Itamar Haber