Glimpse/Glimpse — Issue #164: Round Robin Load Balancing Breaks Glimpse
Author: mattscully
Created: 2012-02-22 16:04:49+00:00
State: open

Our web application is stateless and is deployed behind a load balancer that uses a round-robin distribution algorithm for routing web traffic. However, this causes Glimpse not to work since the <script> tags inserted before the BODY tag cause another HTTP request from the browser that may get routed to a server different from the original page request. To illustrate this problem, here is an outline of the interesting events:

1. HTTP Request 1 is made for a page
2. Load balancer routes Request 1 to Server A
3. Plugins collect data for Request 1 and Glimpse inserts <script> tags in the Response to source glimpseData and glimpseClient code into the page.
4. Browser makes HTTP Request 2 and 3 to request glimpseData and glimpseClient javascript.
5. Load balancer routes requests to Servers B and C.
6. Glimpse on Server B or C does not recognize the glimpseData request ID and no data is returned.
7. Glimpse doesn't load for the page.

I am not sure what the best solution is for this problem as it would be difficult to have a solution that works with every application scenario and load balancer. To fix it for my specific scenario, I have a custom build of Glimpse and modified the HttpContextBaseExtensions.GlimpseResourcePath() method to include the server's IP address and port number as a prefix to the virtual path when IP Forwarding is enabled. This works for us because we are going to allow Glimpse to be used only on our internal network so users will be able to resolve the server's IP address. However, this solution will not work for those that want Glimpse to work externally and have their servers behind a firewall.

Also, in order to get the Elmah plugin to work (we're using an in memory store right now), I had to modify the ElmahPlugin.GetErrors() method to build the View Details link using the same logic as Glimpse so that this link would always route to the correct server.

Perhaps the correct solution would be one that follows the 80/20 rule, but provides an extension point that allows Glimpse consumers to customize this logic if needed. Perhaps using MEF to import a custom strategy for resolving resources? If one is not found, then Glimpse uses a default strategy. Whatever the solution, it may make sense to allow for extensibility points in a consistent manner. For example, a similar strategy may be applied for determining the user's IP address when IP Forwarding is enabled which would help with things like issue #153. This would allow Glimpse to solve for 80% of users while giving the 20% the means to tailor these algorithms to their specific scenarios without having to do a custom build of Glimpse.

For reference, here are the changes that I have made in my custom Glimpse build:

[HttpContextBaseExtensions](http://snipt.org/uSq6)
[ElmahPlugin](http://snipt.org/uSr5)