12

I'm keeping track of a count that users update on the Firebase database through an Android app. The way it works right now is that upon interaction the user's app looks up the current count on the database (using a addListenerForSingleValueEvent() and onDataChange() method defined within the new ValueEventListener) and adds one to it and then sets the count to this new value using mRef.setValue() where mRef is the reference to the database.

The issue I'm worried about is what would happen if a large number of users interacted with the database together at the same time; does Firebase take care of making sure that the value is read and incremented properly or is there a lot of overlap and potentially a loss of data because of that.

4
  • I don't think firebase do that. It won't lock the data for you.
    – Joshua
    Commented Aug 1, 2016 at 1:01
  • 3
    Take a look at firebase transactions Commented Aug 1, 2016 at 10:07
  • @AndréKool can you write that up in an answer with a little sample (probably straight from the docs you linked)? That would allows us to show that the question is properly answered. Commented Aug 1, 2016 at 10:33
  • The guide to transactions in the new (non-legacy) documentation is here.
    – Bob Snyder
    Commented Aug 1, 2016 at 16:29

2 Answers 2

16

When working with complex data that could be corrupted by concurrent modifications, such as incremental counters, Firebase provides a transaction operation.

You give this operation two arguments: an update function and an optional completion callback. The update function takes the current state of the data as an argument and will return the new desired state you would like to write.

For example, if we wanted to increment the number of upvotes on a specific blog post, we would write a transaction like the following (Legacy code):

    Firebase upvotesRef = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes");
upvotesRef.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData currentData) {
        if(currentData.getValue() == null) {
            currentData.setValue(1);
        } else {
            currentData.setValue((Long) currentData.getValue() + 1);
        }
        return Transaction.success(currentData); //we can also abort by calling Transaction.abort()
    }
    @Override
    public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
        //This method will be called once with the results of the transaction.
    }
});

Legacy source

New firebase version source

4
  • Thanks for the answer and the links!
    – Ron7
    Commented Aug 1, 2016 at 17:04
  • How do you use runTransaction for multiple separate nodes that need to be updated at the same time? Right now I am using updateChildren but I am thinking there might be concurrency issues with it Commented Dec 22, 2016 at 1:25
  • I've been working out this issue for days now. Good thing I found this. Thank you! Commented Jan 3, 2018 at 14:49
  • i am getting the final data in onComplete() method that I want. Thank You
    – DeePanShu
    Commented Mar 28, 2020 at 13:21
0

Firebase database handles up to 100 simultaneous real time connections to your database if your are using their free plan but once the 101st users connects to your database the database would stop responding and would display the values that were last edited. Firebase is really good at handling real time connections simultaneously so it depends on your pricing plans. If you want to use the database for free, there will be no issues handling 100 connections but if you want to handle more users use their generous pricing plans.

1
  • Wrong answer... The problem is not about "100 simultaneous real time connections" If you don't implement with transactions you will encounter a problem although less than 100 real time connections. Commented Nov 29, 2023 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.