简体   繁体   中英

Can I read/load a document from Azure Cosmos DB and acquire a writing lock on it at the same time?

Basically I want to know that only one party is going to change the document. I want to avoid situation when 2 different instances of a worker role or different worker roles will read the same state of the document and then each will write its own modification, potentially different. Sequential writes may easily go fine one after another, but this is not what I want.

This writing lock should auto expire after some time if write will not happen for whatever reason.

I heard some rumors that this is possible, but I cannot find this in the documentation.

AFAIK, what you're looking for (acquiring write lock) is not supported by Cosmos DB at least as of writing of this answer. What's supported today is Optimistic Concurrency using ETags that is suitable for sequential writes but that's not you're looking for.

Just thinking out loud, you can however make use of Lease functionality available in blob storage and use that in conjunction with your Cosmos DB write operation.

Essentially what will happen is your worker processes will try to acquire a lease on a blob for certain duration (15-60 seconds). Only one worker will be able to acquire a lease. That worker will have an opportunity to update the document in Cosmos DB for the duration it has acquired the lock. The other worker process will have to wait till the lease has been expired or the 1st worker has explicitly broken the lease.

This is somewhat similar to Leader Election Pattern described here: https://docs.microsoft.com/en-us/azure/architecture/patterns/leader-election .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM