System designDeep dive9 min
WebRTC & peer-to-peer
A video call between two people doesn't need to pass through your servers, and usually shouldn't. WebRTC lets browsers and apps send audio and video directly to each other, encrypted, once a server has introduced them. The hard parts are getting through home routers, and deciding when a server must step back in.
1 · The problem
Sending a video call through your servers doubles the distance and the bill
An online tutoring app runs 10,000 one-to-one video lessons at the evening peak, plus group study rooms of up to 12 students. The first design sends every stream through the app's servers: the student's camera goes up to the data center and back down to the tutor.
That costs twice over:
- Distance. A student and tutor in the same city still send every frame to a distant data center and back, adding round trips to a conversation where every 100 ms is felt.
- Bandwidth. At about 1.5 Mbit/s per video stream, each lesson pushes two streams through the server: 10,000 × 2 × 1.5 Mbit/s = 30 Gbit/s at peak, in and out, paid for by the app.
Through the server, every frame travels to the data center and back, and the app pays for both streams. Directly, the frames take the short path and the app pays for neither.
WebRTC (Web Real-Time Communication), built into every major browser and available to native apps, lets the two devices send media straight to each other. The app's servers only introduce them, and step back in only when a direct path is impossible or a call has too many people. Most of this page is about the two obstacles: devices that can't be reached directly, and groups.
2 · The obstacle
Two devices behind home routers can't simply call each other
The student's laptop has an address like 192.168.1.23, valid only inside the home network. The home router shares one public address among every device in the house using NAT (network address translation, covered in Private networks: NAT, VPCs & firewalls): outgoing traffic is rewritten to the router's public address and a port, and replies to that traffic are let back in. Traffic that nobody inside asked for is dropped:
Neither laptop knows its own public address, and each router drops packets it wasn't expecting. A direct call needs both sides to learn their public addresses and open the path from both ends at once.
So a peer-to-peer call needs three things before any video flows: a way for the two devices to exchange messages before they are connected, a way for each to learn the public address its router uses, and a fallback relay for the routers and firewalls that won't cooperate. WebRTC builds these from standard pieces.
3 · Signaling
A signaling server introduces the peers, then steps out of the way
Before two devices can talk directly, they must agree on what to send (codecs, resolutions, encryption keys' fingerprints) and how to reach each other. They exchange this through a signaling server, which WebRTC deliberately leaves unspecified: a WebSocket server is typical (WebSockets & long polling). The descriptions are written in SDP (Session Description Protocol), a data format, not a transport:
The signaling server only relays small messages it doesn't need to understand. Once the peers find a working path, media flows between them and the server is out of the loop.
The signaling server still matters for security. It sees and forwards the fingerprints that the peers use to authenticate each other's encryption keys, so a malicious signaling server could insert itself into the call. WebRTC has identity mechanisms for applications that need protection even from their own signaling server.
4 · Finding a path
STUN finds your public address; TURN relays when nothing else works
Each device gathers candidates, possible addresses it could be reached at, and sends them to the other through signaling. ICE (Interactive Connectivity Establishment) then tests pairs of candidates and picks the best one that works:
Candidate | How it is found | When it works |
|---|---|---|
Host | The device's own local address | Both devices on the same network |
Server-reflexive | Ask a STUN server "what address do I appear as?" | Most home routers: both sides send at once, and each router lets the replies in |
Relay | Allocate an address on a TURN server, which forwards everything | Always, at the cost of server bandwidth and an extra hop |
STUN is cheap: a STUN server answers one small question per device and never touches the media. TURN is expensive: it is the relay design from the first section, used only for the calls that need it, such as routers that pick a different public port for every destination (symmetric NAT) and corporate firewalls that block UDP. For the last case, WebRTC implementations must support TURN over TCP and over TLS, which can get through networks that allow little more than web traffic.
Plan TURN capacity from your own measurements rather than a rule of thumb: the share of calls that need a relay depends heavily on whether users are at home, on mobile networks or behind corporate firewalls. Every relayed lesson costs the same 3 Mbit/s through your servers as the original design, and TURN servers should be close to users, because a relay on another continent adds its round trip to every packet.
5 · The media
Real-time media prefers late over lost, and is always encrypted
A video frame that arrives 2 seconds late is worse than one that never arrives: the conversation has moved on. So WebRTC media normally runs over UDP, which delivers now or never, instead of TCP, which holds back everything behind a lost packet until it is resent (How data moves across a network).
Frame 42 is lost | Over TCP | Over UDP |
|---|---|---|
Frame 43 arrives | Held back until 42 is resent | Played at once |
Frame 42 | Resent, and played late | Skipped; the picture moves on |
What the student sees | A freeze, then a catch-up | A brief glitch |
Packets still arrive with uneven gaps. The receiver holds them briefly in a jitter buffer to play them out smoothly. The buffer can only absorb as much variation as the delay it adds, so it is a direct trade: a bigger buffer means fewer glitches and a longer delay before the student hears the tutor.
Three more properties come built in:
- Encryption is mandatory. All media must use SRTP with keys set up by DTLS; WebRTC implementations may not negotiate unencrypted media.
- Bitrate adapts. Endpoints run congestion control and set each codec's send rate to its share of the bandwidth currently available, so quality drops instead of the call freezing, much as adaptive streaming does for recorded video (Video streaming: transcoding & adaptive bitrate).
- Data channels carry other things, such as a shared whiteboard's strokes, over the same connection, in reliable or partially reliable modes.
6 · Groups
A mesh works for a few people; a group call needs a forwarding server
In a pure peer-to-peer mesh, everyone sends their video to everyone else. With N people, each device uploads N − 1 copies of its stream, and home upload links are usually far smaller than download links:
- Mesh: (N − 1) × 1.5
- Forwarding server: 1 × 1.5
At 1.5 Mbit/s per stream, a 4-person mesh needs 4.5 Mbit/s of upload from every participant, and a 12-person study room 16.5 Mbit/s. With a forwarding server, each person uploads one stream whatever the size of the room.
A selective forwarding unit (SFU) receives one stream from each participant and forwards it to everyone else, without decoding or mixing. Senders can upload a few qualities at once (simulcast), and the SFU forwards each receiver the quality it can handle: a student on a weak connection gets the small version, without making everyone else's worse. The older alternative, an MCU, decodes and mixes all streams into one, which saves the receivers bandwidth but costs the server heavily in CPU and adds delay.
The tutoring app ends up with both: one-to-one lessons go peer to peer, falling back to TURN when they must, and group study rooms go through an SFU from the start. Download still grows with the room, since each person receives N − 1 streams, so large rooms show only the active speaker and a few thumbnails at full quality.
7 · In practice
Use peer-to-peer for media, and servers for everything that must be remembered
Peer-to-peer removes the media from your servers, not the servers from your system. The tutoring app still runs:
Server | Job | Load |
|---|---|---|
Signaling | Introduce peers, exchange offers, answers and candidates | Small messages, one long-lived connection per user |
STUN | Tell devices their public addresses | Tiny |
TURN | Relay media for calls that can't connect directly | Full media bandwidth for those calls |
SFU | Forward streams in group rooms | Full media bandwidth, scaled by room size |
App servers | Accounts, scheduling, billing, chat history, recordings | Ordinary web traffic |
Anything that must outlive the call belongs on a server: who attended and for how long, chat messages, shared files and recordings, which means some media goes through a server after all when a lesson is recorded. Monitor calls from the client side too: connection setup time, the share of calls that fall back to TURN, packet loss and freezes are invisible from the server when media never touches it.
Check yourself
4 questions
Takeaways
Remember this
- WebRTC sends encrypted audio and video directly between devices once a signaling server has exchanged their offers, answers and candidates.
- NAT and firewalls block unsolicited traffic, so ICE tries local, STUN-discovered and TURN-relayed candidates and picks the best that works.
- TURN carries the full call for the connections that need it, so measure how many do and place relays near users.
- Real-time media runs over UDP with jitter buffers, because late frames are useless; encryption is mandatory.
- Meshes work for a few people; group calls use an SFU so each person uploads one stream, with simulcast for mixed connections.