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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Ron7
  • 395
  • 3
  • 7
  • 18
  • I don't think firebase do that. It won't lock the data for you. – Joshua Aug 01 '16 at 01:01
  • 3
    Take a look at [firebase transactions](https://www.firebase.com/docs/android/guide/saving-data.html#section-transactions) – André Kool Aug 01 '16 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. – Frank van Puffelen Aug 01 '16 at 10:33
  • The guide to transactions in the new (non-legacy) documentation is [here](https://firebase.google.com/docs/database/android/save-data#save_data_as_transactions). – Bob Snyder Aug 01 '16 at 16:29

2 Answers2

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

André Kool
  • 4,880
  • 12
  • 34
  • 44
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.