简体   繁体   English

PG上的Heroku上的Rails / ActiveRecord真的可以这么慢吗?

[英]Can Rails / ActiveRecord on Heroku with PG really be this slow?

In my Rails-application I need to prevent two people from being assigned the same case. 在我的Rails应用程序中,我需要防止两个人被分配相同的案例。

I do this by looking at an "events" table if the case has already been assigned. 我通过查看“事件”表来做到这一点(如果案例已经分配)。 If not, it's assigned and a row is created saying that it's already assigned. 如果没有,则分配该行,并创建一行说已被分配。

This all works - except that sometimes it doesn't. 所有这一切都有效-有时它不起作用。 If two people try to access the same case at the same time (we're talking within the same couple of hundred milliseconds) it seems like the events-table isn't updated in time. 如果两个人试图同时访问同一案件(我们在同一两百毫秒内通话),似乎事件表没有及时更新。

  1. Case is assigned User1. 案例被分配了User1。 Saved to Events-table. 保存到事件表。
  2. User2 asks to be assigned the same case. User2要求分配相同的大小写。 Check Events-table if this case has already been assigned. 如果已经分配了这种情况,请检查事件表。
  3. The same case is assigned to User2, because the write from #1 hasn't been registered by the time we're checking on #2 相同的情况分配给User2,因为在我们检查#2时尚未注册来自#1的写入

I'm running Rails 5.0.2 with Postgres on Heroku (Standard-2X dyno and a Standard-2 Postgres). 我在Heroku(Standard-2X dyno和Standard-2 Postgres)上运行带有Postgres的Rails 5.0.2。

Unfortunately it's not that uncommon for things like this to happen. 不幸的是,发生这种情况并不少见。 If you want to avoid resource contention between multiple threads/requests, you can use ActiveRecord Locking to lock the Case record in question. 如果要避免多个线程/请求之间的资源争用,可以使用ActiveRecord锁定来锁定有问题的Case记录。

I personally prefer pessimistic locking because it works out-of-the-box and in my opinion that's what those DB locks are for. 我个人更喜欢悲观锁,因为它是开箱即用的,我认为这就是那些数据库锁的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM