I have a Rails 3 app that inserts user inputs into the the database.
Every time a user clicks the submit button, all fields(name, email, phone number, etc) will be inserted into the database. But what if two users submit the form at the EXACT same time, will the database run into a deadlock state?
I found that rails 3 has a threaded mode which is to change config.threadsafe! in production.rb. But I am not sure whether this will also take care of the database deadlock problem.
Is there a way to resolve or avoid this?
Thank you!
At the database level, there is no danger that the Database will enter deadlocked state. Databases like MySQL are extremely robust, and even 1000s of updates per second will not result in a deadlock.
However, it might be worth using Active Record’s Optimistic locking support to alert the second user that the record has changed since they started editing it.
The following example from the above linked documentation should help explain this:
As an aside
config.threadsafe!is used to configure compatible servers that they can use threaded-concurrency to server requests, rather than the more-common-to-Rails process concurrency. Aaron Patterrson has a writeup on threadsafe! that’s worth reading.