Nick Messick
Nick Messick

Reputation: 3212

In Rails, will database locking ensure only process is checking a attribute value at one time without being a giant bottleneck?

Our setup is Rails 3 with a 6 app servers behind a load balancer and one PostgreSQL database.

In our app, and user can "tip" and artist during a performance.

The process flow looks like this:

  1. User clicks on "tip" button
  2. Tip object is created
  3. An after_create callback makes sure user account has enough money, if so a financial transaction moves money. Else, a Rollback exception is raised.

What can happen is that if the user "spams" the tip button, multiple tips can be in-process at once. When this occurs the "does this user have enough money?" check returns the same value for many tips since the financial transaction have not happened yet.

What I need is to make sure each "tip" gets process sequentially. That way, the balance check for tip #2 does happen before tip #1 updates the balance.

We're already using Resque for other stuff, so that might be one solution. Although I don't know of a way of making sure multiple works don't start processing jobs in parallel and cause the same issue. Having one worker do tip jobs would not be viable solution as our app processes a lot of tips at any given instant.

Upvotes: 0

Views: 144

Answers (1)

kgrittn
kgrittn

Reputation: 19471

If you enforce this within database transactions it is a fairly simple problem to solve.

http://www.postgresql.org/docs/9.1/interactive/mvcc.html

Upvotes: 3

Related Questions