---
title: "Load Balancing"
description: "Configure load balancing for TraceRTM"
keywords: [load balancing, load balancer, nginx, haproxy, traffic]
---

# Load Balancing

Configure load balancing to distribute traffic across TraceRTM instances.

## Load Balancer Types

### Application Load Balancer (ALB)

```yaml
# AWS ALB
apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
spec:
  type: LoadBalancer
```

### Nginx Load Balancer

```nginx
# nginx.conf
upstream tracertm_api {
    least_conn;
    server api1:8000;
    server api2:8000;
    server api3:8000;
}

server {
    listen 80;
    
    location / {
        proxy_pass http://tracertm_api;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
```

## Load Balancing Algorithms

### Round Robin

```nginx
upstream tracertm_api {
    server api1:8000;
    server api2:8000;
    server api3:8000;
}
```

### Least Connections

```nginx
upstream tracertm_api {
    least_conn;
    server api1:8000;
    server api2:8000;
    server api3:8000;
}
```

### IP Hash (Sticky Sessions)

```nginx
upstream tracertm_api {
    ip_hash;
    server api1:8000;
    server api2:8000;
    server api3:8000;
}
```

## Health Checks

### Kubernetes Health Checks

```yaml
livenessProbe:
  httpGet:
    path: /health
    port: 8000
  initialDelaySeconds: 30
  periodSeconds: 10

readinessProbe:
  httpGet:
    path: /health
    port: 8000
  initialDelaySeconds: 5
  periodSeconds: 5
```

### Nginx Health Checks

```nginx
upstream tracertm_api {
    server api1:8000 max_fails=3 fail_timeout=30s;
    server api2:8000 max_fails=3 fail_timeout=30s;
    server api3:8000 max_fails=3 fail_timeout=30s;
}
```

## SSL/TLS Termination

### Nginx SSL

```nginx
server {
    listen 443 ssl;
    ssl_certificate /etc/ssl/certs/tracertm.crt;
    ssl_certificate_key /etc/ssl/private/tracertm.key;
    
    location / {
        proxy_pass http://tracertm_api;
    }
}
```

## Related Documentation

- [Scaling](./06-scaling) - Scaling strategies
- [Horizontal Scaling](./06a-horizontal-scaling) - Horizontal scaling
