Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore BatchWrite vs Transactions

After following this firestore document, it's pretty clear that both batch writes and transactions are a great for grouping multiple operations.

Although i couldn't figure what would be the best practice for my use case.

This is my batch write:

val batch = db.batch()

        val docRef = db.collection(CHATS_COLLECTION).document()
        chat.id = docRef.id

        return Observable.create { emitter: ObservableEmitter<String> ->
            batch.set(docRef, chat)
            batch.update(db.collection(USERS_COLLECTION).document(userId), "chatIds",
                FieldValue.arrayUnion(db.document("$CHATS_COLLECTION/${docRef.id}")))

            batch.commit()
                .addOnSuccessListener {
                    emitter.onNext(docRef.id)
                    emitter.onComplete()
                }
                .addOnFailureListener { e ->
                    emitter.onError(e)
                }
        }

I'm creating new chat document and at the same time updating my user document "chatsIds" array with the newly created document using batch write.

What could be drawbacks in replacing batch write with transaction?

like image 522
Juvi Avatar asked Jul 22 '19 18:07

Juvi


People also ask

What is a firestore transaction?

There are two types of atomic operations in Cloud Firestore: Transactions: a transaction is a set of read and write operations on one or more documents. Batched Writes: a batched write is a set of write operations on one or more documents.

What is firestore batch?

A batch of write operations, used to perform multiple writes as a single atomic unit.

What is considered a write in firestore?

According to Firebase pricing, writes are defined as: You are charged for each document read, write, and delete that you perform with Cloud Firestore. Charges for writes and deletes are straightforward. For writes, each set or update operation counts as a single write.

How many data types does cloud firestore support?

Note: Cloud Firestore supports a variety of data types for values: boolean, number, string, geo point, binary blob, and timestamp. You can also use arrays or nested objects, called maps, to structure data within a document.

What is the difference between a FireStore transaction and a batch?

Each operation in the batch counts separately towards your Firestore usage. Like transactions, batched writes are atomic. Unlike transactions, batched writes do not need to ensure that read documents remain un-modified which leads to fewer failure cases. They are not subject to retries or to failures from too many retries.

What is a batched write in FireStore?

Batched Writes: a batched write is a set of write operations on one or more documents. Each transaction or batch of writes can write to a maximum of 500 documents. For additional limits related to writes, see Quotas and Limits. Using the Firestore client libraries, you can group multiple operations into a single transaction.

What is a a transaction in Cloud Firestore?

A transaction consists of any number of get () operations followed by any number of write operations such as set () , update (), or delete (). In the case of a concurrent edit, Cloud Firestore runs the entire transaction again.

What is the difference between a transaction and a batch write?

A batched write can contain up to 500 operations. Each operation in the batch counts separately towards your Cloud Firestore usage. Like transactions, batched writes are atomic. Unlike transactions, batched writes do not need to ensure that read documents remain un-modified which leads to fewer failure cases.


2 Answers

It would get slower without any added benefit. Batched writes are perfect for your case: you want to update multiple places in your database without any reads.

If you had reads, then it would make sense to use a transaction. In your case, a batch write is the correct choice simply because it runs faster.

like image 68
Gazihan Alankus Avatar answered Oct 21 '22 17:10

Gazihan Alankus


Both batch and transactions are will execute atomic manner.Batch write is suitable if you are not reading data from document while updating the document and transaction will be suitable if there is a reading before updating the document

like image 6
pepe Avatar answered Oct 21 '22 16:10

pepe