Thursday, October 14, 2010
I have been meaning to update my blog for some time with the routing solution I presented in Cloud Cover 24. One failed laptop harddrive later and well. I lost my original post and didn't get around until now to try to replace it. Anyhow, enough of my sad story.
One of the many great benefits of using Windows Azure is that networking is taken care of for you. You get connected to a load balancer for all your input endpoints and we automatically round-robin the requests to all your instances that are declared to be part of the role. Visually, it is pretty easy to see:
However, this introduces a slight issue common to all webfarm scenarios: state doesn't work very well. If your service likes to rely on the fact that it will communicate with exactly the same machine as last time, you are in trouble. In fact, this is why you will always hear an overarching theme of designing to be stateless in the cloud (or any scale-out architecture).
There are inevitably scenarios where sticky sessions (routing when possible to the same instance) will benefit a given architecture. We talked a lot about this on the show. In order to produce sticky sessions in Windows Azure, you need a mechanism to route to a given instance. For the show, I talked about how to do this with a simple socket listener.
We introduce a router between the Load Balancer and the Web Roles in this scenario. The router will peek (1) the incoming headers to determine via cookies or other logic where to route the request. It then forwards the traffic over sockets to the appropriate server (2). For all return responses, the router will optionally peek that stream as well to determine whether to inject the cookie (3). Finally, if necessary, the router will inject a cookie that states where the user should be routed to in subsequent requests (4).
I should note that during the show we talked about how this cookie injection could work, but the demo did not use it. For this post, I went ahead and updated the code to actually perform the cookie injection. This allows me to have a stateless router as well. In the demo on the show, I had a router that simply used a dictionary lookup of SessionIDs to Endpoints. However, we pointed out that this was problematic without externalizing that state somewhere. A simple solution is to have the client deliver to the router via a cookie where it was last connected. This way the state is carried on each request and is no longer the router's responsibility.
You can download the code for this from Code Gallery. Please bear in mind that this was only lightly tested and I make no guarantees about production worthiness.