开发者

Question related to the use case of Redis/NoSQL database

I am building a website where I will have two kind of users - X and Y. X will be few (hundreds) and Y will be many (millions). Basically for each X, there will be some set of Ys. Y will add friends in their network. X will post a message which can be seen to particular set of Y and those people can forward that message to their friends. Friends can see that message and can either forward to their friends or reply back to the sender.

So this is my use case and I was exploring different kind of databases primarily NoSQL databases because I am considering scalability and performance of the system as the main concerns in my website. I have started using Spring Data Redis APIs and found it quite useful to my use case. My question here is how do we perform 'update' operation in NoSQL databases specifically in Redis. Lets say I want to update user information stored in Redis. Another question is how do I perform operations like get me all the users who are below 30 years of age provided we have stored user information in Redis which has got 'age' field.

I am 开发者_开发技巧quite new in NoSQL world and have very less experience with it. I would also want to hear from experienced people about the right database for my use case. I have previously used Spring, Hibernate combination with MySQL as database and was not satisfied with the performance of the system when load is heavy on the system.

Thanks, Sachin


My question here is how do we perform 'update' operation in NoSQL databases specifically in Redis. Lets say I want to update user information stored in Redis.

This depends on the structure to which you store your users. A fifteen minute introduction to Redis data types tutorial can help you to get a bigger picture of these data structures. Usually updates are done with SET operations, so if I have stored user information in hash structure and want to update certain field I would use HSET command.

Another question is how do I perform operations like get me all the users who are below 30 years of age provided we have stored user information in Redis which has got 'age' field.

Redis is an advanced key/value data store and it doesn't support ad hoc querying of data like you may be used to from SQL world. If you need this querying functionality you should rather look at other NoSQL solutions which supports it, for example try to look at MongoDB or some other from this list.


The best way to check if Redis can do what you want is to install it in a development machine and give redis-clia try. This built-in client has command completion and help available (just hit help <TAB> to see categories), and also a good documentation is available at http://redis.io/commands.

As for your request, you should study the sorted set datastructure, to store the user's info. This is better than sets alone, because it does allow you some basic form of queries, as you will see:

Suppose you want to be able to query all users between 18 and 30 years in your database, which contains (as an example) 3 users: Joe, 28 years; Bob, 17 years; and Adam, 50 years.

You would populate Redis as:

ZADD age 28 Joe
ZADD age 17 Bob
ZADD age 50 Adam

The syntax is ZADD [key] [score] [member]. The set is in [key], and in sorted sets each [member] has a [score] which MUST BE A DOUBLE. That means you can't query scores using string patterns or even string values (in this point Mongo should be better).

So, time to query. I will paste redis-cli queries now:


To list all users between 18 and 30 years, you would do:

redis> ZRANGEBYSCORE age 18 30
1) "Joe"
redis> ZRANGEBYSCORE age 18 30 WITHSCORES
1) "Joe"
2) "28"

The option WITHSCORES show the score of each member in the result.


To list all users under the age of 18:

redis> ZRANGEBYSCORE age 0 (18
1) "Bob"
redis> ZRANGEBYSCORE age 0 (18 WITHSCORES
1) "Bob"
2) "17"

The ( modifier allows the beginning or end interval to be open. It would be like where age >=0 and age < 18.


To list all users above the age of 30:

redis> ZRANGEBYSCORE age 30 +inf
1) "Adam"
redis> ZRANGEBYSCORE age 30 +inf WITHSCORES
1) "Adam"
2) "50"

The +inf is infinity, so there is no upper limit on the result, like where age >= 30.

And so on.


This is really just the beginning. You can do intersections, unions and select count(*) operations in sorted sets, all very fast, specially if you use an optimized library (like phpredis). Hope you get a good impression of it now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜