49354

Reasons for using transactions in Firebase Database over normal writes

Question:

The Firebase documentation states that transactions are used in scenarios where a field might be needed to update frequently(Like a counter,for example.) But I don't get the point, Why cant I just read the data from the database using a listener and write data to the node? I know it's very sketchy but I wanted to know what are the drawbacks of using this approach.

Answer1:

Imagine a situation where two clients are both trying to update some count in the database at the same time. We'll call them Client A and Client B. Their behavior is ordered like this:

<ol><li>Client A reads a count with value 1</li> <li>Client B reads a count with value 1</li> <li>Client A wants to increment the count. It writes a count of 1+1=2 back to the database.</li> <li>Client B wants to increment the count. It writes a count of 1+1=2 back to the database.</li> </ol>

In this situation, the count <em>should</em> be 3, but instead a value a of 2 remains in the database. The count is obviously wrong now, and a transaction would have prevented the problem by forcing the writes in a particular order, and forcing the second client to re-read the count after the first client updated it. This means one of the clients updates from 1 to 2, and the second client updates from 2 to 3. Using a transaction is the only way to ensure correctness in this situation.

Recommend

  • How can I make my form field have a background image?
  • Azure Mobile Services Scheduled Delete From Table
  • pyspark substring and aggregation
  • Delete ARP entry on Windows CE 6.0 CF.NET 2.0
  • Do custom action after devise model confirmation
  • Acts as Tree with Multiple Models
  • Show HTML user input, security issue
  • ResponseBuilder is not working when used with entity object
  • How to resend request with android volley when not authorized
  • Better file search algorithm than creating a list of files
  • Removing event listeners on automatically created multiple elements
  • On which thread JavaFX change listeners are executed ?
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • C++/CLI Thread synchronization including managed and unmanaged code
  • Java making confirming exit
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Simple linked list-C
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • CakePHP ACL tutorial initDB function warnings
  • Alternative to overridePendingTransition() - Android
  • Recording logins for password protected directories
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • How to add date and time under each post in guestbook in google app engine
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • what is the difference between the asp.net mvc application and asp.net web application
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Calling of Constructors in a Java
  • SVN: Merging two branches together
  • Transpose CSV data with awk (pivot transformation)
  • coudnt use logback because of log4j
  • embed rChart in Markdown
  • How does Linux kernel interrupt the application?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app