简体   繁体   English

使用服务器端 firestore 客户端时是否可以选择乐观锁定?

[英]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.

相关问题 Electron Amplify AppSync“在服务器端环境中实时禁用” - Electron Amplify AppSync "Realtime disabled when in a server-side environment" 如何检查客户端或服务器端的 Google Play 订阅是否处于活动状态 - How to check if Google Play Subscription is active on client-side or server-side 仅使用函数修改服务器端对象的元数据 - Modify metadata of object on server-side only using a function AWS S3 中是否有乐观锁定? - Is there optimistic locking in AWS S3? 我可以将 Cognito 与“客户端凭据”流联合(或其他信任在别处验证的服务器端应用程序的方式吗?) - Can I federate Cognito with "client credentials" flow (or other way to trust a server-side application authenticated elsewhere?) 如果我使用服务器端,是否需要 firebase 规则? - Do I need firebase rules if I'm using server-side? Express Amplify Serverless/GraphQL 与 Express 服务器端后端 - Express Amplify Serverless/GraphQL vs Express Server-Side backend .htaccess 服务器端路由和节点环境重定向 - .htaccess server-side routing and node environment redirects 在 GCP 中查找已部署的 Google 跟踪代码管理器服务器端版本 - Finding deployed Google Tag Manager server-side version in GCP 使用服务器的云 Firestore - cloud firestore using server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM