Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you use Firebase Transactions

Tags:

I understand that Firebase transactions enable the atomic update of some value, given its old value and new value

But given that Firebase is a realtime database, I assume, one must use transactions only sparingly and not in 'realtime features'

here's an example:

  1. I understand that if you are performing some mathematical operation on a value (adding 'likes' or equivalent), it makes sense to use transaction

  2. I dont understand if it makes sense to use transactions in the following use case: Say a text field can be updated by any number of users, and we are interested in all the updates, as they occur in real time. Does firebase recommend we use transaction in this case? Or is the final 'persist operation' that occurs on the value, only limited to a single 'persist operation' per timestamp granularity of the Firebase server clock?

Further is it guaranteed that the events will be delivered in the order of which the final values were persisted?

like image 273
7hacker Avatar asked Jun 29 '16 22:06

7hacker


People also ask

When should I use Firebase realtime database?

Primarily synchronizing data, with basic querying. If you don't need advanced querying, sorting and transactions, we recommend Realtime Database. Advanced querying, sorting, and transactions. If you need complex interactions with your data, for example in ecommerce apps, we recommend Cloud Firestore.

What is transactional data in Firebase?

Advertisements. Transactional data is used when you need to return some data from the database then make some calculation with it and store it back. Let us say we have one player inside our player list. We want to retrieve property, add one year of age and return it back to Firebase.

What is Firebase good for?

Google Firebase is an application development platform that allows developers to create iOS, Android, and Web apps. Here's why you should use it! Google Firebase offers many features that pitch it as the go-to backend development tool for web and mobile apps. It reduces development workload and time.

What is Firebase advantages and disadvantages?

Firebase doesn't provide the same capabilities for Android and iOS apps. It's still more Android centered and it's the one system that gets most of dedicated services and facilities. For instance, Test Lab can be easily integrated with the Android studio and supports a wide range of Android devices for testing.


1 Answers

Whenever you use transactions on a database, you sacrifice some of your scalability in order to have a stronger data consistency guarantee. Transactions on the Firebase Database are no different, except maybe that developers tend to use Firebase in more highly concurrent situations.

With any transactional system: the key to keeping the system scalable is to minimize the number of clients contending to update the same data. In the case of Firebase, you'd accomplish that by running your transaction as low as possible in your JSON tree. I.e. a counter is a example of something that could work well under a transaction.

For larger pieces of data, such as your text editing example, using a transaction will not scale well. For such use-cases it is better to find a way to avoid the conflict altogether. Quite often this boils down to storing the delta that each user is making, instead of storing the updated state. A great example of this is in the Firepad example, which uses operational transform to create a highly concurrent collaborative editor on top of the Firebase Database.

like image 55
Frank van Puffelen Avatar answered Oct 05 '22 13:10

Frank van Puffelen