0

I have a node under Firebase for different restaurants which contains a list of active and past visitors. This node is named listvisitors. Under the restaurant node, i want a counter for all the visitors. Here is where I want to use Firebase transactions.

The idea is to increment the counter when a person opens the restaurant page. As there would be contention, transaction seems like a good option.

A popular restaurant can have loads of visitors simultaneously visiting thus leading to contention. Can transactions scale to handle this?

If so, what is the maximum retries that transaction can handle before failing with the max retries error. (Essentially the max simultaneous updates assuming all users log in at the same time)

If not. Is there an efficient alternative to essentially count the number of current children under lists and get the correct number?

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Kushan
  • 5,855
  • 3
  • 31
  • 45

2 Answers2

3

The Firebase client currently aborts the transaction if it hasn't succeeded after 25 tries.

Well before you see that level of contention it'd be wise to change to a different strategy. The common way to solve this problem is to not have each visitor update the common counter, but instead have each of them write a "I visited this" into the database with a push():

ref.child("visits").push().setValue(currentUser.getUid());

Then you can have a server-side process that pulls the visits off the list and that increments the counter. In a stable state that means that the list of visits is empty. At any moment the visits list only contains visits that haven't been counted yet.

The reason this will perform better is that the server-side process will not have any contention for updating the counter. So it doesn't really need to use a transaction. But even if it does use a transaction, it will "never" have to retry.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi frank, thanks for the answer.... Actually the counter serves to show the current active visitors which tells the customers the amount of crowd they would have to face. Unfortunately i can't have a server doing this. Customers add to the list and restaurants remove when they leave. Please do suggest anything that you can think to solve this. – Kushan Nov 22 '16 at 06:22
  • To be honest: it sounds highly unlikely you'll hit contention issues at this time. If you do: my answer shows the alternatives. – Frank van Puffelen Nov 22 '16 at 15:54
0

Maybe Princig FAQ can help you... To the question: What is a "simultaneous database connection"?
This is the answer they provide:

Firebase imposes hard limits on the number of simultaneous connections to your app's database. These limits are in place to protect both Firebase and our users from abuse. The Spark plan limit is 100 and cannot be raised. The Flame and Blaze plans currently have an initial limit of 10,000 simultaneous database connections. This limit isn't the same as the total number of users of your app, because your users don't all connect at once. We encourage you to monitor your peak simultaneous database connections and upgrade if needed. We're working to remove the initial 10,000 simultaneous-connections cap on the Flame and Blaze plans.

About the option to get the current number of children, there is a method numbChildren but it depending on your snapshot... Here are some users discussing about that.

Community
  • 1
  • 1
Anfuca
  • 1,329
  • 1
  • 14
  • 27
  • 1
    I meant simultaneous updates to one particular locations value.... Transactions are meant to solve this concurrency... My question is what amount of concurrency is too much for transactions as it will constantly try to write the data until it finally succeeds without any contention or just gives up with the error i specified. – Kushan Nov 21 '16 at 22:43