Building API Gateway Using Ocelot In ASP.NET Core - Load Balancing


In the previous articles of this series, we discussed how to build the API Gateway in ASP.NET Core.

And in this article, we will discuss the Load Balancing module of Ocelot.

If you want to look at the previous articles of this series, please visit the links given below.

What is Load Balancing? 

Load balancing improves the distribution of workloads across multiple computing resources, such as computers, a computer cluster, network links, central processing units, or disk drives. Load balancing aims to optimize resource use, maximize throughput, minimize response time, and avoid overload of any single resource. 

For our API services, Ocelot's load balancer is a software program that is listening on the port where external clients connect to access services.

I will use version 5.0.0 of Ocelot to show you this feature.


We need to create three projects and ensure that they can run well.

Here I use two API services as one API service to show, which may make you understand more clearly.

In APIServicesA project, it will return From APIServiceA.

  1. [HttpGet]  
  2. public string Get()  
  3. {  
  4.     return "From APIServiceA";  
  5. }   
 In APIServicesB project, it will return From APIServiceB.
  1. [HttpGet]  
  2. public string Get()  
  3. {  
  4.     return "From APIServiceB";  
  5. }    
After running them up, we will add configuration of Load Balancing to them.

Configure Load Balancing

We need to pay attention to node DownstreamHostAndPorts and node LoadBalancer.

DownstreamHostAndPorts is an array that contains all services' host and port. In this sample, it will contain both APIServiceA and APIServiceB.

The value of LoadBalancer node is a string which specifies the scheduling algorithms.

At this time, Ocelot support two scheduling algorithms, one is RoundRobin, the other one is LeastConnection.

Here is a sample shows you how to configure.
  1. {  
  2.   "DownstreamPathTemplate""/api/values",  
  3.   "DownstreamScheme""http",  
  4.   "DownstreamHostAndPorts": [  
  5.     {  
  6.       "Host""localhost",  
  7.       "Port": 9001  
  8.     },  
  9.     {  
  10.       "Host""localhost",  
  11.       "Port": 9002  
  12.     }  
  13.   ],  
  14.   "UpstreamPathTemplate""/",  
  15.   "LoadBalancer""RoundRobin",  
  16.   //"LoadBalancer": "LeastConnection",  
  17.   "UpstreamHttpMethod": [ "Get" ]  
  18. }    
  19. //others.....  

The above configuration means that when we visit http://localhost:9000/, we will get the result from http://localhost:9001/api/values or http://localhost:9002/api/values, which is based on the RoundRobin load balancer.

Let's take a look at the result:


As you can see, there is an alternation of From APIServiceB and From APIServiceA! It means that load balancing is working now, Ocelot forwards the requests to the downstream services.

Here is the source code you can find on my GitHub page.

This article introduced the basic usage of load balancing in Ocelot. It also can be used with service discovery. I will show you more about this later.

I hope this helps you!