[Ruby on Rails (RoR)] Optimistic Locking in Rails

Optimistic locking

Optimistic locking allows multiple users to access the same record for edits, and assumes a minimum of conflicts with the data. It does this by checking whether another process has made changes to a record since it was opened, an ActiveRecord::StaleObjectError exception is thrown if that has occurred and the update is ignored.

Check out ActiveRecord::Locking::Pessimistic for an alternative.

Usage

Active Record supports optimistic locking if the lock_version field is present. Each update to the record increments the lock_version column and the locking facilities ensure that records instantiated twice will let the last one saved raise a StaleObjectError if the first was also updated. Example:

1
2
3
4
5
6
7
8
p1 = Person.find(1)
p2 = Person.find(1)

p1.first_name = "Michael"
p1.save

p2.first_name = "should fail"
p2.save # Raises an ActiveRecord::StaleObjectError

Optimistic locking will also check for stale data when objects are destroyed. Example:

1
2
3
4
5
6
7
p1 = Person.find(1)
p2 = Person.find(1)

p1.first_name = "Michael"
p1.save

p2.destroy # Raises an ActiveRecord::StaleObjectError

You’re then responsible for dealing with the conflict by rescuing the exception and either rolling back, merging, or otherwise apply the business logic needed to resolve the conflict.

This locking mechanism will function inside a single Ruby process. To make it work across all web requests, the recommended approach is to add lock_version as a hidden field to your form.

1
<%= form.hidden_field :lock_version %>

In our controller, we need to permit the version column so that it is included in the params used to update the user.

1
2
3
4
# Only allow a list of trusted parameters through.
def product_params
params.require(:user).permit(:first_name, :lock_version)
end

This behavior can be turned off by setting ActiveRecord::Base.lock_optimistically = false. To override the name of the lock_version column, set the locking_column class attribute:

1
2
3
class Person < ActiveRecord::Base
self.locking_column = :lock_person
end

You can override the name of the locking column globally in environment.rb:

1
2
3
# config/environment.rb:

ActiveRecord::Base.set_locking_column 'record_version'

References

[1] ActiveRecord::Locking::Optimistic - https://api.rubyonrails.org/v6.1.4/classes/ActiveRecord/Locking/Optimistic.html

[2] Ruby Avoiding Race Conditions with Optimistic Locking - Rails - http://underpop.online.fr/r/ruby/rails/tutorial/ruby-on-rails-3-19.html

[3] Using ActiveRecord Optimistic Locking to prevent overwrites - https://blog.unathichonco.com/activerecord-optimistic-locking

[4] ActiveRecord::Locking::Pessimistic - https://api.rubyonrails.org/v6.1.4/classes/ActiveRecord/Locking/Pessimistic.html

[5] sql server - Optimistic vs. Pessimistic locking - Stack Overflow - https://stackoverflow.com/questions/129329/optimistic-vs-pessimistic-locking

[6] Active Record Basics — Ruby on Rails Guides - https://guides.rubyonrails.org/active_record_basics.html