Redis and Querying Values

Redis is conceptually different from the traditional SQL databases that I use, and I'm trying to figure out if this is suitable for my project ... I look around but can't find the answer to my question.

I have a set of users that I need to store, each with a unique identifier and several values ​​(such as their name) associated with it. It seems I can just save them as a hash:

user:fef982dcfe1a7bcba4849b4c281bba95
"username" "andrewm" "name" "Andrew"

I also have a bunch of messages that I want to save, each of which has several properties, such as sender and receiver:

message:1a7bcba4849b4c281bfef98a952dcfeb
"sender" "fef982dcfe1a7bcba4849b4c281bba95" "recipient" "82dcfe1a7bcba4849b4c281bba95fef9" "message" "Hi!"

My question is: how can I get all messages sent by a specific user (indicated by their hash). Should I use a traditional relational database or even a NoSQL database like MongoDB (which I used before)? If so, does anyone have any suggestions for high-end stores? I will not search for the true query (i.e. MySQL LIKE) - just browse the key values.

+6
source share
1 answer

Of course, you can model this data using Redis, but you need to think about data structures and access paths. In Redis, access paths are not implicitly controlled (as is the case with indexes in RDBMS / MongoDB).

For the above example, you could have:

user:<user hash> -> hash of user properties
user:<user hash>:sent -> set of <msg hash>
user:<user hash>:received -> set of <msg hash>
message:<msg hash> -> hash of message properties

/ *: sent *: receive, , / .

- SMEMBERS, SORT, :

# Get a list of message hash codes only in one roundtrip
smembers user:<user hash>:received

# Get a list of message contents in one roundtrip
sort user:<user hash>:received by nosort get message:*->sender get message:*->message

.:

1: Redis , UUID - ( ), .

2: , . , . , .

+9

All Articles