Archive for the ‘Uncategorized’ Category

Hints on the nginx fair load balancer

Friday, September 11th, 2009

The default load balancing for nginx is simple round robin. If you are proxying to a set of peers, and one request for whatever reason is very slow, nginx will continue to give the busy peer additional requests. The result is that requests that might ordinarily be quickly fulfilled will stack up behind the slow request.

There is an add on module for nginx called the fair load balancer. There isn’t all that much documentation available. Hopefully this post will fulfil some of that need. This is of necessity a summary — the fair load balancer has three weight modes and two scan modes, and can fit a large number of site requirements.

The fair load balancer is initialized in the upstream declaration:


upstream mongrel {
fair;
192.168.1.77:4000;
192.168.1.77:4001;
192.168.1.77:4002;
}

This declaration will use fair in its default mode. In this mode, fair will assign idle peers requests first, when all are busy it will assign the requests using a score that depends on both the number of requests assigned to the peer (as most important) and if all equal, to the peer that had the earliest assignment.

Peers may be assigned weights. A weight is an integer value that is related to the number of requests that can be assigned to each peer, although the interpretation is a little different for each weight mode,

Fair also keeps track of peer failures. A failure is defined as an I/O error connecting to the peer — but not an html error and, most importantly, not a timeout. If a peer fails once, the load balancer will only consider the peer for a request if all others are busy. Moreover, when a peer reaches “max_fails”, the software will not assign it a request for fail_timeout seconds.

Here is an example of a configuration that takes advantage of these features. It uses weight_mode=peak to prevent upstream overload, It sets peer failure to pause 60 seconds in assignment on the event of a backend error.


upstream mongrel {
fair weight_mode=peak;
192.168.1.77:4000 max_fails=1 fail_timeout=60 weight=6;
192.168.1.78:4000 max_fails=1 fail_timeout=60 weight=6;
192.168.1.79:4000 max_fails=1 fail_timeout=60 weight=6;
}

The weight_mode=peak option imposes a limit on each peer of weight requests, in this case, 6. If all peers have 6 requests, then the directive instructs the load balancer to return busy (nginx will display the “site is temporarily unavailable” page). Obviously, this sort of configuration only makes sense if you have a large number of servers and would rather have an error page than an overloaded backend.

Bug: There is an important bug in the fair load balancer that affects 64 bit systems. ngx_http_upstream_choose_fair_peer_busy has an initialization that is incorrect. The problem line that reads:


ngx_uint_t best_sched_score = ~0U;

This only assigns a 64 bit integer 32 bits of 1s, it needs to be 64 bits of ones.


ngx_uint_t best_sched_score = ULONG_MAX;

Is one simple way to fix the problem.

Kawaii - an amazing web console

Friday, October 3rd, 2008

The Ruby on Rails console is one of the most useful tools a web developer could have. It is the backend equivalent of firebug for those not familiar with Ruby on Rails. But every now and then I find myself hoping that I could actually connect to a running instance of ruby on rails and debug it.

Today, I decided that I am desperate enough that I was going to look for one (not enough to build one yet). I googled “ruby on rails web console” and there it was on the first page: KAWAII (which means “cute” in japanese).

I had to do some changes to make it work with the tree where I need it, since the tree is on rails 1.2.6. Here is a list of changes if you happen to need it:

  • Modify all html.erb files back to the rhtml extension
  • Download the prototype 1.6 javascript file and replace the “javascript_include_tag :defaults” with an explicit link to the new prototype file (rails 1.2.6 includes prototype 1.5)
  • Copy the contents of config/initializer/kawaii.rb into config/environments/development.rb

enjoy!

MedHelp seeking superstar developers

Sunday, June 29th, 2008

We are always looking for great developers to join the team. Here is our job description:

Are you looking for a challenge? Work on cutting edge technologies for one of the hottest startups in tech? Are you passionate about technology and ready to make an impact? If so, MedHelp is the place for you!

MedHelp is the world’s largest online heath community. We are a social network where we connect people with similar interests, a destination where doctors from the best hospitals answer medical questions, and where users can find and use medical applications related to their health.
We are bringing health to web2.0. We have over 5.5 millions users and growing rapidly. We are a top 10 health destination.

We are looking for top notch engineers. We are located in San Francisco. We offer competitive salary, full benefits, and stock options…and cool co-workers, a pool table, and fully stocked fridge :)

Some of our recent projects include:
* building an application platform for tracking and charting medical information: http://www.medhelp.org/land/ovulation-calendar
* implementing real-time activity feeds that scale to handle millions of user activities
* developing social networking features that apply to medical information and our health-interested users

Responsibilities:
MedHelp is a fast-paced startup looking for motivated and passionate developers.
We look for candidates who are ready to take on a problem and drive it end to end from concept to completion.

Desired Skills:
• Ruby on Rails or related MVC framework development experience
• Database experience (MySQL)
• AJAX, JS, prototype, CSS
• Experience building scalable and performant applications

Send your resume to jobs@medhelp.org

Home page: http://www.medhelp.org
Typical user: http://www.medhelp.org/user_profiles/show/193137

Ruby Meetup at MedHelp

Monday, June 9th, 2008

In Feb, we hosted the SF Ruby Meetup.

We had a great time hosting this event and meeting other local developers building apps on ruby. We are big time rails proponents and we gave a presentation covering our top lessons learned in scaling MedHelp