YouTube follows the concept of just-in-time delivery i.e. the video is delivered just in time to the video player, so when the user cancels a video, only a limited quantity of data is discarded.

Design & Implementation

The YouTube serving infrastructure is complicated, with many interacting components, including load balancing, hierarchial storage, multiple client types and many format conversions. However, the YouTube content delivery uses the same server application, called ustreamer independent of client type, video format or geographical location.

The just-in-time algorithm in YouTube comprises of two phases namely the startup phase and a throttling phase.

Startup Phase

It builds up the playback buffer in the client, to minimize the likelyhood of player pauses due to rebuffering(buffer-under-run) events.

ustreamer sends the first 30-40 seconds of video as fast as possible into the TCP socket

Throttling Phase

In this phase, ustreamer uses a token bucket algorithm to compute the schedule for delivering the rest of the video.

Tokens are added to the bucket at 125% of the video encoding rate and they are removed as soon as the video is delivered.

A delay timer for a data block (64KB) is computed to expire as soon as the bucket has sufficient tokens.

If for some reason, the video delivery is running behind, then the calculated delay will be zero and the data is written to TCP socket as soon as possible.

The extra 25% added to data rate as described above reduces the number of rebuffering events.