sz

Sejan

Blog

WebRTC Architecture at Scale: Selective Forwarding Units (SFUs), Simulcast, ICE NAT Traversal, and Real-Time Media Routing

Home › System Architecture › WebRTC Architecture at Scale: Selective Forwarding Units (SFUs), Simulcast, ICE NAT Traversal, and Real-Time Media Routing
WebRTC Architecture at Scale: Selective Forwarding Units (SFUs), Simulcast, ICE NAT Traversal, and Real-Time Media Routing

Building a 1-on-1 video call demo with WebRTC using standard browser APIs takes less than an hour. However, transitioning that demo into a production-grade interactive broadcast or multi-party video conferencing engine capable of supporting dozens of simultaneous speakers, hundreds of concurrent subscribers, and lossy mobile networks is one of the most demanding engineering feats in distributed systems.

Peer-to-peer (P2P) mesh connections collapse under $O(N^2)$ upstream bandwidth constraints as soon as four or five participants join a room. Symmetric NATs and enterprise corporate firewalls actively block direct UDP transmission. Variable cellular jitter degrades frame rates into unusable slideshows without dynamic congestion control.

In this architectural guide, we dissect how production WebRTC platforms (LiveKit, mediasoup, Daily, and Discord) scale real-time audio and video: evaluating server topologies, traversing NATs with ICE/STUN/TURN, optimizing bandwidth via Simulcast and Scalable Video Coding (SVC), and orchestrating low-latency Selective Forwarding Units (SFUs).

---

1. Multi-Party Topologies: Mesh vs MCU vs SFU

When scaling beyond two participants, the underlying architectural topology dictates CPU costs, latency, and client bandwidth consumption:

Topology Client Upload Bandwidth Server CPU Load End-to-End Latency Max Practical Room Size
Full Mesh (P2P) $O(N-1)$ (Multiplies with every peer) Zero (Signaling server only) Lowest (<50ms direct peer-to-peer) 3 to 5 participants
Multipoint Control Unit (MCU) $O(1)$ (Single stream to server) Massive (Decodes, composites, and re-encodes all video) High (150ms – 300ms encoding delay) Hundreds (Expensive legacy hardware)
Selective Forwarding Unit (SFU) $O(1)$ (Single stream to server) Very Low (Inspects packet headers, forwards RTP packets without transcoding) Lowest (<100ms routing) Thousands (Horizontally scalable)

Why the SFU Won the Industry

An SFU acts as an intelligent Layer 4/Layer 7 media router. It never decodes or decompresses the underlying H.264, VP9, or AV1 video bitstreams. Instead, it inspects incoming RTP (Real-time Transport Protocol) packet headers and selectively routes packets downstream only to clients who are actively viewing that speaker, preserving cloud compute resources while providing sub-100ms latency.

---

2. NAT Traversal: The ICE, STUN, and TURN Protocol Chain

Because the IPv4 address space is exhausted, most client devices sit behind Network Address Translation (NAT) routers or firewalls without publicly routable IP addresses. WebRTC solves this via the Interactive Connectivity Establishment (ICE) framework (RFC 8445):

Client A                                                       Client B
   │                                                              │
   ├──────> [ STUN Server ] (Queries public reflective IP/port)    │
   │ <───── Returns: 203.0.113.45:49152                           │
   │                                                              │
   ├──────> Exchanges SDP Offer/Answer via Signaling Plane ──────>│
   │                                                              │
   ├── [ Direct UDP P2P Candidate Pair Check (STUN Binding) ] ────┤ (Success: Media flows direct)
   │                                                              │
   └── [ Fallback on Symmetric NAT Failure ] ─────────────────────┤
              │                                      │
              └──────> [ Relayed via TURN Server ] <─┘ (TCP/UDP over port 443)

Production TURN Server Best Practices

  • Symmetric NAT Traversal: Approximately 8% to 15% of real-world enterprise and mobile connections cannot negotiate direct peer connections due to Symmetric NATs. A globally distributed coturn cluster or managed TURN service is non-negotiable.
  • Port 443 Fallback: Always configure TURN to listen on TCP/TLS port 443 alongside standard UDP 3478. Corporate firewalls that perform Deep Packet Inspection (DPI) and block non-standard UDP ports will allow TLS-wrapped TURN traffic.
  • Ephemeral Authentication: Use the turn-rest-api time-limited HMAC credentials standard to prevent unauthorized third parties from using your TURN servers as open proxies.
---

3. Bandwidth Adaptation: Simulcast and Scalable Video Coding (SVC)

In a room with 50 participants, participant network speeds vary wildly: a user on a Gigabit fiber connection can receive 4K video, while a mobile user on a congested subway connection will suffer severe packet loss. If the broadcaster sends only one 1080p stream, the slow mobile user causes frame drops or forces the entire room to downscale.

3.1. Simulcast Architecture

In Simulcast, the publishing client's hardware encoder simultaneously produces three distinct bitstreams at different resolutions and bitrates over a single WebRTC connection:

Publisher Client Camera:
  ├── High Layer (1080p @ 2.5 Mbps) ──> [ SFU Router ] ──> Routed to Desktop Fullscreen Viewers
  ├── Medium Layer (720p @ 800 Kbps) ──> [ SFU Router ] ──> Routed to Grid Gallery Viewers
  └── Low Layer (360p @ 150 Kbps)    ──> [ SFU Router ] ──> Routed to Mobile / Low-Bandwidth Viewers

The SFU constantly monitors downstream receiver feedback via RTCP Receiver Reports (RR). If a mobile user starts dropping packets, the SFU instantly drops down to forwarding the 360p layer without re-negotiating the WebRTC peer connection.

3.2. Scalable Video Coding (SVC)

Modern codecs like VP9 and AV1 support temporal and spatial SVC within a single unified bitstream. Instead of generating three independent video tracks (which consumes ~30% more client CPU and upload bandwidth), SVC encodes video into interdependent layers (base layer + enhancement layers). The SFU can selectively drop enhancement layer packets on the fly without breaking the video decode chain.

---

4. Congestion Control and Packet Pacing (TWCC & GCC)

Unlike TCP, which uses retransmissions and window shrinking to handle congestion, WebRTC operates primarily over UDP. To prevent overwhelming network buffers and causing bufferbloat, WebRTC employs advanced congestion control algorithms:

  • Transport-wide Congestion Control (TWCC): The receiving client sends high-frequency feedback packets detailing the exact microsecond arrival time of every packet. The sender calculates packet delay gradient: if packet arrival intervals widen, the sender preemptively scales back its video encoding bitrate before packets are dropped.
  • Packet Pacing: Video encoders output keyframes (I-frames) that can span dozens of UDP packets simultaneously. Blasting all packets into the network interface at the same microsecond causes switch queue drops. SFU routers implement packet pacing queues that smooth transmissions over the frame interval.
  • Selective Retransmission (NACK): When an RTP packet sequence gap is detected, the receiver requests a selective retransmission via an RTCP NACK frame. If the round-trip time (RTT) is lower than the playout buffer deadline, the packet is recovered transparently without visual artifacts.
---

5. Architecture Summary

Scaling real-time interactive media requires orchestrating network, protocol, and codec primitives concurrently. By deploying Selective Forwarding Units (SFUs) that forward media without transcoding, automating ICE/TURN fallbacks, and utilizing Simulcast and TWCC congestion controls, modern engineering teams can support millions of simultaneous real-time video participants with broadcast-grade reliability and sub-100ms latency.

Comments (0)

Leave a Reply ↓

No comments yet. Be the first to start the discussion!

Leave a Comment