load balancer with example

load balancer with example

A load balancer is a device or software that distributes network or application traffic across multiple servers. By distributing the load, it ensures that no single server becomes overwhelmed, thus enhancing the availability and reliability of applications. Load balancers can manage various types of traffic, including HTTP, HTTPS, TCP, and UDP.

load balancer with example

Types of Load Balancers:

  • 1. Hardware Load Balancers: Physical devices designed for load balancing.
  • 2. Software Load Balancers: Applications that provide load balancing functionality.
  • 3. Cloud Load Balancers: Load balancing services provided by cloud platforms (e.g., AWS ELB, Azure Load Balancer).

Load Balancing Algorithms:

  • 1. Round Robin: Distributes requests sequentially across the server pool.
  • 2. Least Connections: Directs traffic to the server with the fewest active connections.
  • 3. IP Hash: Distributes requests based on the hash of the client’s IP address.

Example using Spring Cloud Load Balancer

In this example, we’ll create a simple microservices architecture with a load balancer using Spring Cloud Load Balancer.

  • Service Instances:
    • We have two instances of a User Service running on different ports.

User Service
```java
// UserServiceApplication.java
@SpringBootApplication
@RestController
@RequestMapping("/users")
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable String id) {
        return new User(id, "John Doe", "john.doe@example.com", System.currentTimeMillis());
    }
}

// User.java
public class User {
    private String id;
    private String name;
    private String email;
    private long timestamp; // To distinguish which instance responded

    // Constructors, Getters, and Setters
}
```

Order Service
```java
// OrderServiceApplication.java
@SpringBootApplication
@RestController
@RequestMapping("/orders")
public class OrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{orderId}")
    public Order getOrderById(@PathVariable String orderId) {
        ResponseEntity<User> response = restTemplate.getForEntity("http://USER-SERVICE/users/123", User.class);
        User user = response.getBody();
        return new Order(orderId, "Product 1", user);
    }
}

// Order.java
public class Order {
    private String orderId;
    private String productName;
    private User user;

    // Constructors, Getters, and Setters
}

// RestTemplateConfig.java
@Configuration
public class RestTemplateConfig {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
```

Configuration (application.yml)
```yaml
spring:
  application:
    name: order-service
server:
  port: 8081

# Instance 1 of user service
---
spring:
  profiles: instance1
  application:
    name: user-service
server:
  port: 8082

Instance 2 of user service
---
spring:
  profiles: instance2
  application:
    name: user-service
server:
  port: 8083

Order service
---
spring:
  application:
    name: order-service
server:
  port: 8081
```

Explanation:

  • 1. User Service:
    • Provides an endpoint `/users/{id}` to get user details.
    • Running two instances on different ports (8082 and 8083).
  • 2. Order Service:
    • Uses `RestTemplate` with `@LoadBalanced` annotation to distribute requests across multiple instances of User Service.
    • The `getOrderById` method calls the User Service to get user details and then creates an Order object using the retrieved user information.

Advantages:

  • High Availability: Distributes traffic across multiple servers to prevent overload and ensure.
  • Scalability: Easily add or remove servers to handle varying levels of traffic.
  • Reliability: If one server fails, traffic is redirected to other available servers, ensuring continuous service.