Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realtime Database Firebase "All or nothing" transactions

Is there a way to manipulate the transactions API in the RTDB for "batch" writes?(We currently can't migrate to Firestore)
Our problem is as follows:
When writing a new "Job" object to the server we are preforming three consecuteve writes:

  1. Writing the location key using the GeoFire API
  2. Writing the job
  3. Connecting the Job ID to the user created it

Unfortunately, if one of the writes fails but a previous one succeeded, it causes major bugs in the system.
We are looking for ways to ensure the completeness, or reverting the operation if one of the writes fails.

like image 503
DsCpp Avatar asked May 21 '18 07:05

DsCpp


People also ask

How do I optimize my Firebase Realtime Database?

Clean up unused dataPeriodically remove any unused or duplicate data in your database. You can run backups to manually inspect your data or periodically back it up to a Google Cloud Storage bucket. Also consider hosting stored data through Cloud Storage for Firebase.

Is Firebase Realtime Database NoSQL?

The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in realtime. NEW: Cloud Firestore enables you to store, sync and query app data at global scale.

What is the limit of Firebase Realtime Database?

256 MB from the REST API; 16 MB from the SDKs. The total data in each write operation should be less than 256 MB. Multi-path updates are subject to the same size limitation.


1 Answers

If it is just a matter of writing and "ensuring the completeness, or reverting the operation if one of the writes fails", you can use a simultaneous update by using the update() method.

As detailed in the documentation, "simultaneous updates .... are atomic: either all updates succeed or all updates fail.". See this part of the documentation: https://firebase.google.com/docs/database/web/read-and-write#update_specific_fields

Note that the Real Time Database also offers the possibility to save data as transactions, see the documentation items here and here. But probably, in your case, using simultaneous updates will be sufficient.

like image 138
Renaud Tarnec Avatar answered Nov 12 '22 07:11

Renaud Tarnec