Created
March 14, 2026 15:13
-
-
Save mohashari/3cf7db17b8061b26efd0dea89e179f45 to your computer and use it in GitHub Desktop.
Code snippets — Load Balancing Techniques
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| upstream backend { | |
| server 192.168.1.10:8080 max_fails=3 fail_timeout=30s; | |
| server 192.168.1.11:8080 max_fails=3 fail_timeout=30s; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Client → L7 LB → Routes /api/* to API servers | |
| → Routes /static/* to CDN | |
| → Routes /ws/* to WebSocket servers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # NGINX Plus or use Lua in open-source NGINX | |
| upstream backend { | |
| zone backend 64k; | |
| server 192.168.1.10:8080; | |
| server 192.168.1.11:8080; | |
| health_check interval=5s passes=2 fails=3 uri=/health; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Request 1 → Server A | |
| Request 2 → Server B | |
| Request 3 → Server C | |
| Request 4 → Server A (cycle repeats) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http { | |
| upstream api_backend { | |
| least_conn; # Algorithm | |
| server 10.0.0.10:8080 weight=2; # Higher capacity | |
| server 10.0.0.11:8080 weight=1; | |
| server 10.0.0.12:8080 weight=1; | |
| keepalive 32; # Reuse connections to backends | |
| } | |
| server { | |
| listen 443 ssl http2; | |
| server_name api.example.com; | |
| ssl_certificate /etc/ssl/cert.pem; | |
| ssl_certificate_key /etc/ssl/key.pem; | |
| location /api/ { | |
| proxy_pass http://api_backend; | |
| proxy_set_header Host $host; | |
| proxy_set_header X-Real-IP $remote_addr; | |
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
| proxy_connect_timeout 5s; | |
| proxy_send_timeout 30s; | |
| proxy_read_timeout 30s; | |
| # Retry on errors | |
| proxy_next_upstream error timeout http_500; | |
| proxy_next_upstream_tries 3; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Server A (weight 3): handles 3x more traffic | |
| Server B (weight 1): baseline | |
| Sequence: A, A, A, B, A, A, A, B... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Server A: 45 connections | |
| Server B: 52 connections | |
| Server C: 38 connections → next request goes here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Server A: 38ms avg, 45 connections → score: 38 × 45 = 1,710 | |
| Server B: 22ms avg, 80 connections → score: 22 × 80 = 1,760 | |
| Server C: 45ms avg, 30 connections → score: 45 × 30 = 1,350 → winner |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| hash(client_ip) % num_servers = server_index |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Hash ring with virtual nodes: | |
| [ Server A ] [ Server B ] [ Server C ] [ Server A ] [ Server B ] ... | |
| ↑ ↑ | |
| Request 1 (hash → here) Request 2 (hash → here) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| api.example.com → | |
| US-East: 1.2.3.4 (primary) | |
| US-West: 5.6.7.8 (failover) | |
| EU: 9.10.11.12 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| upstream backend { | |
| server 192.168.1.10:8080; | |
| server 192.168.1.11:8080; | |
| server 192.168.1.12:8080; | |
| sticky cookie srv_id expires=1h domain=.example.com path=/; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Client → L4 LB (sees: TCP port 443) → Backend Server | |
| ↑ Doesn't look inside the packet |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment