1

So the cloud firestore's admin SDK documentation states that a transaction holds a pessimistic lock on all documents being read inside it. Which means that during this locked period (moment from when the document was fetched to when the transaction is committed), any updates to the locked documents outside of the transaction itself will get rejected instead of the transaction being retried.

Now what happens when let's say at time t1 a transaction T1 reads (and thus locks) a document d1, while at a later time t2 another transaction T2 tries to read the same document d1 while the first transaction T1 is still running and is not yet committed?

  • Will the second transaction T2 will just fail upright?
  • The documentation suggests that during the locked period updates to the document will get rejected but not reads. So will that be a case that T2 does not fails upright, but when it tries to perform a write operation on the document d1 it fails as this update operation is outside the transaction T1?
  • If my 2nd assumption is correct, then is it possible to run both transactions T1 and T2 simultaneously on the same document d1 successfully, providing that either T2 does not even modify document d1 (it is only a read transaction for d1) or when it performs write operations on d2, the transaction T1 has already been committed?
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Maaz Bin Khawar
  • 435
  • 3
  • 13
  • I have reviewed the documentation and I haven't found this part about "reads that are not being rejected"... Can you add some links to documentations you are referring to ? – vitooh Jun 02 '20 at 12:21
  • 1
    @vitooh from the fact that documentation uses the term [_holds a pessimistic lock_](https://googleapis.dev/nodejs/firestore/latest/Transaction.html#get), it is evident that external reads of the locked document(s) should not get rejected (as this is a part of the very definition of Pessimistic Lock itself). Please refer to the explanation of pessimistic locking [here](https://en.wikipedia.org/wiki/Lock_(computer_science)) – Maaz Bin Khawar Jun 02 '20 at 13:48
  • thanks for confirmation, I just wanted to ensure we are talking about the same matter.... – vitooh Jun 03 '20 at 15:06

1 Answers1

0

I have tried to find some information and ask few people as well. According to all reads are of course possible as it was mentioned.

So T2 will not fail, if it only reads. It should fail if it will try to update locked document, and than will be retied.

Both transaction should finish successfully, however I think that important thing to mention is that T2 will get data in old form, before changes done by T1.

I tried to find any documentation regarding directly Firestore, however it seems there is no, so I think this is exactly as in Wiki page you have provided (here)

You can find a lot of similar consideration here on SO and over internet. I only mention this and this for me are quite good.

I hope it will help somehow!

vitooh
  • 4,132
  • 1
  • 5
  • 16