I would like to build an app that will consist of a user feed made of collections of questions, videos, general notes and other object
How to avoid slowness issues? How can it be designed to be as close as possible to the experience most users are used to in today's social apps
Thank you
Definitely read @leebyron 's answer to How to build an activity feed like Facebook.
In addition to the above answer, I would like to give you a basic idea about how to architect such a system.
Before starting out here are two things you should be aware of :
Imagine you have 100 users in your system. Each user will have a personalised feed which will display items from their areas of interest. You can easily implement such a system by having one channel for every user. So, 100 users means 100 different channels. When someone posts a new item (video, article etc), you need to push it into the relevant channels. Now when your users visit their feed, you will go to their channel and fetch the items. This is called "Fan out on Write" because we are pushing items to various channels at the time of writing. The other way is "Fan out on Read", but personally I feel "Fan out on Write" is easier to set up and get started with.
In Redis you can store key/value pairs. So, when a user joins your platform you will ideally store a new key in Redis . The key may look something like this :
user:<userId>:feed. The middle part represents the user's id so that you can easily look up the feed when needed. For example, the key for a user with id112will beuser:112:feed.In Redis, each key has a type. In our case, we need a Set or Sorted Set to store our feed items.
user:112:feed = [321,322,123,098 ... ]As you can see, each item in our set is a post id. Whenever you need to push some content into users' feed you will just add post id to the Set. The above example uses a simple Set. In a real world app you will probably use a Sorted Set so that you can attach score to each item in the feed and sort them accordingly.
While reading from feed you will just fetch the
idsand make a database call to convert theidsinto full blownpostobjects.As your app grows further you may face scalability issues, but this arrangement should get you started easily!
Let me know if you have any questions.