ORM 和 mysql 复制滞后
我们正在转向主/多从 MySQL 设置,我关心如何在不过多更改应用程序代码的情况下解决数据完整性问题。
我们的应用程序使用 ORM(Doctrine PHP),理论上我们可以将其扩展为简单地将 SELECT 语句发送到从站之一或主站,并向主站发送 UPDATE/DELETE 查询。
由于 ORM 的性质,这会失败:
- 根据从属设备中的过时数据进行写入,例如从从属设备中读取最近在主设备上更改了 Y 列但尚未在从设备上更改的记录,然后保存对 X 列的更改以及主服务器上 Y 列的过时数据,从而覆盖对 Y 列的先前更改。
- 在主服务器上最近写入之后立即从从服务器进行读取(用户刚刚发布表单的情况 - 比如说)论坛评论 - 以及重新加载页面并且看不到他的评论)
我认为我们应该在应用程序控制器上构建逻辑,该逻辑应该让数据层知道是否可以容忍获取过时的数据以及何时不能容忍(例如,当打算编写)并且当不这样时,数据层可以相应地连接到主站或从站;这似乎违背了良好的软件工程实践。例如,控制器应该实现业务逻辑,而不必处理数据是否来自从设备或主设备。
我相信过去已经处理过这个问题。也许不是使用 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:
- 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.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论