ASP.NET Session State Management With Redis (Local Server Farm Testing)

What is Redis

Redis is an open source key value data structure store. Keys can be strings, hashes, lists, sets, sorted sets, etc. This in memory data store is broadly used in session state storing and caching.

Why Redis for Session State Storing

If you are developing web applications for big companies, possibilities are your application is hosted on multiple servers. To be frank that is actually true. In some cases, for example, you may want to provide a backup server if your main server fails. In other cases, for example you may want to decrease the requests load on one of your server and rather distribute them across multiple servers. So you got the point, right?

We will configure IIS to balance load across multiple servers. Don’t worry about the servers because I’ll configure my localhost with different port numbers to act as multiple server instances.

Now the is a catch in doing that! Suppose as a user I’m sending requests through your web application, you have a load balancer configured on top of your backend servers which can route incoming requests to one of your servers but you don’t know exactly which one since it depends on the state of your servers (load on a server/ is server up and running?). Again suppose in your app you have the session storing mechanism configured. For example, in your e-commerce web application you may want to store the added items to the cart through sessions. This is a real life scenario because you don’t want your user to lose all the added items if he/she accidentally closes the browser window. You can persist those items for a limited amount of time in sessions so that user do not have to start again from the beginning. So sessions are good! Right?

Anyways since we have multiple servers, sessions will be lost if something goes wrong. For example, you accidentally closed the browser window while adding items to the cart. So, when you again open your browser and go to the same application URL, your request may or may not be routed to the same server which you were in the last browsing session. It may also be the fact that by the next time you are requesting to the application, the previous server dies and routed your request to a backup server. So in both the cases you are doomed!

Redis comes to rescue us from this kind of situations! Since it is an in-memory data store, it sits on the load balancing server. Requests came into the load balancer where Redis is used to store the sessions. Again when a next request comes in the previous session it can be found in the load balancer. So there are no worries of losing sessions. May be a simple diagram can clear you mind.


So in our server farm we have four servers among which one is a load balancer or sometimes we call it the staging server. All the requests came into the staging server then it is bypassed to one of the connected servers where the actual instances of our web application is.

CREATING LOCAL SERVER FARM

To create local server farm, first you have to configure IIS (not IIS express that comes with visual studio) on your machine. Configuring IIS is easy. Just follow the steps below,

  • Go to Control Panel, then Programs and Features.
  • Click on “Turn Windows features on or off” from right panel.
  • Select “Internet Information Service”and expand the node.
  • Select the node“World Wide Web Services”>Application Deployment Features, then ASP.NET 4.5.
  • Click OK to install IIS on Windows 7/8/8.1/10.

After installing a reboot may be required. From start menu open Internet Information Services (IIS) Manager.



To be able to create server farm from IIS manger you will need an extension. You can use Web Platform installer and download a whole bunch of extensions from there. For now, we only need Application Request Routing (ARR) extension for IIS

Install Web Platform Installer from this link (https://www.microsoft.com/web/downloads/platform.aspx)



Install Application Request Routing 2.5

  • Web Farm Framework (WFF) included.
  • Or install ARR 3.0 & Web Farm Framework (WFF) separately.

CREATING SERVERS FOR SERVER FARM

Now we have to create local servers for our server farm. Since we are using different ports on localhost to act as multiple servers, we have to include their address in the windows host files. Add entry to the host file (C:\Windows\System32\drivers\etc\host) for three different servers i.e. load balancer (staging server in the diagram), server-a, server-b.



As you can see, we have configured three server addresses against localhost (127.0.0.1). Now to distinguish between them, we have to bind ports against them.

Go to IIS manager, right click on the “Sites” node and select “Add Website”. Add three sites i.e. load balancer, server-a and server-b. Use 9000 for the load balancer whereas 9001 is for server-a and 9002 is for server-b. Here is a screenshot on how I configured and added server-a.



If you added all the sites you should have the sites list like this,


Time to add the servers to the server farm. From IIS manager right click on the server farm node and select “Create Server Farm”.

Give your server farm a name (redis-testing-farm).



On the next page add your two servers (www.redis-testing-server-a.com, www.redis-testing-server-a.com ) with their respective http ports i.e. 9001 and 9002. Finish the step and click on the “No”button when rewrite rules window pops up.



DEFINE URL REWRITE RULES

We have to define the URL Rewrite rules. Just follow the steps below.

On root node select URL Rewrite



Click “Add Rule” from right action bar and select blank rule

  • Give a name to your rule (redis-testing-farm-rule).
  • Type( .* ) in pattern.

Expand condition node and add two conditions

C0ndition input Check if input string Pattern
{HTTP_HOST} Matches the pattern (www\.|^)redis-testing-server-staging.com
{SERVER_PORT} Matches the pattern 9000

Expand the“Action” node

  • Select “Route to Server Farm” from action type dropdown.
  • Check “Stop processing of subsequent rules”.
  • Apply rules.

Select redis-testing-farm and go to “Load Balance” option

  • Select “Weighted round robin”
  • Apply





Again select redis-testing-farm and go to “Server Affinity”

  • Select “Client Affinity”.
  • Apply


We have selected round robin as load balancer algorithm. It will evenly distribute your incoming requests to servers. Which means if a request comes for the first time to the load balancer server, it will be routed to server A. If the page is reloaded and request is sent again then it will be routed to server B. The servers will be switched back and forth every time a request comes in. I’ve added an index in every server and placed some demo text in it to give you some indications about which server the request is currently hitting. Here is a GIF for you,



CREATING A SAMPLE ASP.NET WEB APPLICATION AND DEPLOYING

  • Download the attached code .
  • Deploy the application in all the servers using file system (run your Visual Studio as administrator if you got access denied error).



Browse to localhost

  • Servers will be toggled back and forth every time a new request is sent.
  • But sessions are different for each server (server-a, server-b).

DOWNLOADING AND RUNNING (REDIS-SERVER) REDIS VIA NUGET

Install Redis x64 server from nuget or chocolatey.

Here is the command to put in the package manager console of Visual Studio.

Install-Package Redis-64.

And here you can install it from chocolatey,

https://chocolatey.org/packages/redis-64

      •  After installation double click on redis-server.exe to run the redis server.

  • Run redis-cli from the command prompt.
  • Type (keys *) to get all the stored keys.



TESTING SESSION STATE WITH REDIS-CLI

We are almost at the end, last of all we need a nuget package that will help us to store the session states in Redis. The package is called RedisSessionStateProvider. Here is the command to install it from package manager console,
Install-Package Microsoft.Web.RedisSessionStateProvider.

And to make Redis store our sessions, we need to add these lines to our web config file.



If it is already added, then all is okay. Let’s deploy the project again in your three servers and give it a spin, you will see all your session state persisted successfully.



As you can see I’m setting the text in a session variable in server A. Later on, that is shown in both the Result.aspx page of those two servers i.e. our session is persisted. If you see the source code, you will see that I’ve used two session variables, one for input in the textbox and another for the current date and time. Both are saved as encrypted keys in the redis store.

Up Next
    Ebook Download
    View all
    Learn
    View all