How to perform transaction which includes both firebase real-time database and firestore? - firebase

I'm using firebase Realtime Database and Firestore for my project. We can perform batch operations on firebase Realtime Database using updateChildren() and commit() on Firestore. But I have to perform a batch operation which includes some parts of Realtime Database and some part of Firestore. Is there any way I can do that?

There is no way to run a single transaction across both Cloud Firestore and the Realtime Database.
You'll have to find a way to write the data in two separate transactions, and make sure your code that reads the data is robust enough to handle the case where one transaction has completed while the other hasn't.

Related

How can I write to the Realtime Database, Cloud Storage and Firestore at the same time using transactions? [duplicate]

This question already has answers here:
Couple Firebase Firestore and Firebase Storage calls together into a batch?
(2 answers)
Closed last month.
I'm developing an app in Flutter, I have a method called **CreateUser **which takes as parameters the user information, his profile picture, and a list of strings, I need to save the information in the Realtime Database, the picture in the Cloud Storage and the list in the Firestore.
I would like all these operations to be successful, if one of these should fail then I would like the others to undo the data they wrote. How can I implement the rollback of the other operations? Can I use transactions?
I've tried using transactions but I'm not sure if I can use them on different Databases.
I need to save the information in the Realtime Database, the picture in the Cloud Storage, and the list in the Firestore.
That's indeed possible, by performing one operation, right after another, only when the operation succeeds. For example, as soon as the operation for writing data to the Realtime Database completes, then inside the callback, perform the addition of the image to Storage. As soon as the addition of the image to Storage succeeds, perform the last operation of writing the data to Firestore.
I would like all these operations to be successful, if one of these should fail then I would like the others to undo the data they wrote.
There is no built-in mechanism for that. If you thought you can add to a batch operation, a Realtime Database write operation, a Firebase Storage file upload
and Firestore write operation and be sure that all three are complete, so you can have consistent data, please note that this is not possible. These operations are a part of different Firebase services and unfortunately, at the moment I'm writing this answer there is no way you can make them atomic, meaning all succeed or all fail with an exception.
How can I implement the rollback of the other operations?
You have to write code for that because none of the Firebase products support cross-product transactional operations. To solve this, you'll have to nest the calls during your write/upload operations and handle the error if the next operation fails. This means that you either have to delete the data from the Realtime Database and the file from Storage if the write operation in Firestore fails. Or only delete the data from the Realtime Database if the file upload to Storage fails.
But note, at some point in time, there will be a failure that the client can't roll back one of the delete operations. The most common approach for these inevitable failures which might happen is to make your code robust by handling exceptions and performing occasional cleanups in both places, Firebase Storage and Firestore, considering that the first operation is the one that writes data to the Realtime Database.
As discussed with the Firebase engineers, the reason is quite clear. Even if the Realtime Database and Cloud Firestore are both Firebase products, they are still different products. Besides that, Firebase Storage is a service within Google Cloud. So now, 2023-01-12 there is no way you we can do that. Hopefully, it will be available in the near future.
Can I use transactions?
No, and that's for the exact same reason as above.
One way I might address this, is to use the Firestore document write operation to trigger a Workflow [1] that can handle the three operations and rollback depending on failure state. That way you can also have a constant transaction record follow the process.
If you wanted to provide app feedback say to the user, you could have your app. wait for a DB record of completion (or error) get written and based on that report back to the user.
[1] https://cloud.google.com/firestore/docs/solutions/workflows

How to push new data from Realtime Database to Cloud Firestore?

I am pushing data to Firebase via Zapier and it lands in the Realtime Database. However, I am using Cloud Firestore. I've looked through a lot of documentation for both services but neither seems to have answers.. (there is information on migrating but I would like to keep both DBs).
How would I push each new or updated data entry from Realtime Database to Cloud Firestore?
There is nothing built into Firebase to automatically do this.
The fastest I can think of is to build a Cloud Function that listens for the Realtime Database writes, and then also sends them to Firestore.
Alternatively, doesn't Zapier allow you to have two zaps, one that writes to each database?

Using Firebase Realtime Database node keys as document ID's in Cloud Firestore

I am in the process of migrating my Realtime Database to Cloud Firestore. Ideally, I need to keep the same Realtime Database node keys that have been generated using push() and use it as the document ID in Firestore, but is this safe to do so?
I have read information at https://firebase.google.com/docs/firestore/best-practices and I am still unsure whether this will be safe. I am aware that auto generated document IDs in Cloud Firestore are in a different format to those automatically generated in Realtime Database.
Am I likely to run into problems by using by using Realtime Database generated keys such as: -M_NHw525_IxMqiGPUvd as the document ID in Cloud Firestore?
I really appreciate any help, Thanks.
Firestore is sensitive to hot spots in its writing process, meaning that write throughput is best when the writes are randomly distributed across the address space. In other words: if the IDs of the documents being written, and the values that are being written to the indexes, are randomly distributed.
Firebase Realtime Database push IDs start with an encoded timestamp, so they are definitely not randomly distributed. They are (by design) largely sequential: subsequent calls to push() typically leads to keys that are next to each other. This is exactly what they were designed for in Realtime Database, but it doesn't meet the requirement of a random distribution that is needed for maximizing write throughput in Firestore.
Whether you'll run into problems when using existing push keys for your Firestore writes really depends on the implementation. For example, during a data migration you'll want to be ready to throttle the writes (sooner) when they're not randomly distributed. Hopefully the above helps you to know what to keep an eye out for when performing the data migration.

Does Firebase Function reading and writing to Firebase Realtime Database contribute to Firebase Realtime Download? [duplicate]

This question already has an answer here:
Do firebase cloud functions involve costs for realtime db and firestore?
(1 answer)
Closed 3 years ago.
I am using Firebase Realtime Database to store data for my app. For some server based processing, I am using Firebase Function to do a periodic operation to read the data from the Realtime Database and update it if necessary and write it back to the Realtime Database.
I wanted to ask whether does Firebase Function operation to both read and write back to Firebase Realtime Database will contribute to the Realtime Database download usage (as Realtime Database download usage is charged)?
Reading data from the Realtime Database into your Cloud Function is a charged operation.
Note that this only applies to data that you code explicitly read in your Cloud Functions code. The data that is used to trigger your Cloud Function, and is passed into your code in the snapshot, change, and context parameters, is not charged.
Also see:
Do firebase cloud functions involve costs for realtime db and firestore?

.info/serverTimeOffset for Firestore?

I am using Firebase Firestore and want to get a rough idea of the the server time. Similar to how the Realtime Database had .info/serverTimeOffset. I could create a realtime database just to use the .info/serverTimeOffset endpoint but is there any better way using just Firestore?
There is no equivalent of .info/serverTimeOffset in Cloud Firestore at the moment. That's one of the reasons we recommend using the Firebase Realtime Database for a presence system, even when you use Cloud Firestore for other data storage needs.

Resources