Skip to main content

When two choices are enough

I came across the power of two choices  theorem recently, it was referenced in a tech talk about schedulers. The central premise of the power of two choices is simple. Yet it is striking in its effectiveness in making decisions about routing requests. The following is my restatement of the premise:

You have n options, and a score or rank associated with each of them. You also have an incoming queue of decisions to make about which option to pick. At each decision point, if you pick two options from the n at random, and then use the score to pick the best among those two, you will get exponentially better results (in terms of distributing load), than choosing a random option. 

Going from best of two, to best of 3 or 4 or more, only improves the results by a constant factor. 

Using power of two choices really starts to pay off when n is very large and examining all n options becomes prohibitively expensive. 

The paper applies this to load balancing, where they showed how effective this was. I stated it in more abstract terms because unsurprisingly, there are uses for this beyond load balancing algorithms. It has been shown to be effective in hashing, shared memory emulation for DRAMs, circuit routing, and scheduling work onto servers.

All of this sounds really cool, but I wanted to understand why it works. Like other "magical" approximations I've learned about - bloom filters, Minhash,  T-Digest etc, there is complex math behind it.

The paper models the problem using balls and bins, instead of servers and requests. If we chose a bin at random among n bins to send an incoming ball to, the bin with the maximum load can be proven to have O( log n/ log log n ) balls with probability at least (n-1)/n. I tried to follow the proof behind that, but I couldn't understand it. It involves using a Chernoff Bound. I'll have to dig into this part later. For the purpose of this blog post, it is enough to understand that when picking a bin at random, the bin with a maximum load with high probability has O(log n/log log n) balls.

For power of two choices, instead of picking a bin at random like we did above, we pick two bins independently and uniformly at random, and then send the ball to the one with the lesser load between those two. Now, the bin with the maximum load can be proven to have a load of O(log log n /ln 2) + O(1) with high probability. That is exponentially smaller than the completely random choice method. But why?

Intuitively, with the two choices method using n bins, we can show that at-least half the bins have 2 or more balls, a quarter of them have 3 or more, 1/16 have 4 or more, and 1/256 have five or more. To prove this, consider the ratio α to be the fraction of bins that have i balls. The upper bound for the fraction of bins with at-least i+1 balls would be α*α. Given this intuition,  and that the placing of balls into two bins meets the criteria for a binomial random variable, the proof again uses Chernoff bounds to show that the load is O(log log (n)/log(2))

I don't fully understand the probability math behind this proof. However, I think the power of two choices is a great piece of knowledge to have at the back of my mind. I no longer work at Indeed, but now I wonder if the boxcar implementation  would have been simplified on the client side if it used power of two choices, instead of maintaining a heap sorted by server slot numbers. Boxcar clients only ever talked to pools consisting of up to a max of hundreds of servers, so the n is too small for it to be a powerful enough performance optimization. Maintaining a min heap of size < 100 is not a performance bottleneck.  Still, it might be interesting to compare average load on the servers using both methods, as a fun theoretical exercise.

Update: Former co-worker Dan Heller pointed out that power of choices could apply to life outside of CS too. It should work when choosing which super market line to go to, or which summer blockbuster movies to try getting tickets for.

Comments

Popular posts from this blog

What are your future plans? Why are you *still* a developer?

If I had a nickel for every time someone has asked me that question I'd have enough change for a year? Inspired by my friend's post here , I thought I'd write about how I ended up doing what I do now. Then I thought about it some more and decided to write about something else. Is it important to know where you are going in life? If you aren't moving forward in your career does it mean that you are doing something wrong? What does "moving up the ladder" even mean? I am going to attempt to answer these questions for myself. Its almost 5 years since I began my professional career. It has been great so far, lots of ups some downs as well. However, once in a while when I get the title question it still throws me off. It is usually my parents or well meaning relatives, sometimes friends that ask this. I have nothing much to say to them except "I enjoy what I am doing right now, haven't really thought about the future". But the truth is - I have thou

Craigslist, wrong calls and me

I have the worst luck when it comes to getting wrong calls on my phone. Why do I say that? It started a few weeks ago with a phone call - "I'm calling to ask about the Nissan altima you have on sale". I replied saying wrong number yada yada etc. After the third call like this, I finally asked them where they found the ad and it turned out to be Craigslist. One google search later I found the ad listing my number, probably a typo. Contacted them via Cragislist and asked them to correct it. End of story, right? Two days later, I got ANOTHER call, here is how it went. Caller: Maam, Im calling about the toyota four runner for sale Me: You mean the Altima, that was a mistake in the ad. I am not the seller and it has been corrected in the ad online. Caller: Is your number 602-*******? That's what's on there.. Me:ARGGH Another google search, and I find a DIFFERENT Craigslist ad in another city, listing my number. What are the chances of this happening to the same perso

Don't teach programming by asking students to program - What?

Saw this article in one of the ACM blogs that cited a recent study in an educational psychology journal on how teaching introductory Cs students to program by having them write code actually has a detrimental effect. More details here . I absolutely disagree with the author that this study has implications in CS! First of all it cites a study done with teaching algebra so it is a different domain. I think that the problem is that there is too little programming going on rather than too much. I also wonder of anyone has considered that an introductory programming class probably has a mix of two very different types of students - one just exploring CS as one of many career options, the other the kind that took their dad's computer apart in the garage and started programming at age 8. The latter kind still have to take the introductory class because of degree requirements but now it gives you a class sample that's skewed and hard to make any conclusions about. If everyone that to