乐观锁定& HTTP If-Match 标头

发布于 2024-08-24 14:47:12 字数 438 浏览 1 评论 0原文

您在使用 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 PUTs, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭泣的笑容 2024-08-31 14:47:12

嗯,这是 If-Match 的设计目的之一,是的,它按设计工作。

Well, this is one of the things If-Match was designed for, and yes, this works as designed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文