ORM 和 mysql 复制滞后

发布于 2024-12-03 20:57:13 字数 627 浏览 1 评论 0原文

我们正在转向主/多从 MySQL 设置,我关心如何在不过多更改应用程序代码的情况下解决数据完整性问题。

我们的应用程序使用 ORM(Doctrine PHP),理论上我们可以将其扩展为简单地将 SELECT 语句发送到从站之一或主站,并向主站发送 UPDATE/DELETE 查询。

由于 ORM 的性质,这会失败:

  1. 根据从属设备中的过时数据进行写入,例如从从属设备中读取最近在主设备上更改了 Y 列但尚未在从设备上更改的记录,然后保存对 X 列的更改以及主服务器上 Y 列的过时数据,从而覆盖对 Y 列的先前更改。
  2. 在主服务器上最近写入之后立即从从服务器进行读取(用户刚刚发布表单的情况 - 比如说)论坛评论 - 以及重新加载页面并且看不到他的评论)

我认为我们应该在应用程序控制器上构建逻辑,该逻辑应该让数据层知道是否可以容忍获取过时的数据以及何时不能容忍(例如,当打算编写)并且当不这样时,数据层可以相应地连接到主站或从站;这似乎违背了良好的软件工程实践。例如,控制器应该实现业务逻辑,而不必处理数据是否来自从设备或主设备。

我相信过去已经处理过这个问题。也许不是使用 Doctrine PHP,而是使用 Hibernate 或其他 ORM 解决方案?

有什么可以分享的一般性良好做法或建议吗?

We are moving to a master/multi-slave MySQL setup and I am concerned about how to tackle the issue of data integrity without changing the application code too much.

Our application uses an ORM (Doctrine PHP) and in theory we could extend it to simply send SELECT statements to one of the slaves or to the master and UPDATE/DELETE queries to the master.

where this would fail due to the nature of the ORM is:

  1. Doing writes based on outdated data from a slave, e.g. read a record from the slave that has recently had its column Y changed on the master but not yet on the slave and then saving a change on column X together with the outdated data of column Y on the master, thus overwriting the prior change to column Y.
  2. Doing reads from the slave right after a recent write on the master (scenario where a user just posted a form - say a forum comment - and reloads the page and does not see his comment)

I am thinking that we should build the logic on our application controllers that should let the data layer know whether there is tolerance for getting outdated data and when not (e.g. when there is an intention to write) and when not so that the data layer can connect to master or slave accordingly, however; it seems against good software engineering practices. E.g. controllers should be implementing business logic not having to deal with whether the data should come from a slave or a master.

I am sure this has been dealt with in the past. Maybe not with Doctrine PHP, but with Hibernate or other ORM solutions?

Are there any general good practices or recommendations one could share?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文