ezmobius/redis-rb — Issue #153: connections pool and synchronize
Author: antirez
Created: 2011-10-17 07:48:23+00:00
State: open

Hello,

I'm writing a Sinatra/Redis application, and so far I used something like that to create my Redis object:

before do
$r = Redis.new(:host => RedisHost, :port => RedisPort) if !$r
end

So basically all the web server threads are sharing the same Redis connection. Once the number of parallel clients using the web application grows the locking needed in redis-rb makes this approach not super efficient, so I wanted to switch to a connection pool.

The pool was designed in order to use only a single connection for a whole request, and is as simple as this:

```
RedisPoolSize = 10
def setup_redis_connection
$rpool = Array.new(RedisPoolSize) if !$rpool
i = rand(RedisPoolSize)
$rpool[i] = Redis.new(:host => RedisHost, :port => RedisPort) if !$rpool[i]
$r = $rpool[i]
end

before do
setup_redis_connection
end
```

This way I hoped to reduce the contention between different web server threads.
However this does not working hanging the application.

From a first glance to the source code it seems like there is a deadlock, and this deadlock may be caused by redis-rb using a single lock for all the instances, but I'm not sure about this since I don't full understand the file monitor.rb for lack of familiarity with Ruby threading.

Thank you in advance for any help on this issue.

Salvatore