Concurrent requests

15 views
Skip to first unread message

Meenakshi Sharma

unread,
Apr 21, 2016, 11:42:07 PM4/21/16
to Ruby on Rails: Talk
Hi all,

I am running into 'concurrent POST requests' issue in the application that I would like to handle.
It's resulting in duplicate entries in database for columns with indexes.

I am thinking of adding unique constraint to the table columns (with index) and handle resulting database errors in the application.

Another way is to lock the row using something like

order.transaction do
        order.lock!
        end
end

I would like to know your thoughts about the 2 approaches. 
It will be great if you could direct me to useful links.

Thank you in advance!

Colin Law

unread,
Apr 22, 2016, 3:45:43 AM4/22/16
to Ruby on Rails: Talk
On 21 April 2016 at 23:21, Meenakshi Sharma <meena...@gmail.com> wrote:
> Hi all,
>
> I am running into 'concurrent POST requests' issue in the application that I
> would like to handle.
> It's resulting in duplicate entries in database for columns with indexes.
>
> I am thinking of adding unique constraint to the table columns (with index)
> and handle resulting database errors in the application.

If you have a uniqueness validation then you should always also
enforce that in the database, for exactly the reason you describe.
See
http://guides.rubyonrails.org/active_record_validations.html#uniqueness

Colin

Greg Navis

unread,
Apr 22, 2016, 7:57:15 AM4/22/16
to rubyonra...@googlegroups.com
Hey!

I think it'd be helpful if you provide us with a little bit of context. What are you trying to achieve?

Basically, you have two lines of defense: the app and the database. Like Colin said, if uniqueness is an essential constraint in your domain then you should enforce it at the database level. You can also use Active Record uniquness validator to display a nice error message (but this w/o the database-level constraint is not enough). If there's a race condition Active Record will raise RecordNotUnique.

If you tell us a bit more we might be able to give you more specific advice.

Best regards
--
Greg Navis
I help small tech companies to scale Heroku-hosted Rails apps.

Reply all
Reply to author
Forward
0 new messages