yeyo
yeyo

Reputation: 3009

How to properly use/plug Redis with Rails?

I have a Rails application that I want to connect to a Redis data structure server. I'm wondering how I should proceed. I'm using a global variable $redis locate at config/initializers/redis.rb to make queries across the entire application.

I believe this approach it is not suitable for a application with 80+ simultaneous connections, because it uses one single global variable to handle the Redis connection.

What should I do to overcome this problem? am I missing something about Rails internals?

Tutorial I'm following http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html

Upvotes: 2

Views: 1109

Answers (1)

2called-chaos
2called-chaos

Reputation: 3078

This depends on the application server you will use. If you're using Unicorn which is a popular choice you should be fine.

Unicorn forks it's workers and each one will establish it's own database connection. And since each worker can only handle one request at a time it will only need one connection at a time. Adding more connections won't increase performance, it just will open more (useless) connections.

ActiveRecord (which is the DB-part of Rails) or DataMapper support connection pooling which is a common solution to overcome the problem you've mentioned. Connection pooling however only make sense in a threaded environment.

On top of that Redis is mainly single threaded (search for "Single threaded nature of Redis") so there might be no advantages anyway. There was an request to add connection pooling but it got closed, you might get more information from there.

Upvotes: 3

Related Questions