Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Poor man's distributed lock using SimpleDB

branch: develop

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 Rakefile
Octocat-spinner-32 sdb_lock.gemspec


Poor man's distributed lock using SimpleDB. It is useful when you don't want to maintain distributed lock server by yourself.


Add this line to your application's Gemfile:

gem 'sdb_lock'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sdb_lock


require 'sdb_lock'

lock =
  'my_app_lock_domain',  # SimpleDB domain name to use
  create_domain: true,   # At the first time, you will need to create domain. Note it might take long time.

  # Other hash members will be passed to AWS::SimpleDB#new as is.
  # You can set credential by other ways including environmental variables.
  # See

  # see
  simple_db_endpoint: "",
  access_key_id: YOUR_AWS_ACCESS_KEY,
  secret_access_key: YOUR_AWS_SECRET

locked = lock.try_lock("a1") do
  # do some work

# if you want to block until gain lock, then use #lock
# WARN There is no way to wake up others. It is slow with high contention
#   because it does polling internally.
lock.lock("a1") do
  # do some work

# List locked resource names

# Some times lock might remain because of network failure. Then we'll need
# a way to unlock these.
# Unlock older than 10 secs.


  • Lock might remain by network failure or other reason. See #unlock_old.
  • Number of domains are limited by SimpleDB.
  • Each Lock is represented by an item in SimpleDB. Number of items are limited by SimpleDB.


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Something went wrong with that request. Please try again.