Implementation Example: Nginx Content-Based Routing

http {
    upstream api_servers {
        server api1.example.com;
        server api2.example.com;
    }
    
    upstream static_servers {
        server static1.example.com;
        server static2.example.com;
    }
    
    upstream admin_servers {
        server admin1.example.com;
        server admin2.example.com;
    }
    
    server {
        listen 80;
        server_name example.com;
        
        # Route API requests
        location /api/ {
            proxy_pass http://api_servers;
        }
        
        # Route static content
        location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
            proxy_pass http://static_servers;
        }
        
        # Route admin requests
        location /admin/ {
            proxy_pass http://admin_servers;
        }
    }
}

Benefits of Layer 7 Load Balancing

  • Content-based routing for specialized handling
  • Ability to implement complex routing rules
  • SSL termination and security policy enforcement
  • Request manipulation and response caching

2. Global Server Load Balancing (GSLB)

GSLB distributes traffic across multiple data centers or regions, typically using DNS.

Implementation Example: AWS Route 53 Latency-Based Routing

resource "aws_route53_record" "www" {
  zone_id = aws_route53_zone.example.zone_id
  name    = "www.example.com"
  type    = "A"
  
  latency_routing_policy {
    region = "us-west-2"
  }
  
  set_identifier = "us-west-2"
  alias {
    name                   = aws_elb.us_west.dns_name
    zone_id                = aws_elb.us_west.zone_id
    evaluate_target_health = true
  }
}

resource "aws_route53_record" "www-eu" {
  zone_id = aws_route53_zone.example.zone_id
  name    = "www.example.com"
  type    = "A"
  
  latency_routing_policy {
    region = "eu-west-1"
  }
  
  set_identifier = "eu-west-1"
  alias {
    name                   = aws_elb.eu_west.dns_name
    zone_id                = aws_elb.eu_west.zone_id
    evaluate_target_health = true
  }
}

Benefits of GSLB

  • Reduced latency by routing to the nearest data center
  • Disaster recovery and business continuity
  • Compliance with data sovereignty requirements
  • Load distribution across regions

3. Service Mesh Load Balancing

Service meshes like Istio and Linkerd provide sophisticated load balancing for microservices architectures.

Implementation Example: Istio Traffic Management

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 75
    - destination:
        host: reviews
        subset: v2
      weight: 25
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  trafficPolicy:
    loadBalancer:
      simple: LEAST_CONN
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN

Benefits of Service Mesh Load Balancing

  • Fine-grained traffic control
  • Advanced load balancing algorithms
  • Automatic retries and circuit breaking
  • Detailed metrics and observability

4. Client-Side Load Balancing

Client-side load balancing moves the load balancing logic into the client, eliminating the need for a dedicated load balancer.

Implementation Example: Spring Cloud LoadBalancer

@Configuration
public class LoadBalancerConfig {
    @Bean
    public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
            DiscoveryClient discoveryClient) {
        return new DiscoveryClientServiceInstanceListSupplier(discoveryClient);
    }
}

@RestController
public class ClientController {
    @Autowired
    private LoadBalancedWebClient.Builder webClientBuilder;
    
    @GetMapping("/client/products")
    public Flux<Product> getProducts() {
        return webClientBuilder.build()
            .get()
            .uri("http://product-service/products")
            .retrieve()
            .bodyToFlux(Product.class);
    }
}

Benefits of Client-Side Load Balancing

  • Reduced infrastructure complexity
  • Lower latency by eliminating an extra hop
  • More control over load balancing logic
  • Better integration with service discovery

5. Adaptive Load Balancing

Adaptive load balancing dynamically adjusts routing decisions based on real-time metrics and feedback.

Implementation Example: Envoy Adaptive Load Balancing

static_resources:
  clusters:
  - name: backend_service
    connect_timeout: 0.25s
    type: STRICT_DNS
    lb_policy: LEAST_REQUEST
    load_assignment:
      cluster_name: backend_service
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: backend1.example.com
                port_value: 80
        - endpoint:
            address:
              socket_address:
                address: backend2.example.com
                port_value: 80
    health_checks:
      - timeout: 1s
        interval: 5s
        unhealthy_threshold: 3
        healthy_threshold: 2
        http_health_check:
          path: "/health"
    outlier_detection:
      consecutive_5xx: 5
      base_ejection_time: 30s
      max_ejection_percent: 50

Benefits of Adaptive Load Balancing

  • Automatic adjustment to changing conditions
  • Better handling of performance variations
  • Isolation of problematic instances
  • Optimized resource utilization

Health Checking and Failure Detection

Effective load balancing requires robust health checking to detect and respond to failures.

Active Health Checks

Active health checks involve the load balancer periodically probing backend servers to verify their health.

Implementation Example: HAProxy Health Checks

backend servers
    balance roundrobin
    option httpchk GET /health HTTP/1.1\r\nHost:\ example.com
    http-check expect status 200
    default-server inter 5s fall 3 rise 2
    server server1 192.168.1.10:80 check
    server server2 192.168.1.11:80 check
    server server3 192.168.1.12:80 check