Design Redis Database Table Like SQL

Design Redis database table like SQL?

Redis, like other NoSQL datastores, has different requirements based on what you are going to be doing.

Redis has several data structures that could be useful depending on your need. For example, given your desire for a select * from student where name = 'xxx' you could use a Redis hash.

redis 127.0.0.1:6379> hmset student:xxx id 1 college nnn address xn
OK
redis 127.0.0.1:6379> hgetall student:xxx
1) "id"
2) "1"
3) "college"
4) "nnn"
5) "address"
6) "xn"

If you have other queries though, like you want to do the same thing but select on where college = 'nnn' then you are going to have to denormalize your data. Denormalization is usually a bad thing in SQL, but in NoSQL it is very common.

If your primary query will be against the name, but you may need to query against the college, then you might do something like adding a set in addition to the hashes.

redis 127.0.0.1:6379> sadd college:nnn student:xxx
(integer) 1
redis 127.0.0.1:6379> smembers college:nnn
1) "student:xxx"

With your data structured like this, if you wanted to find all information for names going to college xn, you would first select the set, then select each hash based on the name returned in the set.

Your requirements will generally drive the design and the structures you use.

The Comment&Like design with Redis

Like/no like is just the same as yes/no, true/false, they are only binary information. Binary information can be easily recorded as 1/0. Therefore you should consider using bitmap to record this type of information.

This is a typical Redis bitmap usage, so instead of me copying and pasting these information from a blog, I shall introduce you to one that's been mentioned in the Redis official documentation: http://blog.getspool.com/2011/11/29/fast-easy-realtime-metrics-using-redis-bitmaps/

It is a quite detailed technical blog and come with code examples. I am sure you will find it useful.

Redis store design for traditional select * from where and type query

Redis is not a replacement for SQL databases. They have different purposes.
In Redis you should design based on how you will access the data.

See this SO question.

Also this tutorial by Simon Willison is very interesting even though it has some years.



Related Topics



Leave a reply



Submit