Introduction
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.
- Building API Gateway Using Ocelot In ASP.NET Core - Basic
- Building API Gateway Using Ocelot In ASP.NET Core - Authentication
- Building API Gateway Using Ocelot In ASP.NET Core - Logging
- Building API Gateway Using Ocelot In ASP.NET Core - Rate Limiting
- Building API Gateway Using Ocelot In ASP.NET Core - QoS
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.
I will use version 5.0.0 of Ocelot to show you this feature.
Preparation
We need to create three projects and ensure that they can run well.
In APIServicesA project, it will return From APIServiceA.
- [HttpGet]
- public string Get()
- {
- return "From APIServiceA";
- }
- [HttpGet]
- public string Get()
- {
- return "From APIServiceB";
- }
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.
- {
- "DownstreamPathTemplate": "/api/values",
- "DownstreamScheme": "http",
- "DownstreamHostAndPorts": [
- {
- "Host": "localhost",
- "Port": 9001
- },
- {
- "Host": "localhost",
- "Port": 9002
- }
- ],
- "UpstreamPathTemplate": "/",
- "LoadBalancer": "RoundRobin",
- //"LoadBalancer": "LeastConnection",
- "UpstreamHttpMethod": [ "Get" ]
- }
- //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.

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.
I hope this helps you!

Khanh NguyenPosted Dec 12, 2020, 12:19 AM
Hi, I want to use images from Service A(wwwroot/images) through the GateWayAPI like UseStaticFiles. How to config it? Please!
MJ EbrahimiPosted Sep 10, 2020, 3:06 PM
Good article. For the record, this article was added to this awesome repository. https://github.com/mjebrahimi/Awesome-Microservices-NetCore
vino testPosted Nov 30, 2019, 2:59 AM
Hi, If we down one port in it will return 404 and next time result from running port vice versa its going, How to we redirect the request to running port and get the response? Please advise.
Uttam VyasPosted Oct 13, 2019, 5:08 AM
I've tried the above explained and it's not working for me. It always showing From APIServiceA. Could you help me with this?
andres descalzoPosted Apr 16, 2018, 4:01 PM
Do you know how to combine a response from two API? for example, if a call an apigateway endpont, but to response this, I need to request 2 internal api.
Tridip BhattacharjeePosted Apr 3, 2018, 8:24 AM
Yes how Ocelot implement this feature or others.
Tridip BhattacharjeePosted Apr 3, 2018, 5:02 AM
What is round robin...if you please explain it how thing works in round robin fashion. thanks