我可以从Azure Cosmos DB中读取/加载文档并同时获得对其的写入锁定吗? [英] Can I read/load a document from Azure Cosmos DB and acquire a writing lock on it at the same time?

查看:112
本文介绍了我可以从Azure Cosmos DB中读取/加载文档并同时获得对其的写入锁定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想知道只有一方可以更改文档.我想避免以下情况:当一个工作角色的两个不同实例或不同工作角色的两个实例读取文档的相同状态,然后每个实例将编写自己的修改(可能不同).顺序写入可能很容易接连进行,但这不是我想要的.

如果由于某种原因无法进行写操作,则此写锁应在一段时间后自动失效.

我听到一些谣言说这是可能的,但是我在文档中找不到.

解决方案

AFAIK,至少在撰写此答案时,Cosmos DB不支持您要查找的内容(获取写锁定).今天支持的是 Optimistic Concurrency using ETags 适用于顺序写入,但不是您要找的.

只需大声思考,您就可以使用 Blob存储中提供了Lease 功能,并将其与Cosmos DB写入操作结合使用.

基本上,将要发生的是,您的工作进程将尝试在一定时间内(15-60秒)在Blob上获取租约.只有一名工人能够获得租约.该工作人员将有机会在获得锁的时间内更新Cosmos DB中的文档.另一个工作进程将不得不等待,直到租约到期或第一个工作进程显式破坏了租约.

这与此处描述的Leader Election Pattern有点相似: https://docs.microsoft.com/zh-CN/azure/architecture/patterns/leader-election .

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.

这篇关于我可以从Azure Cosmos DB中读取/加载文档并同时获得对其的写入锁定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆