Building A Peer-to-Peer Mesh Network

How does Peer5 work? How do we choose peers for each viewer consuming a stream?

These are two of the questions that we’re most frequently asked. To understand the answer, we need to start at the beginning.

Basic Streaming Overview - Experts Feel Free To Skip This Section

Modern streaming protocols (i.e. HLS and MPEG-DASH) are based on the concept of segmentation. A piece of content (like an episode of Game of Thrones) is cut into small slices of video. Each slice (aka “segment” or “chunk”) is typically 10 seconds long but segment duration is configurable on a per stream basis. The basic principle is that you’re downloading lots of small files instead of a single large file. There is a “manifest” file that tells your video player where to fetch the individual segments from – i.e. the URL of segment 1, the URL of segment 2, etc – and your video player makes HTTP requests to fetch these segments. These HTTP requests are routed to the nearest CDN edge server where the segments are cached and downloaded to your computer. Once received, these segments go into a buffer and are extracted as needed and stitched together by your video player to give you the episode of Game of Thrones that you wanted to watch.

Everyone who is watching the same content at the same time needs to download the same underlying segments. At Peer5, we simply ask the question: why should everyone connect to the CDN edge server to download the same segments over and over again when it’s possible (and frequently more efficient) to fetch them from other viewers?

The Fun Part

Peer5 works by creating an elastic mesh network consisting of users (peers) who are watching the same content and coordinating these users so that they can share video segments with each other instead of everyone always fetching the segments from an edge server.

So how exactly does Peer5 connect to individual viewers to create a network capable of delivering video segments? As you can see in the figure below, there are a few steps. First, our Javascript library is loaded in the browser. Then, this client-side code communicates with our back-end (the “tracker”), telling it that there’s a new viewer who wants to start watching stream X. Since Peer5 is built on top of WebRTC, no end-user plugins are necessary.

Peer5 P2P CDN Architecture

The Mesh Network

At this point, we need to create a swarm for viewers who are watching stream X. A swarm can consist of an unlimited number of viewers (part of the reason p2p is so scalable), but usually users won’t connect to more than 15 or so peers at the same time. So how do we decide (in real-time) which are the right 15 peers?

Our #1 goal is to improve stream quality for end users without adding any additional latency. This principle guides our swarm creation algorithm. For Peer5, the “edge” effectively consists of your closest peers. For p2p transfer specifically, your closest peers are determined based on both geography and ISP Network. This solution is a beautifully simple idea, but executing it at scale has taken us 5 years to achieve. The result of all our hard work is the potential to improve video streams using peer-to-peer data transfer.

The Finale

Next, our hybrid switcher determines whether a viewer can load the next video segment better via our p2p CDN, a publisher’s origin server / HTTP CDN or some combination thereof. Once the switcher makes its decision, it has the appropriate source deliver the content to a media wrapper. This wrapper connects directly to the publisher’s video player.

So there it is, an admittedly simplistic overview of how Peer5 works and decides which peers each user connects to.


Peer5 offers a serverless, peer-to-peer content delivery network (CDN) for massively-scaled video streaming, in order to provide TV-grade broadcasting experiences to the Internet. With Peer5, instead of viewers fetching all of your streams from servers, they’ll work together to deliver the highest quality stream possible. This means that Peer5 effectively turns the peak demand issue into an advantage – the more users that watch, the more effective the streaming becomes. By increasing our customers’ streaming capacity by a factor of up to 100x, Peer5 ensures perfect video playback with no buffering for millions of viewers every day.