Load Balancing with Ribbon

Gaurav Sharma
2 min readMay 30, 2020

--

While working on providing transaction support @ Fabhotels by using multiple transaction gateways, I needed to make sure that —

  1. No Single Payment Gateway is overloaded.
  2. We choose a gateway that is alive for each request.
source: studytonight.com

How can we solve this problem?

  1. We can load balance by simple round-robin — Use the gateways in a cycle (A > B > C >A). But this will not solve the second issue of avoiding failures.
  2. Use a load balancer with intelligent routing.

What is Ribbon?

Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides client-side load balancing algorithms.

Why Ribbon?

  1. If you are in the Spring eco-system then using Ribbon as a load balancer is surprisingly easy and quick.
  2. Ribbon is robust and production tested by Netflix and by countless others who use Netflix’s cloud libraries.
  3. Its Open Source 👍 (https://github.com/Netflix/ribbon/wiki)

Cool, how do we use it?

There are few key components of configuring Ribbon in your Spring-Boot Application.

  1. IPing — This bean allows you to ping the service.
  2. Server — This bean represents an instance of a service
  3. IClientConfig — This bean will contain all the configurations needed for Ribbon to work.

How to configure Ribbon in Spring Boot?

  1. Maven Dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

2. Add the following in application.properties

3. Create Gateway Server (which is a child class of Server Class)

4. Create Configuration Bean for Gateways

5. Create Configuration Bean for Ribbon

How to get the load balanced gateway?

After getting the Gateway enum value, we can then use strategy pattern to get Service for that gateway and proceed with the transaction.

That’s it! 😄

--

--