Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Framework for using a distributed mutex and an implementation of a MySQL database stored mutex.
Latest commit 5f4adba @birkirb Short update to the readme.



by Birkir A. Barkarson <>


A rails plugin proving an abstract distributed mutex along with a MySQL implementation that uses mysql named locks. Others distributed locks using Memcached etc could be implemented using the same framework.

A few words about the naming. It's a bit confusing, since we're talking about a mutext that works for a distributed application environment, but is in itself centralized. This is a fairly common environment for Ruby on Rails since many application will run many instances all taking to a centralized database. If so then yes, this could be what you need.

Lastly this is by far the only solution to this kind of problem. Generally you should just be using record locks if you are using a database to begin with. However I did find a use for this where I wanted to prevent a user from executing a certain code path that was only designed to work for a single thread. Why users would try doing the same thing in multiple windows is of course beyond the scope of this text, but a centralized mutex could be used to prevent such undesired assholery.

If you don't have MySQL or prefer to store your mutex in a different centralized repo (such as memcache, or MongoDB) then adding such an implementation to this one should be trivial.

Installation from source

git clone git://
cd distributed_mutex
rake install

Rails Plugin Installation

script/plugin install git://

Gem Install

gem install distributed_mutex

To add the plugin to your rails project, add the following to your config/environments.rb:


or just stick in your bundle.


Copy spec/config/database.stub to spec/config/database.yml and edit as needed for your mysql installation.

gem install bundler
bundle install
rake spec



Birkir A. Barkarson <>


Copyright © 2009


MIT License

Something went wrong with that request. Please try again.