How YouTube optimizes video delivery
Tags: performance, webdev, devops
Published: 2021-09-21 12:39:35

## Introduction
YouTube is a massive video-sharing platform with billions of active users so YouTube needs to optimize everything on its platform. YouTube uses a lot of ways to optimize the performance of its apps. YouTube uses a lot of caching and has a very big network of servers over the planet. We will discuss how YouTube stores the videos and how it delivers the videos to its clients.

## Overview
YouTube has a lot of video quality options like you can watch the video at a lot of different resolutions like 144p, 320p, 480p, 720p, 1080p, 2k, and 4k. So when a video is uploaded on YouTube, YouTube makes a lot of copies of the video in a lot of different resolutions and stores it on their servers. So when a client asks for a particular resolution, YouTube responds with a particular copy of the video. A particular video on YouTube is available on a lot of databases located at different places on the planet. 
<br><br>

When a video is posted to YouTube from the USA, it is at first stored at the nearest server to the location it was uploaded from. When the same video is requested in Russia, YouTube requests the video from the USA servers and sends it to the requester from Russia. When the video reaches the Russian requester, a copy of the same video is stored on the Russian server so that it is quicker for the other Russian requesters to receive that video. YouTube might cache the video in the local area only if a certain amount of people request the video from that region. Receiving data from a far location consumes more time than receiving data from a near location, this is why YouTube caches the video in the local server/CDN.
<br><br>

When a video is uploaded to YouTube, YouTube breaks the video apart into very short clips for a better delivery time of the video to the client. It responds to the client with short clips of the actual video, so that it takes less delivery time and also because YouTube could fetch short clips of the video in the background while the client was watching a certain clip of the video. YouTube doesn't need to fetch the clips in proper order, sometimes it fetches clips of the video out of order and then couples them together for the client when the client is watching the video.<br><br>

YouTube splits the video into the video graphics and the audio for content delivery optimization. A lot of people run YouTube in the background and are just hearing the audio of the video, this happens in a lot of cases like when you are listening to a podcast or music on YouTube. YouTube listens for this case and then just sends the audio to the client and does not send the video at all to the client, this saves YouTube a lot of bandwidth and also increases the speed of delivery of the content as just the audio is being sent which is a lot smaller in size than the audio and video combined.<br><br>


## Conclusion
YouTube has a crazily complex infrastructure and the fun fact is that when you post a video to YouTube, it will end in 100s of pieces and at lots of locations on the planet, so at last your video would have 1000s of copies on the YouTube servers. Other video streaming platforms like Netflix, Amazon Prime Video, Twitch, etc. use a similar technique for video streaming. The complexity of the infrastructure increases manifold as an app grows.
<hr>