# Load balancing in Rails

**URL:** <https://www.ruby-forum.com/t/load-balancing-in-rails/107558>\
**Category:** Deployment\
**Created:** [June 23, 2007, 5:07am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558 "2007-06-23T05:07:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 23, 2007, 5:07am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/1 "2007-06-23T05:07:38Z")

</div>

Hey,

Can someone give me some advice in setting up load balancing (and  
suggestions on some good cheap software load balancers.) I need a load  
balancer which can look at the incoming http request, query a session  
variable in the request, and based on the value, it should route the  
request to 1 of 10 servers. My questions are:

a) Is it even possible to inspect session variables, or do the Load  
balancers (LB) work at a higher level? Will it be a considerable  
performance hit to do this level of lookup?

b) I have a hash which maps a value in the session variable to the  
server to which we need to route the request. What’s the best way to  
maintain this hash? Keep it in memory in the LB? or have a database that  
stores these values? Which is better given that I have about 10 million  
values to choose from?

c) Any suggestions on a good software load balancer that can do this?

I would appreciate any help possible.  
Thanks,

Ram

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 23, 2007, 6:16am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/2 "2007-06-23T06:16:12Z")

</div>

On 6/22/07, Ram R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> c) Any suggestions on a good software load balancer that can do this?

HAProxy

You may also want to use RubyWorks ([http://rubyworks.rubyforge.org](http://rubyworks.rubyforge.org)),  
as a starting point.

–  
Alex

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 23, 2007, 6:28am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/3 "2007-06-23T06:28:15Z")

</div>

On Jun 22, 2007, at 8:07 PM, Ram R. wrote:

> balancers (LB) work at a higher level? Will it be a considerable  
> performance hit to do this level of lookup?

Are you talking about variables stored in your rails session? If so  
then no there is no way to query the session variables and make  
decisions based on that in the load balancer. Most load balancers can  
do balancing based on some cookie or special header in the request.  
But getting at the data in the rails session is only possible from  
within a rails app and the load balancer works in front of rails ands  
has no way to query the session values.

> b) I have a hash which maps a value in the session variable to the  
> server to which we need to route the request. What’s the best way to  
> maintain this hash? Keep it in memory in the LB? or have a database  
> that  
> stores these values? Which is better given that I have about 10  
> million  
> values to choose from?

It is going to be a huge performance hit if your load balancer has to  
check a database for each request it routes. Can you expound on what  
problem you are trying to solve with this? Maybe there is a different  
way to accomplish what you want that will still perform acceptably.

> c) Any suggestions on a good software load balancer that can do this.

Second vote for HAProxy, it’s probably the most flexible fast  
software load balancer around. Although I know it will not be able to  
inspect your rails sessions, thats not an option at the load  
balancing point.

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 23, 2007, 6:39am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/4 "2007-06-23T06:39:16Z")

</div>

On 6/22/07, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Second vote for HAProxy, it’s probably the most flexible fast  
> software load balancer around. Although I know it will not be able to  
> inspect your rails sessions, thats not an option at the load  
> balancing point.

HAProxy has some rather fancy stuff around session affinity - it can  
match sessions by patterns in cookies. Even though it obviously won’t  
have access to server-side Rails session data, you can copy enough of  
that data to a cookie and have HAProxy look at that.

–  
Alex

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 23, 2007, 6:40am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/5 "2007-06-23T06:40:17Z")

</div>

On 6/22/07, Alexey V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> HAProxy has some rather fancy stuff around session affinity - it can  
> match sessions by patterns in cookies.

Oh well, I should have read your entire reply first… 🙂

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 23, 2007, 10:30am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/6 "2007-06-23T10:30:28Z")

</div>

> It is going to be a huge performance hit if your load balancer has to  
> check a database for each request it routes. Can you expound on what  
> problem you are trying to solve with this? Maybe there is a different  
> way to accomplish what you want that will still perform acceptably.

Thanks for the replies. I have a few hundred thousand users in around 10  
servers. All information about a user is completely within one of the 10  
servers. I was planning on using the load balancer to figure which  
server contains a user’s information by doing a database (with some sort  
of memcache?)look up. When a new user signs up, he is assigned a server  
according to the information he gives at signup. I would then update the  
database on the load balancer with this information.

I am kind of lost actually… is there a better way of doing this?

Thanks,  
Ram

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 23, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/7 "2007-06-23T22:25:48Z")

</div>

Hello Ram.

I’d love to discuss your deployment with you.

Perhaps we can be of service here?

Thanks!

–  
– Tom M., CTO  
– Engine Y., Ruby on Rails Hosting  
– Support, Scalability, Reliability  
– (866) 518-YARD (9273)

On Jun 23, 1:30 am, Ram R. \<ruby-forum-incom…@andreas-

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 26, 2007, 5:12am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/8 "2007-06-26T05:12:12Z")

</div>

On Jun 23, 4:30 am, Ram R. \<ruby-forum-incom…@andreas-  
[s.net](http://s.net)\> wrote:

> according to the information he gives at signup. I would then update the  
> database on the load balancer with this information.
> 
> I am kind of lost actually… is there a better way of doing this?
> 
> Thanks,  
> Ram

If you are sending a user to a single specific server, then why bother  
with the load balancer? I would just do the simplest path, which is  
send them a unique URL pointing to “their” server. For example, login  
to a main page, do your database lookup there and present them with a  
link to continue. The link will contain a url to “their” server (eg:  
[host9.x.com](http://host9.x.com)).

With your original route, you are now going to have to also manage a  
load balancer (and its fault tolerant brother) and you’ll be relying  
on a database lookup for every page request. Seems like a lot of  
moving parts and it isn’t even buying you any scalability. Since you  
are forced to one server per use, I’d just say go with it. 🙂

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 26, 2007, 7:02am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/9 "2007-06-26T07:02:30Z")

</div>

> If you are sending a user to a single specific server, then why bother  
> with the load balancer? I would just do the simplest path, which is  
> send them a unique URL pointing to “their” server. For example, login  
> to a main page, do your database lookup there and present them with a  
> link to continue. The link will contain a url to “their” server (eg:  
> [host9.x.com](http://host9.x.com)).

Thanks for your suggestion. I guess I am going with something similar  
(in lines of Ezra’s suggestion). At first the user gets forwarded to any  
of the servers which figures out which server he really belongs to. It  
then sets a cookie for the load balancer for future forwarding to the  
right server. It also serves the main page.

By doing it this way, the user notices nothing explicitly about the  
redirection. He may notice the slower response time for just the first  
request.

Thanks,  
Ram

---

<div class="post-metadata">

**Author:** ![raam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raam](https://www.ruby-forum.com/u/raam)\
**Post date:** [June 26, 2007, 6:32am UTC](https://www.ruby-forum.com/t/load-balancing-in-rails/107558/10 "2007-06-26T06:32:11Z")

</div>

> Perhaps we can be of service here?

Yeah, at some point very soon when I get enough dough from FFF (friends,  
family and fools) for my startup 🙂

Ram
