Load Balancing with Ribbon

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.
Image for post
Image for post
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.

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

  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)

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.
  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! 😄

Written by

I like building stuff.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store