proxy/docs/proxy-load-balancer.md
The proxy-go load balancer distributes client streams across multiple backend origin servers. It provides a pluggable interface with two implementations:
Both implementations maintain stream-to-server mappings to ensure stream consistency - once a stream is assigned to a backend server, all subsequent requests for that stream route to the same server.
Backend Server Registration:
Server Selection:
Protocol-Specific State:
HLS Streams: Dual-index storage for M3U8 playlists and TS segments
WebRTC Connections: Dual-index for session management
Stream-Level Stickiness:
Health-Based Selection:
The load balancer uses a clean interface-based architecture:
Core Interface: SRSLoadBalancer
Data Models:
SRSServer: Backend origin server representationHLSPlayStream: Interface for HLS streaming sessionsRTCConnection: Interface for WebRTC connectionsStorage: In-memory maps for fast access
Use Case: Single proxy instance handling moderate stream counts
Characteristics:
PROXY_LOAD_BALANCER_TYPE=memory
Storage: Shared Redis instance for distributed state
Use Case: Multiple proxy instances sharing load
Characteristics:
PROXY_LOAD_BALANCER_TYPE=redis
PROXY_REDIS_HOST=127.0.0.1
PROXY_REDIS_PORT=6379
PROXY_REDIS_PASSWORD=
PROXY_REDIS_DB=0
Server Keys:
srs-proxy-server:{serverID} - Server registration (300s TTL)srs-proxy-all-servers - Server list index (no expiration)Stream Mapping Keys:
srs-proxy-url:{streamURL} - Stream-to-server mapping (no expiration)Session State Keys:
srs-proxy-hls:{streamURL} - HLS by URL (120s TTL)srs-proxy-spbhid:{spbhid} - HLS by SPBHID (120s TTL)srs-proxy-rtc:{streamURL} - WebRTC by URL (120s TTL)srs-proxy-ufrag:{ufrag} - WebRTC by ufrag (120s TTL)Server Heartbeat: 300 seconds
Session State: 120 seconds
Stream Mappings: No expiration
| Aspect | Memory Load Balancer | Redis Load Balancer |
|---|---|---|
| Deployment | Single proxy | Multiple proxies |
| State Storage | Local memory | Shared Redis |
| Latency | Lowest (in-process) | Network + serialization |
| Scalability | Single instance | Horizontal scaling |
| Dependencies | None | Redis required |
| Complexity | Simple | Moderate |
| Fault Tolerance | Single point of failure | Multiple proxies |
| Best For | Moderate traffic | High traffic, high availability |