DEV Community

Discussion on: Building a news feed with Firebase

Collapse
 
mehedih_ profile image
Mehedi Hassan

Understood, thanks! I have looked into Redis before, but to be honest I don't want to complicate even more. I just want to implement the best possible performance using Firebase here, but my current implementation seems to be the best way to go (correct me if I'm wrong, please) -- even though it isn't performant or scalable.

Collapse
 
jessefulton profile image
Jesse Fulton

Oh I wasn't suggesting adding Redis, I was just referencing an article that can give you some ideas on how you might approach the problem.

If you really want to optimize for end-user performance, I'd recommend looking at fan-out on write. Fan-out on write essentially guarantees your "feed" reads will become O(1) instead of O(n). Note that your writes, will become O(n), but if you defer the fan-out to a Cloud Function, then the end-user performance doesn't take a hit at all.

Thread Thread
 
mehedih_ profile image
Mehedi Hassan

Yep, my current implementation is already in O(1) since posts from the users a user is following is stored on a their "timeline" field where posts are referenced through their IDs. Because of this when the app actually loads in all the post data on the timeline, it can be a bit slow. Any way I can cache the existing data so the app shows the cached data on initial load, and then loads in the "new" data once that's ready?

Thread Thread
 
jessefulton profile image
Jesse Fulton • Edited

posts from the users a user is following is stored on a their "timeline" field where posts are referenced through their IDs

If the client needs to do an additional "Find Post By ID" call for each ID referenced in the timeline list, then the function getMyTimelinePosts() would be at least O(n). The way around this is to "fan out" copies of the full posts into each user's timeline object. When you make that first call, everything you need for the timeline will be in the response - no additional calls are necessary. Here is another article that helps explain it.

Any way I can cache the existing data so the app shows the cached data on initial load, and then loads in the "new" data once that's ready?

Like this.

Thread Thread
 
mehedih_ profile image
Mehedi Hassan

Thank you for the help :)