RMK
RMK

Reputation: 1937

How to create latest posts feed based on user's subscriptions

I need to implement latest posts page in my mobile app that uses firebase real-time database.

System description:

Application users (AU) can subscribe to content creators (CC) who create posts. For instance, we have a user AU123 who subscribes to content creators CC1, CC2, CC3. Another user AU456 subscribes CC2, CC6, CC7 What is the most optimal way to perform operation getLatestPosts(AU123)?

In general, it is a very similar functionality that Facebook offers (but way simplified as we only rely on the post creation date to construct the latest list). We have users that follow (subscribe) other users (Content creators). When we open up Facebook, we can see a list of latest posts.

A simple solution would be to simply create "latest" list for each of the users and once CC posts, he also appends a reference to his post to that list. But having 1000 users subscribing to a CC, we would need to append that postID to 1000 lists.

Another approach could be to store all references to the posts in a list along with the timestamp. Then, each user could fetch that list and pick only the posts from CC that his subscribing. This solution is not optimal either as the posts list can quickly get very long and finding even a single post belonging to the subscribed CC can take a while (assuming we have 1000 CC that create 1 post per day and user that subscribes only to few of them).

Do you have any articles or ideas how to approach this problem? Is is even possible to solve in a reasonable way using only firebase database?

Kind regards

Upvotes: 0

Views: 174

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599886

You seem to already have two valid approaches, and are making the right considerations. Both duplicating on write, and client-side joining on read are common and valid.

Most systems that do this at scale, do a hybrid of these two approaches, deciding whether to duplicate or join based on the number of subscribers of the content creator.

One thing I'd like to dispel is that lookups are slow. Many developers new to Firebase think so, but it is seldom the case as Firebase can pipeline most of these requests over its existing web socket connection.

Upvotes: 1

Related Questions