0

I am currently making an expense manager app using firebase realtime database. My data on firebase looks like this :

{ Txns:[{s:"x", amount:1000 }
        {x:"x",  amount:2121 }
        ...
       ]
  Balance: {total:2000(sum of all amounts in txns array)}

}

Now, i want to add all amounts in json array and store it in "balance:". So, should i use google cloud function for this, or i should do the calculation on client device? I know google cloud function is a smart approach but, every time a user enters json object will be added to the array, the google cloud function will run on every single entry updating the balance and google provides some limited reads and write in their free plan. So, should i worry about read write operation count or not ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Nutty Geek
  • 109
  • 2
  • 8
  • 1
    As mentioned by you it would be ideal to use cloud function, as cloud function will run once a entry is added in the firebase, but if it is done on the client end, this operation will be performed by each client, which would obviously require querying all the amount and adding it on each client, which would result in more data consumption. – Praveen Pandey Dec 12 '17 at 17:03
  • 1
    If you want to know if you should worry or not, begin some calculations about what the real cost will be. – Doug Stevenson Dec 12 '17 at 17:04
  • Firebase Realtime Database [pricing](https://firebase.google.com/pricing/) is not based on the number of read or write operations. It is primarily based on the size of the data that is stored and downloaded. Reading just the balance, instead of reading all transactions, will quickly become cheaper as the number of transactions grows. – Frank van Puffelen Dec 12 '17 at 17:36

1 Answers1

0

Firebase Realtime Database pricing is not based on the number of read or write operations. It is primarily based on the size of the data that is stored and downloaded. Reading just the balance, instead of reading all transactions, will quickly become cheaper as the number of transactions grows.

Updating the balance can be done from each client that writes a transaction. In that case you'll want to secure the update of the balance through security rules, ensuring the the balance is updated by the amount in the transaction. And you'll need to use transactions for these updates or (preferably) implement your own client-side retry logic for when the update is rejected. While this is definitely possible, it can get quite cumbersome. I recommend reading my answer here for an example of how to get started: Is the way the Firebase database quickstart handles counts secure?

Alternatively you can make a trusted environment, such as a server you control or Cloud Functions, responsible for updating of the balance. an advantage of this is that you don't have to worry about security rules anymore. But a disadvantage is that this only works when your clients are connected to the server, where the approach with client-side balance updates would work transparently while the client if online (in most cases at least).

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • As per your suggestion, for making it offline compatible also. I will use android client to update balance in firebase database. But, I am unable to get clarity, how will i do that ? coz, in firebase we have event listener, i cannot just request balance when ever the activity is opened. I will have to use event lisetener. And the problem is when i update the balance it will trigger the event listener again. @frankvanpuffelen – Nutty Geek Dec 17 '17 at 10:36