Logo

dev-resources.site

for different kinds of informations.

What is load balancing and how to do it on client side

Published at
1/10/2025
Categories
java
spring
Author
ozkanpakdil
Categories
2 categories in total
java
open
spring
open
Author
11 person written this
ozkanpakdil
open
What is load balancing and how to do it on client side

“Load balancing” can be explained as maintaining any workload. For example if you have to serve 1000 breakfast in the morning you can divide the work among 2-3 or more caterers to lower the delivery/preparation time.

In the computer world, same logic applies, if you want to deliver fast, you can divide the work, for example for a website we can have 5-10 webserver, this way website will be delivered faster(especially during high traffic), this is server side.

I want to talk about client side load balancing and how this works, for example you are querying backend for every request, and you have multiple servers doing the backend work, for this I will use WHOIS service

whois1.service.com whois2.service.com

We can load balance every request in these two servers, most used algorithm and default round robin meaning every request will go to one than another, like who1, who2 who1 and so on, this can be done on the client side with “Spring Cloud LoadBalancer”. we need to configure 3 classes

  1. ServiceInstanceListSupplier for defining the backend addresses and ports
  2. Bean ServiceInstanceListSupplier for supplying the list of end points
  3. @LoadBalancerClient which is the webclient will be used for backend calls

Now, we have the whole setup for the backend

public class RestCaller implements ServiceInstanceListSupplier {

    private final String serviceId;

    public RestCaller(String serviceId) {
        this.serviceId = serviceId;
    }

    @Override
    public String getServiceId() {
        return serviceId;
    }

    @Override
    public Flux<List<ServiceInstance>> get() {
        return Flux.just(Arrays.asList(
                new DefaultServiceInstance(serviceId, serviceId, "abc.cyclic.app", 443, true),
                new DefaultServiceInstance(serviceId, serviceId, "someting.koyeb.app", 443, true),
                new DefaultServiceInstance(serviceId, serviceId, "127.0.0.1", 8080, false)
        ));
    }
}


@Configuration
public class RestCallerConfiguration {

    @Autowired
    @Qualifier("webClientBuilder")
    WebClient.Builder webClientBuilder;

    @Bean
    @Primary
    ServiceInstanceListSupplier serviceInstanceListSupplier(ConfigurableApplicationContext ctx) {
        return ServiceInstanceListSupplier
                .builder()
                .withRetryAwareness()
                .withHealthChecks(webClientBuilder.build())
                .withBase(new RestCaller("whoisService"))
                .build(ctx);
    }
}


@Configuration
@LoadBalancerClient(name = "whoisService", configuration = RestCallerConfiguration.class)
public class WebClientConfig {

    @LoadBalanced
    @Bean
    @Qualifier("loadBalancedWebClientBuilder")
    WebClient.Builder loadBalancedWebClientBuilder() {
        return WebClient.builder();
    }

    @Bean
    @Qualifier("webClientBuilder")
    WebClient.Builder webClientBuilder() {
        return WebClient.builder();
    }

}

Enter fullscreen mode Exit fullscreen mode

This can now be used in any @Component or @Service.

@Component
public class WhoisCaller {

    @Autowired
    @Qualifier("loadBalancedWebClientBuilder")
    WebClient.Builder webClientBuilder;

    public String getWhois(String ip, String source) {
        String url = "http://whoisService/whois?ip=" + ip + "&source=" + source;

        return webClientBuilder.build()
                .get()
                .uri(url)
                .retrieve()
                .bodyToMono(String.class)
                .timeout(Duration.ofMinutes(1))
                .doOnError(e -> {
                    WebClientResponseException e1 = (WebClientResponseException) e;
                    log.error(e1.getRequest().getURI().getHost());
                })
                .block();
    }
}

Enter fullscreen mode Exit fullscreen mode

References:

spring Article's
30 articles in total
Favicon
Launched a Web version of my Project using Java Spring Framework, Spring Boot Web
Favicon
Developing a project using Java Spring Framework, JSON, JPA and PostgreSQL
Favicon
Волшебные скоупы: Как Spring организует работу бинов
Favicon
Quando usar ResponseEntity?
Favicon
What is load balancing and how to do it on client side
Favicon
Capturing and Testing Logs in Java with SLF4J and Logback: A Simple Guide
Favicon
Can you find the Output of this Java Code
Favicon
Ways to Speed Up Spring Boot Application Startup Time
Favicon
Apache wicket with spring boot example application: notice board
Favicon
Handling the "Forwarded" Header in Spring Boot Applications
Favicon
About UriComponentsBuilder and UriComponents
Favicon
Spring Boot: About @SpringBootApplication
Favicon
Spring Security: CSRF protection
Favicon
ISBN Stacks — A look at a possible Spring Application implementation without annotations
Favicon
spring profiles dev production
Favicon
Learn Spring Data JPA, Part - 1
Favicon
Methods for Efficient Large File Processing in Spring Boot
Favicon
Hexagonal Architecture — A Favorite Lyrics Spring Boot — Java Example
Favicon
Validation in Spring REST Framework (SRF)
Favicon
spring
Favicon
Spring Security: Redirect to login page if access pages which is for authorized user only
Favicon
Lambda vs. Named Functions: Choosing the Right Tool for the Job
Favicon
Techniques for Mastering Spring Interceptors: Detailed Guide with Examples
Favicon
Techniques for Using @NoRepositoryBean in Spring Data JPA: A Comprehensive Guide
Favicon
Understanding the Spring Framework: A Developer’s Journey to Clean Code 🚀
Favicon
@PreConstruct and @PostConstruct annotation Spring Boot Example
Favicon
Java’s Functional Programming: the OOP influence
Favicon
🧪 Тестирование с TestRestTemplate и MockMvc: миссия "Котики против багов" 🐞
Favicon
spring boot
Favicon
Reasons to Use WireMock for Testing REST APIs in Spring Boot Applications

Featured ones: