While working on providing transaction support @ Fabhotels by using multiple transaction gateways, I needed to make sure that —
- No Single Payment Gateway is overloaded.
- We choose a gateway that is alive for each request.
How can we solve this problem?
- 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.
- 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.
- If you are in the Spring eco-system then using Ribbon as a load balancer is surprisingly easy and quick.
- Ribbon is robust and production tested by Netflix and by countless others who use Netflix’s cloud libraries.
- 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.
- IPing — This bean allows you to ping the service.
- Server — This bean represents an instance of a service
- IClientConfig — This bean will contain all the configurations needed for Ribbon to work.
How to configure Ribbon in Spring Boot?
- Maven 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! 😄