[英]Is optimistic locking an option when using a server-side firestore client?
So if you read the docs here , which says that:所以如果你在这里阅读文档,它说:
The server client libraries (C#, Go, Java, Node.js, PHP, Python, Ruby) use pessimistic concurrency controls to resolve data contention.服务器客户端库(C#、Go、Java、Node.js、PHP、Python、Ruby)使用悲观并发控制来解决数据争用。
What I don't get, is the use case where I would use a Firestore client on the server side and implement an UPDATE endpoint
.我没有得到的是我将在服务器端使用 Firestore 客户端并实现UPDATE endpoint
的用例。 To avoid data races on this endpoint, we can use a Firestore transaction
but can I also implement/consider an optimistic locking by adding a Version
field on a document?为了避免此端点上的数据竞争,我们可以使用Firestore transaction
,但我是否也可以通过在文档上添加Version
字段来实现/考虑乐观锁定?
I've found it's not possible with Firestore to do this in a single operation like a SQL "update where" statement.我发现 Firestore 不可能在单个操作中执行此操作,例如 SQL“更新位置”语句。 Instead, you will have to get the document, check the value, then update the value if the condition is met.相反,您必须获取文档、检查值,然后在满足条件时更新值。
Yes, that's correct.对,那是正确的。 You cannot perform such an operation in a single go. You have to perform the search, and once you receive the documents, you can then update them.您不能在单个 go 中执行这样的操作。您必须执行搜索,一旦收到文档,您就可以更新它们。
So I understand that the only way to avoid data races is to use transactions when using a server-side Firestore client.所以我明白避免数据竞争的唯一方法是在使用服务器端 Firestore 客户端时使用事务。
That's also correct.这也对。 So when it comes to atomically updating Firestore documents in a multi-user environment, then indeed a transaction is required.因此,当谈到在多用户环境中自动更新 Firestore 文档时,确实需要一个事务。 But take into consideration that a transaction requires a round trip to the server, meaning that you are first reading the document and then updating it.但要考虑到事务需要往返服务器,这意味着您首先要阅读文档然后更新它。 If you however want to update a numeric field type, no matter what the value on the server is, for example, a counter, then you should also take into consideration using FieldValue.increment(n) , which is also an atomic operation, which requires only a single write.但是,如果您想要更新数字字段类型,无论服务器上的值是什么,例如计数器,那么您还应该考虑使用FieldValue.increment(n) ,这也是一个原子操作,它只需要一次写入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.