29
.
04
.
2024
22
.
06
.
2023
Ruby on Rails
Backend
Ruby
Tutorial

A simple guide to pessimistic locking in Rails

Michał Łęcicki
Ruby Developer

In the Rails world, optimistic locking is relatively well known, while its pessimistic alternative is often overlooked. In this blog post, I will present how to effectively use pessimistic locking in Rails applications.

What is it?

Pessimistic locking works at the moment of retrieving the records from the database. One process blocks a particular record and others wait until it's unblocked. This ensures that a certain process will always use the newest version of the record (or raise an exception).

Pessimistic locking assumes that transaction conflicts occur frequently in the system. In such a situation optimistic locking wouldn't be much useful: it would cause irritating Stale Object errors too often. To address this challenge, a different approach is necessary.

The remarkable benefit of pessimistic locking is the fact that it doesn't affect the whole system. You don't have to change the database at all. Instead, you need to explicitly specify all areas which will utilize this technique. This way you have full control of which processes needs to care about locking. It's useful for fixing places with race conditions, without affecting other functionalities.

Show me the code


ActiveRecord::Base.transaction do
  # SELECT * FROM INVOICES WHERE id=? FOR UPDATE
  invoice = Invoice.lock.find(invoice_id)

  return unless invoice.status == 'new'

  invoice.create_payment
  invoice.update(status: 'paid')
end

Selecting a particular invoice uses special SQL command: SELECT ... FOR UPDATE. It "locks" the rows returned by SELECT and prevents other processes from retrieving it until the transaction is done. At the same time, other places in the app could use the good old Invoice.find(invoice_id) statement without worrying about locks.

Advanced stuff

It is possible to use database-specific locking by passing custom clauses to the lock method, such as:


# raise an error if a record is already locked
invoice = Invoice.lock("FOR UPDATE NOWAIT").find(invoice_id)

There is also an alternative method for locking individual records: with_lock . In this scenario, all operations happening within the block are wrapped into the transaction.


invoice = Invoice.find(invoice_id)
invoice.with_lock do
  (..)
end

The general rule is: Always use pessimistic locking within a transaction. Theoretically, you can call lock! method on records outside of it, but it doesn’t make sense and won’t simply work.

Testing pessimistic locking

Testing pessimistic locking is not trivial. To simulate the real conditions, many processes must attempt to retrieve a record simultaneously. This can be achieved by using some concurrency mechanisms, like ruby threads:


threads = []
3.times do
  threads << Thread.new do
    service.call
  end
end
threads.each(&:join)

expect(invoice.payments.count).to eq 1 # this should fail without a lock

Summary

There are certain scenarios when pessimistic locking is perfect. It's a valuable tool for resolving race conditions and maintaining data integrity. Use it when you don't want to introduce an extra version column for optimistic locking. Or, when you need to fix a specific place in your Ruby on Rails app without affecting the rest of the system.

Michał Łęcicki
Ruby Developer

Check my Twitter

Check my Linkedin

Did you like it? 

Sign up To VIsuality newsletter

READ ALSO

Ruby MCP Client in Rails by Paweł Strzałkowski

MCP Client in Rails using ruby-mcp-client gem

11
.
06
.
2025
Paweł Strzałkowski
LLM
Ruby on Rails
Actionmcp in Ruby on Rails by Paweł Strzałkowski

MCP Server with Rails and ActionMCP

11
.
06
.
2025
Paweł Strzałkowski
LLM
Ruby on Rails
Banner - MCP Server with FastMCP and Rails by Paweł Strzałkowski

MCP Server with Rails and FastMCP

11
.
06
.
2025
Paweł Strzałkowski
LLM
Ruby
Ruby on Rails

Ruby on Rails and Model Context Protocol

11
.
06
.
2025
Paweł Strzałkowski
Ruby on Rails
LLM
Title image

Highlights from wroclove.rb 2025

11
.
06
.
2025
Kaja Witek
Conferences
Ruby
Jarosław Kowalewski - Migration from Heroku using Kamal

Migration from Heroku using Kamal

11
.
06
.
2025
Jarosław Kowalewski
Backend
store-vs-store_accessor by Michał Łęcicki

Active Record - store vs store_accessor

11
.
06
.
2025
Michał Łęcicki
Ruby
Ruby on Rails
How to become a Ruby Certified Programmer Title image

How to become a Ruby Certified Programmer

11
.
06
.
2025
Michał Łęcicki
Ruby
Visuality
Vector Search in Ruby - Paweł Strzałkowski

Vector Search in Ruby

11
.
06
.
2025
Paweł Strzałkowski
ChatGPT
Embeddings
Postgresql
Ruby
Ruby on Rails
LLM Embeddings in Ruby - Paweł Strzałkowski

LLM Embeddings in Ruby

11
.
06
.
2025
Paweł Strzałkowski
Ruby
LLM
Embeddings
ChatGPT
Ollama
Handling Errors in Concurrent Ruby, Michał Łęcicki

Handling Errors in Concurrent Ruby

11
.
06
.
2025
Michał Łęcicki
Ruby
Ruby on Rails
Tutorial
Recap of Friendly.rb 2024 conference

Insights and Inspiration from Friendly.rb: A Ruby Conference Recap

11
.
06
.
2025
Kaja Witek
Conferences
Ruby on Rails

Covering indexes - Postgres Stories

11
.
06
.
2025
Jarosław Kowalewski
Ruby on Rails
Postgresql
Backend
Ula Sołogub - SQL Injection in Ruby on Rails

The Deadly Sins in RoR security - SQL Injection

11
.
06
.
2025
Urszula Sołogub
Backend
Ruby on Rails
Software
Michal - Highlights from Ruby Unconf 2024

Highlights from Ruby Unconf 2024

11
.
06
.
2025
Michał Łęcicki
Conferences
Visuality
Cezary Kłos - Optimizing Cloud Infrastructure by $40 000 Annually

Optimizing Cloud Infrastructure by $40 000 Annually

11
.
06
.
2025
Cezary Kłos
Backend
Ruby on Rails

Smooth Concurrent Updates with Hotwire Stimulus

11
.
06
.
2025
Michał Łęcicki
Hotwire
Ruby on Rails
Software
Tutorial

Freelancers vs Software house

11
.
06
.
2025
Michał Krochecki
Visuality
Business

Table partitioning in Rails, part 2 - Postgres Stories

11
.
06
.
2025
Jarosław Kowalewski
Backend
Postgresql
Ruby on Rails

N+1 in Ruby on Rails

11
.
06
.
2025
Katarzyna Melon-Markowska
Ruby on Rails
Ruby
Backend

Turbo Streams and current user

11
.
06
.
2025
Mateusz Bilski
Hotwire
Ruby on Rails
Backend
Frontend

Showing progress of background jobs with Turbo

11
.
06
.
2025
Michał Łęcicki
Ruby on Rails
Ruby
Hotwire
Frontend
Backend