Server Load Balancing In WCF

Introduction

Load balancing is an interesting feature in WCF to distribute the workload to the dedicated Server or a group of Servers, which provides a good response time for the client calls. With the help of routing, we can distribute the load and get the response immediately rather than waiting for the response from one Server always. Let us discuss this in detail.

Need for Load balancing

  • Maximizing throughput
  • Minimize response time
  • Dedicated Server for the critical operations
  • Avoiding overload
  • Optimizing resource

Definitely, you can experience any of the problems, mentioned above in train ticket booking Websites during the festival season when you try to book the tickets. Since a large number of users have raised the ticket booking requests, the one Server would not able to process all the requests, which makes the users wait for a long time to get their tickets booked. On the other hand, it might not have a dedicated Server for the ticket booking calls (other calls like PNR Status and food ordering also utilize the same Server and make the network traffic). When there is an increase in the Server load, the response time to the client will also increase. In this article, we will see how we can balance the Server load in WCF by assigning a dedicated Server for the critical client operations like ticket booking. We can also load balance the Servers in a round-robin method.

Load balancing setup

 Load Balancing

In the snapshot given above, you can find that the client will make various operations. If the critical operation like ticket booking request comes to the Load Balancer Service, it will redirect to the dedicated Server assigned for the critical operations to maximize the response time. This Server will take care of only the ticket bookings; it will not receive any requests like PNR status or food order etc. These normal operations will be handled by the other Service. Let us see how the same setup will be implemented, using WCF.

Services Configuration

As a first step, you need to define two services, where one is to handle the critical operations and the other one to handle the normal operations. Operation Contract or Operations will be same across the 2 Servers. Both the Servers are capable of handling all the operations. In the snapshot given below, you can find the operations like PNR,TicketBook will be hosted in a Service 1 and Service 2.

 Load Balancing

 Load Balancing

Load balancer is one which sits between the client and the Services will load balance; the BookTicket requests to the dedicated Server. Since the BookTicket operation will be getting many requests by a large number of clients during the festive/tatkal season, we have hosted a dedicated Server to maximize the response time. Once you implement the contracts, create the Service, endpoint and base address to host the Service. Please take the snapshot, given below as a reference and do the hosting for the Services 1 and 2.

 Load Balancing

Please refer to my old articles for more details on hosting the services.

Load Balancer Configuration

Now, let us configure the load-balancing Server to move the ticket booking operations on a dedicated Server and the PNR operations to the normal Server. In our case, Service 2 will receive and process the critical operation requests (TicketBook) and Service 1 will receive and process the normal operation requests (PNR). Technically, Load balance Server is none other than the routing Server, which redirects the request based on the operation (TicketBook/PNR). Please refer to my old articles for more details on routing Service configuration. As I mentioned in my routing articles, please remember the sentence "Service the FilterTable and FilterEnd", which will take you to configure routing Server easily.

Service = Configure the Routing Service, as mentioned in the snapshot is given below, The name of the routing Service should always be "System.ServiceModel.Routing.RoutingService"and select the address, binding and contract depending upon the requirement.

 Load Balancing

Next step, Filter Table = Define the Filter table which requires Filter name and the associated endpoint name, which means if the filter gets matched, redirect the message to the URL mentioned in the associated end point.

 Load Balancing

The final one is FilterEnd, which means we need to define the filter and the end point. When we are defining the filter, we must supply the filtertype and filterData. In our case, when the data(iSeatBook/BookTicket) comes to the Service, it should pass the URL mentioned in the associated end point.

 Load Balancing

In the snapshot given above, you can find that if the incoming message comes for the BookTicket operation (look at the FilterData element) in Filter, it finds the associated endpoint in the filtertable. In our case, the associated end point of Filtername "TktBookFilter" is "ept_TktBookLoadBalanceServer". Afterwards, it fetches the address from the endpoint element and redirects the message accordingly. In our case, the address to redirect the data (iSeatBook/BookTicket) is http://localhost:8650/RailServices2. This logic will redirect all the ticket booking operations to the address http://localhost:8650/RailServices2 and redirect all the PNR operations to the address http://localhost:8650/RailServices1. Please download the attached source code and have a look on it.

Output

Let’s analyze the output screens to get a better understanding

 Load Balancing

If you notice the first line in the top 2 Windows, it clearly mentions the address, where the Service is hosted. Left side Window in the top side tells that the Server is hosted at http://localhost:8650/RailServices1 and it processes only the PNR operations, whereas the right Window in the top side tells that the Server is hosted at http://localhost:8650/RailServices2 and it processes only the TicketBooking operations. The bottom Window is a client Window, where it sends the requests to the Service side. Please download the attached source code for more details on it.

Summary

Load balancing feature in WCF can be attained with the help of routing. It load balances (routes) the incoming requests to the appropriate Server, which is based on the operation.


Similar Articles