Thursday, December 4, 2014

Breaking YouTube: Thoughts on Psy's Gangnam Style getting all the views

In the past few days, I have seen a few articles about Psy's Gangnam Style "breaking YouTube." In some of the articles, they are claiming that the video broke YouTube, or almost broke YouTube. Here are three articles that I have seen on my Facebook feed in the last couple days about this fascinating event!

http://www.bbc.com/news/world-asia-30288542
http://www.smosh.com/smosh-pit/articles/psy-nearly-brings-down-youtube
Or for those who want to see it in Korean:
http://www.huffingtonpost.kr/2014/12/03/story_n_6260814.html


Looking at these links, I quickly realized that Psy's video didn't break YouTube, it simply found a bug in the code. What happened is that Gangnam Style simply hit the maximum value for a computer integer and the view count stopped there.  Integers are sometimes called "whole numbers," and computers often use 32-bit integers, meaning that the computer uses 32 bits (or 1's and 0's) to store the value of the number. I find it interesting that the view count stopped, because other things could have happened. Consider the following C program:
#include <stdio.h>

int main()
{
    int value = 2147483647;
    printf("2,147,483,674 + 1 = %d\n", value + 1);
    return 0;
}

If you compile and run this program, you will see the output:

2,147,483,674 + 1 = -2147483648

Had YouTube just kept adding to the count past the maximum, we would have seen a negative number of views. This tells us that the number 2,147,483,648 is the max value for a 32-bit integer. It also tells us that something was in place to prevent this number from wrapping around to a negative number. Perhaps there was a check that the the new view count can't be less than the last view count, or perhaps that negative view counts aren't allowed.

Also YouTube could have prolonged this problem by using something called an "unsigned int," meaning that it only used positive numbers (hence doubling the maximum value).

YouTube has now apparently upgraded to 64-bit integers, creating a new max value of 9,223,372,036,854,775,807. With this, each person on Earth would need to watch the video an average of 1,294,508,356 times before it broke again! However, they could have used unsigned 64-bit integers for a maximum view count of 18,446,744,073,709,551,615, but I guess 9.22 quintillion views was enough for YouTube.

So, how long would it take everyone on Earth to watch the video and reach the limit? If we assume each person is only using one computer at a time, we can try to guess.
Psy's video has a time of 4:12 or 252 seconds, so all people on earth would need to watch it for about the next 326,216,105,712 seconds, 5,436,935,095 minutes, 90,615,584 hours, 3,775,649 days, 10337 years, 1033.7 decades, 103 centuries, or 10 millennia.

Let's all open our browsers and watch the video a few more times!