$npx -y skills add ancoleman/ai-design-components --skill load-balancing-patternsWhen distributing traffic across multiple servers or regions, use this skill to select and configure the appropriate load balancing solution (L4/L7, cloud-managed, self-managed, or Kubernetes ingress) with proper health checks and session management.
| 1 | # Load Balancing Patterns |
| 2 | |
| 3 | Distribute traffic across infrastructure using the appropriate load balancing approach, from simple round-robin to global multi-region failover. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | Use load-balancing-patterns when: |
| 8 | - Distributing traffic across multiple application servers |
| 9 | - Implementing high availability and failover |
| 10 | - Routing traffic based on URLs, headers, or geographic location |
| 11 | - Managing session persistence across stateless backends |
| 12 | - Deploying applications to Kubernetes clusters |
| 13 | - Configuring global traffic management across regions |
| 14 | - Implementing zero-downtime deployments (blue-green, canary) |
| 15 | - Selecting between cloud-managed and self-managed load balancers |
| 16 | |
| 17 | ## Core Load Balancing Concepts |
| 18 | |
| 19 | ### Layer 4 vs Layer 7 |
| 20 | |
| 21 | **Layer 4 (L4) - Transport Layer:** |
| 22 | - Routes based on IP address and port (TCP/UDP packets) |
| 23 | - No application data inspection, lower latency, higher throughput |
| 24 | - Protocol agnostic, preserves client IP addresses |
| 25 | - Use for: Database connections, video streaming, gaming, financial transactions, non-HTTP protocols |
| 26 | |
| 27 | **Layer 7 (L7) - Application Layer:** |
| 28 | - Routes based on HTTP URLs, headers, cookies, request body |
| 29 | - Full application data visibility, SSL/TLS termination, caching, WAF integration |
| 30 | - Content-based routing capabilities |
| 31 | - Use for: Web applications, REST APIs, microservices, GraphQL endpoints, complex routing logic |
| 32 | |
| 33 | For detailed comparison including performance benchmarks and hybrid approaches, see `references/l4-vs-l7-comparison.md`. |
| 34 | |
| 35 | ### Load Balancing Algorithms |
| 36 | |
| 37 | | Algorithm | Distribution Method | Use Case | |
| 38 | |-----------|-------------------|----------| |
| 39 | | **Round Robin** | Sequential | Stateless, similar servers | |
| 40 | | **Weighted Round Robin** | Capacity-based | Different server specs | |
| 41 | | **Least Connections** | Fewest active connections | Long-lived connections | |
| 42 | | **Least Response Time** | Fastest server | Performance-sensitive | |
| 43 | | **IP Hash** | Client IP-based | Session persistence | |
| 44 | | **Resource-Based** | CPU/memory metrics | Varying workloads | |
| 45 | |
| 46 | ### Health Check Types |
| 47 | |
| 48 | **Shallow (Liveness):** Is the process alive? |
| 49 | - Endpoint: `/health/live` or `/live` |
| 50 | - Returns: 200 if process running |
| 51 | - Use for: Process monitoring, container health |
| 52 | |
| 53 | **Deep (Readiness):** Can the service handle requests? |
| 54 | - Endpoint: `/health/ready` or `/ready` |
| 55 | - Validates: Database, cache, external API connectivity |
| 56 | - Use for: Load balancer routing decisions |
| 57 | |
| 58 | **Health Check Hysteresis:** Different thresholds for marking up vs down to prevent flapping |
| 59 | - Example: 3 failures to mark down, 2 successes to mark up |
| 60 | |
| 61 | For complete health check implementation patterns, see `references/health-check-strategies.md`. |
| 62 | |
| 63 | ## Cloud Load Balancers |
| 64 | |
| 65 | ### AWS Load Balancing |
| 66 | |
| 67 | **Application Load Balancer (ALB) - Layer 7:** |
| 68 | - Use for: HTTP/HTTPS applications, microservices, WebSocket |
| 69 | - Features: Path/host/header routing, AWS WAF integration, Lambda targets |
| 70 | - Choose when: Content-based routing needed |
| 71 | |
| 72 | **Network Load Balancer (NLB) - Layer 4:** |
| 73 | - Use for: Ultra-low latency (<1ms), TCP/UDP, static IPs, millions RPS |
| 74 | - Features: Preserves source IP, TLS termination |
| 75 | - Choose when: Non-HTTP protocols, performance critical |
| 76 | |
| 77 | **Global Accelerator - Layer 4 Global:** |
| 78 | - Use for: Multi-region applications, global users, DDoS protection |
| 79 | - Features: Anycast IPs, automatic regional failover |
| 80 | |
| 81 | ### GCP Load Balancing |
| 82 | |
| 83 | **Application LB (L7):** Global HTTPS LB, Cloud CDN integration, Cloud Armor (WAF/DDoS) |
| 84 | **Network LB (L4):** Regional TCP/UDP, pass-through balancing, session affinity |
| 85 | **Cloud Load Balancing:** Single anycast IP, global distribution, backend buckets |
| 86 | |
| 87 | ### Azure Load Balancing |
| 88 | |
| 89 | **Application Gateway (L7):** WAF integration, URL-based routing, SSL termination, autoscaling |
| 90 | **Load Balancer (L4):** Basic and Standard SKUs, health probes, HA ports |
| 91 | **Traffic Manager (Global):** DNS-based routing (priority, weighted, performance, geographic) |
| 92 | |
| 93 | For complete cloud provider configurations and Terraform examples, see `references/cloud-load-balancers.md`. |
| 94 | |
| 95 | ## Self-Managed Load Balancers |
| 96 | |
| 97 | ### NGINX |
| 98 | |
| 99 | **Best for:** General-purpose HTTP/HTTPS load balancing, web application stacks |
| 100 | |
| 101 | **Capabilities:** |
| 102 | - HTTP reverse proxy with multiple algorithms |
| 103 | - TCP/UDP stream load balancing |
| 104 | - SSL/TLS termination |
| 105 | - Passive health checks (open source), active health checks (NGINX Plus) |
| 106 | - Cookie-based sticky sessions (NGINX Plus) |
| 107 | |
| 108 | **Basic configuration:** |
| 109 | ```nginx |
| 110 | upstream backend { |
| 111 | least_conn; |
| 112 | server backend1.example.com:8080 weight=3; |
| 113 | server backend2.example.com:8080 weight=2; |
| 114 | keepalive 32; |
| 115 | } |
| 116 | |
| 117 | server { |
| 118 | listen 80; |
| 119 | location / { |
| 120 | proxy_pass http://backend; |
| 121 | proxy_set_header Host $host; |
| 122 | proxy_set_header X-Real-IP $remote_addr; |