乐观锁定& HTTP If-Match 标头
您在使用 If-Match
在 Web 服务上实现乐观锁定方案方面有何经验?
为了简化事情,假设我们正在构建一个简单的 CMS,仅由“页面”组成,每个页面只有一个主体。如果两个用户同时开始编辑同一页面,则仅提交最后保存的用户所做的更改(或者更确切地说,他们覆盖其他用户所做的更改。)
为了防止这种情况,我计划使用条件 < code>PUT,即获取 ETag
响应标头的值,并将其作为 上
If-Match
标头的值提供PUT 请求。如果该值与基于页面当前状态的值不匹配,则服务器会拒绝 PUT
并返回 412。
我知道一个明显的陷阱,其中可能会在前置条件检查和实际的数据库更新,但这些可以通过事务和排序来补救。
你在实践中尝试过这个吗?这是一个坏主意吗?
What are your experiences on using If-Match
to implement an optimistic locking scheme on a web service?
To simplify things, let's say we're building a simple CMS, consisting solely on "pages", each having just a body. If two users begin editing the same page simultaneously, then only the changes made by the user who were last to save are committed (or rather, they overwrite the ones made by the other user.)
To prevent this, I plan on using conditional PUT
s, i.e. fetching the value of the ETag
response header, and supplying it as the value of the If-Match
header on the PUT
request. If this value does not match the one based on the current state of the page, the server rejects the PUT
with a 412.
I'm aware of the obvious pitfall, in which race conditions can occur between the precondition check and the actual database update, but those can be remedied with transactions and the sort.
Have you tried this out in practice? Is it a bad idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这是 If-Match 的设计目的之一,是的,它按设计工作。
Well, this is one of the things If-Match was designed for, and yes, this works as designed.