skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search