Mathematics is full of weird and wonderful constructs that you can use to leap buildings in a single bound. If you examine even a small string of mathematical symbols, you’ll discover a complex dance of interrelated algorithms. Even the humble “plus” and “minus” operators reveal a fair number of mathematical principles and properties.
One of my favorite operators is modulo, or simply mod. This thing is pure magic. For example, suppose I needed to keep track of whether I was on an odd or even row in a table. People do this all the time to color alternating rows, making the information more readable. I could write some code like this:
is_even = false rows.each do |row| if is_even color = :even else color = :odd end is_even = !is_even end
Using the magic of the mod, I can simplify my life a little:
rows.each_with_index do |row, i| if (i % 2) == 0 color = :even else color = :odd end end
Now, that was OK, but not really all that magical. But lets take this idea one step further. Suppose you were providing a web service and you had a bunch of clients connecting to your servers. In order to distribute the load, you could use some kind of proxy or load balancer. Each client would then connect to one IP address and the proxy would divvy up the requests among your servers. The problem with this is you have a potential bottleneck in your system; you can only respond to requests as fast as your proxy can process the traffic.
One alternative is to harness the magic of the mod. The first thing you need is some kind of unique ID for each client. This could be a username, a GUID, or whatever. The second thing you need is to give each client a list of servers it can connect to.
Now, the client will need to figure out all by itself which server in your cluster is its buddy for life. Its soul mate. The client does this by first taking it’s ID and hashing it into a number. The hash algorithm should have as even a distribution as possible (i.e., two IDs that are similar should hash to totally different numbers). Can you tell why this is important? Now, once the client has that fancy number in hand, it is time to invoke the magic of the mod. Simply mod the ID hash by the number of servers, and the number you get is the index of the server you should send your requests to.
If you are, well, paranoid, and want to ensure that a rogue piece of software isn’t sending everything to a single server, regardless of the client ID, you will need some way of looking up the ID for each request and then doing the same hash-lookup procedure on the server side that the client is supposed to do. If the result is kosher (i.e., the server receiving the request is the one the client should be talking to), the server processes the request. Otherwise, the server returns a big fat error. As big and fat as possible.
What is your favorite use for the mod?