Reputation: 589
I'm working on a social media like app. People can create posts, like, comment and so on. For now, most of my operations are handled on the client side. Actually, the only time I use functions is to create a user. Now this might be a dumb idea but it was my first time using firebase when i started working on this app. And tbf most of the things work fine so far. Now that i discovered cloud functions, i was thinking maybe i should move all my CRUD operations to cloud functions. But i’m kinda stuck on which ones should stay on the client. For example, when someone likes a post, i simply add their userId to a liked array on the post's collection. Should such a simple functionality be handled by a cloud function? And what does this do to the user experience since functions take at least 2 seconds to respond? All your inputs are very appreciated.
Upvotes: 0
Views: 391
Reputation: 1858
This is a hard thing to objectively answer, because everyone use case is different. But, since you seem to have experience with GCP, you might start by reading their official guide on choosing the right GCP compute option for your use case:
https://cloud.google.com/hosting-options
What they say about when to use Cloud Functions stands out to me, because it's similar to your use case:
Post a comment on Slack channel following a GitHub commit
As for the 2 seconds causing UX issues, this also means you need to consider each option with respect to your use case and how you think users would expect the system to behave. I recommend consulting UX material to learn about this.
In my opinion, things like posting a comment and loading a lot of data (like a dashboard) are examples of things where I don't mind waiting a few seconds (so Cloud Function slowness wouldn't matter much to me if that's how the system were implemented). There's also the use case of things happening in the background, like telemetry/analytics and syncing data, that I don't even notice (so it's impossible for me as a user to care!).
Also, I'd like to point out to you a way you can handle the delay imposed by a Cloud Function for some actions. You said:
For example, when someone likes a post, i simply add their userId to a liked array on the post's collection. Should such a simple functionality be handled by a cloud function? And what does this do to the user experience since functions take at least 2 seconds to respond?
Think about a user "liking a post". Do they care how long it takes for that action to be acknowledged? If they navigate away from the post back to some main page that displays a list of posts (and a "liked" or "not liked" icon for each of the posts), would they care that the post they just liked is appearing as not liked at that moment (because 2 seconds hasn't yet passed)? Probably not. So that could be a use case where you could get away with eventual consistency.
The idea is that the client would have no way of knowing whether the "like" action has completed, but it won't care. Other users will see slightly stale "number of people liking this" counts on the posts as they browse, and the user who performed the like won't experience any weirdness unless they navigate away from the page less than a few seconds after liking it. Even then, you can just fake that the "like" action succeeded on the page by changing the DOM to the "liked" icon right after the user clicks it (while the request finishes in the background).
Upvotes: 2