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?
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.
A batch of write operations, used to perform multiple writes as a single atomic unit.
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.
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.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With