Rails 3 locking auction engine tables

I am creating an auction system in Rails 3 (using PostgreSQL as a database).

Imagine you have a Product. The product has many offers.

I’m worried about what happens when two different users click β€œBid” at the same time (assuming I have at least 2 application servers and they get to the servers at the same time). I have two possible acceptable behaviors for this:

  • one of them wins and the other receives an error message
  • one of them β€œwins”, and the other bid is created next (each bet adds 0.01 euros to the price). Thus, all simultaneous bets are repeated until they are created

So my question is: how to deal with this problem on Rails 3? I think using regular transactions is not enough or is it?

0
source share
1 answer

Rails pessimistic locking should allow you to achieve what you want. As far as I know, this will allow you to handle the issue of ordering updates in the database without errors.

Here is a thread that better explains two different types of blocking: Optimistic and pessimistic blocking

+3
source

All Articles