Search code examples
databaseredisfeedtimeline

How to implement user feed like in Twitter or Facebook on redis


I'm going to write simple news site on redis with supporting followers. I can't imagine how can I organize users timeline like in twitter. I read about Retwis ( http://redis.io/topics/twitter-clone ), but its feed creating method seems stupid. What if I want to remove entries? I'll should to remove all entry references from followers feeds. What if I already do not follow some users?


Solution

  • There are several ways to attack what you describe using a bit of imagination, here are some examples that address your questions:

    What if I want to remove entries?

    One could mantain a set such as post:$postid:users for each post, holding all the userids that may have the post in their feeds; when the post is to be deleted one just has to extract all members from this set and iterate through the ids to remove it from each uid:$userid:posts set; speaking of which you would have to turn that last one into a set instead of a list like the original article suggests in order to be able to extract and remove individual items but that is trivial, the logic is pretty similar.

    What if I already do not follow some users?

    When the feed is being generated for each individual user you have to necessarily iterate and read each post:$postid key, from which you have access to the author userid; so before showing the post you read this id and look it up in the uid:$userid:following set, if it's there we show the post, if it's not we delete it from uid:$userid:posts and don't show it.

    In a nutshell, this is what you have to keep in mind in order to build this kind of logic in redis:

    • You'll need many commands, but that's ok, Redis is supposed to be fast enough to handle it well.
    • Data will repeat, but that is also ok; it may look insane for someone with a relational DBMS background to store a set of users for each post if each user already has a set with their posts, but this is the only way around building relationships in a non-relational data store like redis.
    • Generally speaking think of sets and sorted sets when designing something relational in Redis.

    With redis you get to do everything yourself, but once you get your head around it it's actually pretty powerful.