-3

TL;DR

Is there any benefit to batched writes over transactions other than offline availability?
That is assuming that transactions are safer than batched writes.

Why transactions

I am thinking about migrating Cloud Functions code from batched writes to transactions. The initial idea was that it would be safer.
I was worried that the Cloud Function would not catch when a batched write failed and it is crucial that all of the writes (changing many documents accross different collections) are actually applied.

I read this answer and was glad to hear the following:

If you don't need or want to be able to write while offline just use a regular transaction: it will handle checking whether or not you're online and will fail if you're offline. You're not obligated to read anything in the transaction.

This basically says that the only point of batched writes is offline availability. This confirmed my initial thoughts, however, I also read documents.

Why batched writes

The "Transaction and batched writes" documentation points out that batched writes "have fewer failure cases than transactions".

Confusion

Now, I am slightly confused. The only valid concern regarding transaction failure for my case was the following:

The transaction read a document that was modified outside of the transaction. In this case, the transaction automatically runs again. The transaction is retried a finite number of times.

As the write operations that are used my Cloud Functions are exclusively update operations, I am wondering if this even applies to my case, i.e. if the batched write would ever encounter such an issue. The way I imagine it, the batched write just updates, say, a thousand documents and all of these updates only modify a single field.

When the transaction does the same, when would this failure ever occur?
And since I am using Cloud Functions, which I know is always online, would batched writes ever encounter any kind of issue?

+ Transactions: Safer, will not fail
+ Batched writes: Fewer failure cases, will not fail

Can you see what my confusion is from this?
Regarding simplicity of code: I think batched writes looks more concise, however, it is actually more code in my case. Hence, it does not matter to me.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • Transactions and batched writes serve different purposes and I don't think can be directly compared. While they are both atomic, if you need to read, that eliminates a batched write entirely. If you want to increment a counter, that would only be possible in a transaction. However, if you need offline writing, batch writes are the only way to go. As is, the question is pretty broad; it's not a bad question but would require a lot of dialog. If you can condense the question and present a simplified, specific use case, there will be a higher chance it will be answered in a way that's applicable – Jay Jun 17 '19 at 21:22
  • @Jay Hope this helps: https://stackoverflow.com/q/56643115/6509751 – creativecreatorormaybenot Jun 18 '19 at 06:43

1 Answers1

0

Transactions are not really comparable to batch writes. You should pick the tool that suits the job at hand. The only thing they have in common is that when the operation completes, all the documents will write at the exact same moment (they are atomic). They can also both fail due to violation of a security rule, like any other operation coming from a client app.

Here's how you decide:

Batch writes

  • Work offline, will be synchronized later
  • Don't pay attention to any current document values, can't safely modify field values
  • Doesn't require any document reads to commit
  • Doesn't fail due to contention, each write will simply clobber prior writes on the same document

Transactions

  • Don't work at all offline
  • Pays attention to current document values, can be used to safely modify a document based on existing field values
  • Requires a document read in order to write updates
  • Can fail due to too much contention on documents in the transaction
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks, that cleared up some things. One last thing: do document updates reset updates that happened in between reading and writing? An example: I have a document, I read that for a batched write. Now, some other client updates field α. After that, I now perform the batched write and **only** modify field β. Will field α have the initial value (reset by the batched write) or the value that was updated in between from another source? – creativecreatorormaybenot Jun 18 '19 at 05:20
  • 1
    An update never overwrites fields that were not specified in the update. – Doug Stevenson Jun 18 '19 at 16:38