Home
Blog
Pessimistic Locking In Rails

Pessimistic Locking In Rails

・3 min read
Pessimistic Locking In Rails

You may also like:

Essential RubyOnRails Patterns — Part 4: Form Objects

Essential RubyOnRails Patterns — Part 4: Form Objects

Read more

The Ruby on Rails framework is powerful in many ways. One of its strong points is that Ruby on Rails is being shipped with ActiveRecord - an Object Relational Mapping system. ActiveRecord comes up with its own DSL, which lets you write Ruby code, which in turn is translated to SQL, allowing you to use database native features, including “locking”.

Before we start discussing pessimistic locking in Rails, we have to understand the mechanism of database locking itself. To cut the long story short, locking can be explained as a feature that allows two people to update, change or modify an object at the same time, in an unconflicted way.

Let me write some code illustrating the point. The first presented iteration will be implemented without setting any locks on the database:

Img

As you can see, those are two methods in two consoles. One of them has sleep, which can represent some kind of timeouts or errors in the system and this is how we usually do it when updating a user.

I run it in the consoles.

Without lock

But as a final result, I got Joe Doe, instead of Anna Doe.

Locking

So the question is, what happened? Well, the first transaction did not finish and in the meantime, someone else wanted to change the user name, because of that we got the wrong result. In such a situation we can make use of pessimistic locking. In Rails, the lock method allows us to block the object and in this case, stop it from updating.

This is how it looks like with the pessimistic locking

Locking

Again, I have two similar methods, but now the second one is run right after the first one and this time, our result is

With lock

IImg

n Rails applications, the default behaviour of the lock method is ‘For Update’, but also other strategies exist. You can check the documentation for PostgreSQL to find different locking mechanism strategies, not only for an update.

What are the real world examples of using database locking technique in  Ruby on Rails applications? Well, I can think of a few examples: we can use it in banking when performing money transfers. From my personal experience, I found it most useful when we wanted to withdraw money from an account, which more people have access to. So by mistake, we could give permission to withdraw money from an empty account. Also, database locking could be a good choice to manage bookings of rooms in hotels. This would allow two people to have access to the same object at the same time.

With pessimistic locking, there also comes optimistic locking, but this is a topic for a separate article, which may be featured on our blog in the nearest future. As for now, this is all. Happy locking!


Rate this article:

4,9

based on 0 votes
Our services
See what we can create for You
Our services

Awards & Certificates

reviewed on
30 reviews
  • Top 1000 Companies Global 2021
  • Top Development Company Poland 2021
HR dream team
  • 2020 HR Dream Team Award
  • 2016 Employer Branding Featured
  • 2015 HR Dream Team Award
ISO CertificateISO Certificate
  • Information Security Management System compliant with PN-EN ISO/IEC 27001
  • Business Continuity Management compliant with ISO 22301