Load balancing in Rails

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

On 6/22/07, Ram R. [email protected] 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),
as a starting point.


Alex

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
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

On 6/22/07, Ezra Z. [email protected] 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

On 6/22/07, Alexey V. [email protected] 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… :slight_smile:

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

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-

On Jun 23, 4:30 am, Ram R. <ruby-forum-incom…@andreas-
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).

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. :slight_smile:

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).

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

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 :slight_smile:

Ram