Recently we have been receiving a lot of questions on how to integrate Telerik’s ASP.NET controls in a Load Balanced Environment. So, we have decided to shed some light on the subject in this blog post. More and more web developers decide to distribute workload across multiple computers, network links, or other resources, because this is one of the most efficient ways to avoid overload, minimize the response time and in the same time achieve optimal resource utilization and maximize network throughput.  Brace yourself for a short journey through the web backend ecosystem.

What is a WebGarden

WebGarden is the scenario in which a single physical machine is used for multiple worker processes running simultaneously. Each worker process is responsible for handling all kinds of requests, responses, session data, cache data, etc. and all the ASP.NET functionality inside IIS runs under the scope of the worker process. By default, every application pool contains a single worker process. A WebSite containing multiple worker processes is called a WebGarden.

Put simply – if you increase the number of worker processes for a given ApplicationPool you get a WebGarden.

WebGarden Scheme

What is a WebFarm

WebFarm is called a hosting environment consisting of multiple web servers. All servers in this architecture are connected through a virtual IP behind a Load Balancer, which routes the incoming requests to the servers within the WebFarm. Whenever a client request is received, it will go through the Load Balancer (LB) and then, based on the load of each server, the LB will distribute the request to the corresponding machine. In order to share one and the same Session data, all servers should be configured to use identical machine key.



What is a Machine key and how to configure it 

 The Machine Key element configures the keys that are used for encryption and decryption of forms authentication cookie data and ViewState data, verification of the out-of-process session state identification, etc.

In standard applications it is configured automatically and it is different for every single WebApplication.

In a WebFarm configuration, however, to get your site running properly on all the machines you must manually generate specific values for validation and decryption keys and you must use those values on all computers in the WebFarm (i.e. configure the <machineKey> in all web.config files with the same values).

The main reason for using one and the same machine key is the inconsistency in the SessionState – the Session depends a lot on the machine key to keep all the needed data in the SessionState. If you keep the keys with the automatically generated values, every server will access only his own Session data and this will lead to discrepancies between the loaded resources. If there is a reason to use different machine keys, a custom implementation of session state should be provided.

You could see bellow how the Telerik’s ASP.NET ImageEditor might look if the machine key is not set manually, making the control not able to load its resources properly:

Resource Discrepancy


The basic steps you should follow in order to manually configure the keys, are:

  1. Open IIS Manager in one of the servers and navigate to the WebApplication you want to manage to generate its Machine Key. In Features View, double-click Machine Key:
    generate-machine-key-1

  2. On the Machine Key page, in order to generate specific validation and decryption key values for a WebFarm, you have to clear “Generate a unique key for each application” for both validation and decryption keys as well as to clear “Automatically generate at runtime” :
    generate-machine-key-2

  3. Click Generate Keys in the Actions pane to create specific key values:

  4. The generated keys will be shown in the middle pane:
    generate-machine-key-4

    Once you have generated the MachineKey, it will be added automatically to the application’s web.config file:
    <system.web>
        <machineKey decryptionKey="26B5FF931A1B12125F38A3777530A4FA473F91E700310403"
                    validationKey="FF7CA280ECBC27363EEA697CBF597DC4E94311D008525F3C38749CC98ED9374A3519FBDAD064A0C805F76431C49FB6138040D3P895A5454824E48A88C67975C7" />
    </system.web>

  5. All you need to do after that is to copy the above generated machine key and add it to the web.config files of the application in all of your servers.

Integrating RadControls in WebFarm/WebGarden

Finally – we get to the main point of the post :-) There are some configurations which have to be made in order to make RadControls work properly in a Load Balanced Environment:

  1. ImageStorageLocation – if the used control supports this property (e.g. Telerik’s ASP.NET Captcha, ImageEditor, etc.) it has to be set to “Session”. By default the image is stored in the Cache. If more than one server (worker process) is used to host the page, the images should be taken from the Session because it is shared among all the machines/applications. For instance:

    <telerik:RadImageEditor ID="RadImageEditor1" runat="server" ImageCacheStorageLocation="Session" ImageUrl="~/Images/image1.jpg">
    </telerik:RadImageEditor>

  2. The HttpHandler and Handler definitions for the Telerik.Web.UI.WebResource (in the web.config file) should be modified manually by setting the type of the HttpHandler to be equal to type="Telerik.Web.UI.WebResourceSession":

    <system.web>
        <httpHandlers>
            <add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResourceSession" verb="*" validate="false" />
        </httpHandlers>
    </system.web>
     
    <system.webServer>
        <handlers>
            <add name="Telerik_Web_UI_WebResource_axd" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResourceSession" verb="*" preCondition="integratedMode" />
        </handlers>
    </system.webServer>

  3. I will say this once more, because it is vital - if the application is running in a WebFarm, the machine key in the web.config file of all servers must be one and the same (configured manually in one of the applications), e.g:

    <system.web>
       <machineKey decryptionKey="26B5FF931A1B12125F38A3777530A4FA473F91E700310403" validationKey="57728BB9E72EDCEB97985B0A907770649282236B081518272776199EC62198B9B1D39FB53B7424CD87E6C2C9A99308B308D1841AA2B2E39CD18FC965AF5B60D " />
    </system.web>

  4. Access the same resources from all servers / applications – if the database or shared files are modified only in one place you will have issues later, so they should be shared among all apps/machines. This is valid even if you don’t have a single RadControl around. This is done by using a common database (e.g. for grids) or a custom content provider for controls like FileExplorer, so that they don’t attempt to use local resources that will not be synced, but the common ones.


At the end if our short journey we know much more of the surrounding web ecosystem. You should take full advantage of it and provide your customers the best possible experience. Leave a comment below if this helped you get started or you’ve had an interesting case you got running!


About the Author

Veselina Raykova

is a Support Officer in the ASP.NET AJAX division. In her work she is mainly responsible for RadImageEditor, RadFileExplorer and RadSplitter. Apart from work she enjoys photography, design and cooking.

Related Posts

Comments

Comments are disabled in preview mode.