7

I was thinking how would one implement stackoverflow using redis. The hardest part with this is how i should do comments and have it in a way i don't loop to get each comments upvotes. I decided to modify the binary chunk. UVComment is kind of complex.

Everytime i was thinking of using hashes i ended up using sets or list. I'm pretty new to redis so i won't be surprised if i make a mistake. Is this well designed?

 CreatePost/Question (same thing except a flag is set and tags are empty in answers

postId=incr postCount
MULTI
rpush p:postId bin[IsQuestion,authorId,title,body,tags,datetimestamp]
foreach tag in tags
    sadd tags:tag postId
EXEC

 UpdatePost/Question

WATCH p:postId  //dont want a new revision in the meantime
old=lrange p:postId -1 -1 //current but now old
MULTI
rpush p:postId bin[IsQuestion,authorId,title,body,tags,datetimestamp] //new revision
foreach tag in old.tags
    srem tags:tag postId
foreach tag in tags
    sadd tags:tag postId
EXEC

 Upvote/Downvote Post

//up
sadd pUV:postId user
srem pDV:postId user
//down
sadd pDV:postId user
srem pUV:postId user
//undo up/down
srem pUV:postId user
srem pDV:postId user

 Comment:

commentId=incr commentCount
multi
SET comment_post:commentId postId //for later use when we flag comments. We'll need to know where in the db it is
RPUSH post_comment:postId bin[authorId,text,HasBeenEdited,datetimestamp,commentId, UVCount]
exec

 UVComment

watch commentUV:commentId
res=SISMEMBER commentUV:commentId userId
if res>0 //already upvoted
    unwatch
    return
watch post_comment:postId
lrange post_comment:postId 0 -1 //then we find which index matches our commentId
//modify UVCount
MULTI
lset post_comment:postId targetIndex modifiedData
sadd commentUV:commentId userId
EXEC

GetPostLogic

lrange p:postId -1 -1 //current revision
scard pUV:postId
scard pDV:postId
comments=lrange post_comment:postId 0 -1 //get all comments
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
  • 3
    See http://stackoverflow.com/questions/10137857/is-redis-just-a-cache/10140190#10140190 – Sripathi Krishnan Sep 01 '12 at 03:48
  • 1
    @SripathiKrishnan: wow completely different then what i wrote and very meant to be a quick show-what-it-can-do then real example. There isn't a single read/write in there. I never thought about questions by upvotes thats pretty cool. Although its not useful to me bc it shouldnt show any flaw in my example which is meant to be more like a real (but simple) implementation. –  Sep 01 '12 at 09:23
  • 3
    The issue here is that you would **not** want to implement SO in Redis. I'm not saying this isn't a good exercise for learning or showing the power of Redis, but contrary to what Sripathi says, a website like this should not live in memory. Memory is much more scarce and more expensive than disk space, sure it will work at first, but you would have to start horizontally scaling much more quickly. It is simply not a cost effective option. @SripathiKrishnan, I am curious to hear your argument for why this is a good idea. I would love to be proven wrong. – Andrew Guenther Oct 03 '12 at 16:23
  • 2
    @AndrewGuenther No, I won't defend keeping all of stackoverflow in memory ;) In the answer I linked, I wanted to demonstrate the capabilities of Redis. And I believe this question was asked more as an exercise in theory, and so I simply linked the two. – Sripathi Krishnan Oct 03 '12 at 17:55
  • Awesome, I was a bit scared for a second. But I totally agree, this is a great example of how powerful Redis can be. – Andrew Guenther Oct 03 '12 at 20:11
  • @AndrewGuenther funny, i got (kind of) chewed out by saying this (a follow up) https://groups.google.com/forum/?fromgroups=#!topic/redis-db/gmeCzRjwwdE and definitely by this (first post) https://groups.google.com/forum/?fromgroups=#!topic/redis-db/s7p4WL4QfbY –  Oct 03 '12 at 21:19
  • I am sorry for the way the Redis community responded to your questions. I haven't been active in the group lately. Though it seems like you have acknowledged this in one of your posts, but this kind of application is not what Redis is designed to do. It is much closer in usage to something like Memcached, something that you would use in tandem with a traditional RDBMS. So I am curious why you are continuing to pursue the possibility of using it as a primary database. Would you mind elaborating on why you are still pursuing this? – Andrew Guenther Oct 03 '12 at 21:41
  • @AndrewGuenther I posted this question while learning redis and wanted an example. At the time i didnt know VM was bad/being removed and was hoping another nosql db supported datatypes like redis does. No other does :x. But if i could use the syntax as a regular db i wonder if the above is correct and well designed. –  Oct 03 '12 at 22:51
  • Although their DB machine has 48gb of ram. Their torrent shows 3700mb compressed (7z). I suspect they can have everything entirely in ram. But i do think if i have a 512mb VPS I should be able to run a forum and not worry (ram will be gone if i use redis, disk will fine if i went with mysql). Specifically after a year or two with active users. Also i see no easy way to incrementally backup redis to my harddrive. But i'm not saying i have setup mysql binary logs yet –  Oct 03 '12 at 22:54
  • I recommend looking into PostgreSQL. It has a lot of NoSQL-esque features, but remains an RDBMS. Also, I didn't notice that this question was asked before you made those posts to the Google Group. My bad. – Andrew Guenther Oct 03 '12 at 22:57
  • @AndrewGuenther lol thats fine. Why would i use PostgreSQL? BTW should we take this to chat? I have a quick two questions for you. One is why do i want to try PostgreSQL? even if it has NoSQL-esque features? No db i have seen has datatypes like redis. The other question is http://stackoverflow.com/questions/12680922/what-are-some-reasons-to-use-nosql but that is closed –  Oct 03 '12 at 23:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17525/discussion-between-acidzombie24-and-andrew-guenther) –  Oct 03 '12 at 23:08
  • Here's pretty relevant link about having Redis as a primary storage: https://muut.com/blog/technology/redis-as-primary-datastore-wtf.html. – saaj Sep 25 '14 at 16:48

0 Answers0