pedrommuller
pedrommuller

Reputation: 16056

Firestore read/write vs cloud function read/write

I'm using Firestore I have these questions, regarding how to user behavior will have an impact on app costs:

what's is more cost-effective:

questions:

Upvotes: 1

Views: 1235

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317467

what's is more cost-effective:

  • To use a realtime form that saves in the database while the user typing in a web form

This is going to cost you a write for each time the form is save in realtime.

  • To save all the fields in the form at once using a firebase function

This is going to cost you a single write.

The difference in cost between the two should be obvious - multiple writes vs. a single write.

questions:

is it overkill to proxy with cloud functions? (just to avoid costs)

If you're proxying for no other reason than to save costs, it's overkill. The function invocation will cost you money, in addition to the document write, which will cost the same no matter where it originates.

  • when the user types (realtime updates) is it considered as a new write to the database every time?

As I said before, yes, it is.

The only real reason to send form submissions through a function is the ability to do deep, secure checking for validity of the form fields. Client side checks are not secure. You could use security rules to perform checks, but those are limited. If you need to make sure the form fields have stricly checked values, a Cloud Function might be your best choice. But it's not possible to tell given the information in your question.

Upvotes: 4

robsiemb
robsiemb

Reputation: 6354

There's no particular reason you need to use a function to save all at the same time -- at whatever point you would call the function, instead call a single update to the database. Using a function here is going to be strictly more expensive (assuming it provides no functionality other than the database write), since you incur the cost of the write and you incur the cost of a function execution.

Of course, its possible you have some other reason to call a cloud function to do the write beyond a simple proxy -- such as to ensure constraints that cannot be enforced by security rules alone. In that case, the cost may be worth the added functionality.

As for is it better to batch or write in real time, it will certainly be cheaper to write all at once, as you are charged for every document write to Firestore. More specifically, each set or update is charged as a single write. So, its definitely going to be less expensive to only write the document once for many fields, as opposed to write it in real time (or per field) as the user is entering data.

Upvotes: 2

Related Questions