DEV Community

Cover image for An Introduction to Microservices pt. 4
Bernardo Costa Nascimento
Bernardo Costa Nascimento

Posted on

An Introduction to Microservices pt. 4

Table of Contents

  1. What are we trying to solve?
  2. The Solution: Load Balancer
  3. Show Me the Code
  4. Bibliography

On part 3, we talked about Service Registry and Health Checks. Now, we're going to improve our Gateway and take advantage of the possibility of multiple instances given by the Service Registry.

What are we trying to solve?

So far, we've implemented an API Gateway that receives all the requests and routes them accordingly. Also, our services/instances are registered with our Service Registry and the Gateway checks the service's location with it. But how can we make so that our Gateway uses all available instances of a service and no instance becomes overloaded?

The Solution: Load Balancer

To insure that, we need to implement a Load Balancer. This pattern will leverage the requests between available instances of our services. Ocelot gives us a simple way to implement a Load Balancer. Also, it provides a few different algorithms of load balancing:

  1. Round Robin - Imagine that you have three instances of a given service. The algorithm will make so that the first request is handled by instance #1, the second by instance #2, the third by instance #3, the fourth by #1 again, and so on;
  2. Least Connections - Every new request is sent to the instance with the least amount of active connections;
  3. Cookies Sticky Sessions - Uses a cookie to direct all request to a specific instance.

IMPORTANT: Keep in mind that other load balancing solutions might have different algorithms.

Show Me the Code

To implement our Load Balancer, Ocelot makes it very easy. All we need to do is add a new property to our 'ocelot.json' file. This property needs to be added on a route per route basis:

  "Routes": [
      // ...
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      // ...
  // ..
Enter fullscreen mode Exit fullscreen mode

That's it! Our API Gateway can now balance the load between all of our services instances!

On the next article, we'll talk about the Messaging pattern pattern. Until next time.


  1. Ocelot: Load Balancer -

Top comments (0)