# Synchronize mongrel cluster

**URL:** <https://www.ruby-forum.com/t/synchronize-mongrel-cluster/135117>\
**Category:** Rails\
**Created:** [April 18, 2008, 2:46am UTC](https://www.ruby-forum.com/t/synchronize-mongrel-cluster/135117 "2008-04-18T02:46:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [April 18, 2008, 2:46am UTC](https://www.ruby-forum.com/t/synchronize-mongrel-cluster/135117/1 "2008-04-18T02:46:12Z")

</div>

Hi everyone,

I would like to know if there’s a way to synchronize the actions in a  
mongrel cluster. Basically, how do I make sure that a specific  
controller action gets executed by only one mongrel server at one  
time?

I obviously cannot use Mutex::synchronize, because we’re talking about  
different processes. I looked into using optimistic and pessimistic  
locking. I don’t like using optimistic locking, because handling the  
conflict in my case is a real pain. I tried using pessimistic locking,  
and I can’t get it to work. This is my controller code:

class DoerController \< ApplicationController  
def do  
Locker.transaction do  
r = Locker.find(1, :lock =\> true)  
sleep 10  
r.save!  
end  
redirect\_to :action =\> :index  
end  
end

If I start the do action at the same time on ports 8000, 8001 and 8002  
(corresponding to my 3 mongrel servers), they will all finish at the  
same time. I expected for 8000 to finish in 10 seconds, 8001 to finish  
in 20 seconds and 8003 to finish in 30 seconds, but they don’t. Maybe  
I’m looking at this the wrong way.

I thought about other options: implement a run queue (and use the id  
as a priority mechanism) and having a dedicated server run this  
controller action (e.g. in do(), have redirect\_to ‘[http://localhost](http://localhost):  
8002/doer/do8002’), but I really want to know what other people use in  
production for this kind of issue.

Thanks,  
Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [April 18, 2008, 9:47am UTC](https://www.ruby-forum.com/t/synchronize-mongrel-cluster/135117/2 "2008-04-18T09:47:37Z")

</div>

Mr\_Tibs wrote:

> end
> 
> If I start the do action at the same time on ports 8000, 8001 and 8002  
> (corresponding to my 3 mongrel servers), they will all finish at the  
> same time. I expected for 8000 to finish in 10 seconds, 8001 to finish  
> in 20 seconds and 8003 to finish in 30 seconds, but they don’t. Maybe  
> I’m looking at this the wrong way.[…]

Does your database support transaction/locking (MySQL with MyISAM  
doesn’t) ?

Lionel

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [April 18, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/synchronize-mongrel-cluster/135117/3 "2008-04-18T20:36:03Z")

</div>

Oh man, that was it! The Rails engine type by default is innodb,  
however, I created the lock table manually; I guess the default in  
mysql is myisam, so it was bombing.

Thanks so much Lionel.
