System Design

How do video sites serve huge audiences? CDN plus peer-to-peer over WebRTC

TL;DR

Video is cut into few-second segments. Instead of the CDN serving every viewer, players watching the same segment share it with each other over WebRTC. If a peer drops out, the player falls back to the CDN for the original, so playback never stalls.

How it works, by example

StepWhat happens
SegmentingVideo is split into few-second segments (HLS/DASH).
Delivery9 viewers on the same segment: the CDN sends 2 copies; the other 7 are relayed between viewers via WebRTC.
A viewer leavesThe in-flight relay drops → the player instantly refetches from the CDN; no stall.
Result (2 segments)5 CDN deliveries, 14 peer-to-peer relays.

How to put it to work

  • When it pays off: live streams or content with many concurrent viewers — CDN bandwidth costs drop significantly.
  • Always keep the CDN fallback: P2P is an optimisation layer; playback must not depend on peers being available.
  • Be transparent: users’ browsers spend upload bandwidth serving others — say so in your terms, and avoid it on mobile data.
  • Tools: P2P plugins for HLS.js/Shaka Player, or commercial hybrid CDN-P2P services.