Netflix YouTube Prime Video System Design on AWS | Video On Demand System Design Interview

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and girls raj here back with another system design for your whiteboarding interview video in today's episode uh we will uh design a on-demand video platform such as netflix youtube amazon prime video as always we will talk about the system design and implement it with aws services so this video will serve as a general system design interview as well as your preparation for your cloud interview all right with that uh let's get started so first things first like i mentioned in all my whiteboarding interview video do not jump into whiteboarding as soon as the interviewer gives you the challenge always put down the features you are going to design as well as the design considerations that the interviewer is expecting systems like netflix youtube they are huge and complex and you cannot design the whole thing in one white boarding session if you think about it this is very similar to your real world meetings where you go to be a meeting with a specific agenda that you want to solve if you don't put down the features and the design considerations interviewer will bombard you with questions and other features that you won't be able to cover all right with that let's write down the features and the design consideration that we are going to cover for this session so the features will cover is users should be able to upload the video into the platform consumer should be able to search for videos and view the videos and on top the platform should be able to detect any adult or explicit content on the uploaded video and should be able to flag it on the design factors the platform should be scalable so this kind of on-demand video platform millions of users are using it so it should be scalable that's like the number one design factor that the interview are will be looking it should be resilient and highly available should be secure and one of the most important factor for this kind of design is it should be cost effective if you think about systems like youtube where you can upload videos for free and there are like petabytes of data being uploaded every day it should be done in a cost effective way right so if it is too expensive then there is no way youtube can be sustained all right with that in mind uh let's start the design process so first things first the user uploads the video file into a storage and let's say the video file is a 4k resolution and the format is dot mp4 so dot mp4 is a very commonly used format if you record a video in your cell phone or your camera it should be saved in dot mp4 and you also upload the title description and tags along with this video so the video itself goes to a storage so let's talk about this storage because uh interviewer is gonna probe you on this one so the storage should be scalable resilient secure and cost effective so whenever someone says storage the first thing that comes to your mind is probably hard disk or elastic block storage in aws and that's not good for this solution because you cannot easily scale a hard disk if you need to scale you have to attach another hard disk and if a file got split between two hard disks you have to manage that splitting partitioning all that stuff it is not resilient like you have to implement resiliency on top so if the attach hard disk fails all your data is gone so you have to implement backup for the hard disk and then if the hard disk fails you have to restore from that backup so a lot of overhead and it is not cost efficient so if you get a hack disk for one petabyte and you are let's say using only 100 gigabyte it doesn't matter you still have to pay for that one petabyte so for the storage we will use amazon s3 or simple storage service it will scale automatically up to infinite capacity the data in the s3 is replicated across multiple availability zone so even if one availability zone fails for some reason you can still access the data it is secure and it is cost effective you only pay depending on how much data you are storing and how frequently you are accessing it so you never have to pay for a huge capacity if you are not using it so we're going to replace this storage with a s3 bucket okay so the video gets stored into s3 but how about this title description and tags so they should be stored in a database because we will use that database to search for a particular video whenever you need to design a system where the design needs to scale to millions of concurrent user and the scale is unpredictable like sometimes it could be 1 million next 10 minutes it grows from 1 million to 10 million right do not use relational database because relational database cannot be horizontally scaled so let's say you have a mysql database running on a server and suddenly the traffic increases tenfold you cannot just add another server with another mysql right just it doesn't work that way because you need to replicate the data you have to make sure the data is consistent etc you can only scale the relational database in a vertical scalable way so you can implement the same mysql database with all the data in a bigger server and then terminate the older server but this is number one not fast number two not cost effective the database should be such that is optimized for searching texts right like if you save this title description tags which are very free formed text fields in a relational database relational database is not meant for searching free form texts so for that reason we will use a nosql database called elasticsearch so elasticsearch is meant for storing text fields as well as optimize search for those field ok so let us put the database in the picture all right so at this point the original video is stored in s3 all the metadata is stored in elasticsearch so now let's talk about the consuming piece even though the video is uploaded in 4k resolution not everyone is gonna watch in 4k resolution uh if you have a cell phone your video will be in 720p resolution if you have let's say 15 inch monitor it can support up to let's say 1080p so depending on different devices the resolution will be different so this 4k video needs to be converted into different resolution to be consumed by different devices so you have two options theoretically one is you can do that conversion from 4k to 720p when the user clicks the video but if millions of users click the same video at the same time your server needs to encode this real time from 4k to 720p which is very resource intensive and depending on the speed of encoding the user might see stuttering right which is not a pleasant user experience so for that reason this video will be encoded into different resolution when you upload this video so for those of you who uploads video to youtube when you upload a video you will see that sd video processing or hd video processing it doesn't get uploaded instantly it says that it the it's processing the video so that's when it does the encoding so let's put the encoding step here in our whiteboard so i'm just showing 720p 960p 1080p but in reality encoded into all available resolutions and to do this we use aws service called elemental media convert and all these different videos will be stored in s3 bucket in different files and the link to the files will be saved in the elastic search along with the tags description title etc so now let's talk about adult content how do you do this so for this we will use a service called amazon recognition you can think of it as a aiml service which gets the video and then analyzes every frame against a pre-sorted database and gives you if there is adult content so amazon recognition is a managed service but if you are strong in iml you can explain it using your own algorithm as well so basically there is a pre-trend machine learning model where it is trained on different images and it categorizes some images into explicit content and when you pass an image it runs that image on that machine learning model and it determines if the image has any adult content so for this after the video is uploaded we are gonna pass the same 4k video into that amazon recognition and we want to do this parallely we don't want to do this one after another right so we will put the amazon recognition step okay and then once amazon recognition runs and if it finds something it can go update the database here as well if it found any explicit content and then it could be blocked from the front end we're going to come to that piece later so all these steps should be done parallelly so how do you achieve that from s3 right so for that we will use a step function so you can think of a step function as a workflow engine where you can implement parallel processing conditional processing loops etc so you don't have to code any of it so how will we implement this so as soon as this 4k video file is uploaded into s3 this s3 will fire a lambda function and this lambda function will trigger the step functions and each of this step will be executing parallely as part of the step function so uh once one one path will be encoding the file from 4k to 720p another path will be encoding it to 960 1080p another path will be calling the recognition etc at the same time another and another path could be as you upload all these title description tags uh into that s3 the step function can grab all that and save it in the elastic search you got the idea all right so at this point we have our video encoded into multiple resolution file we have the title description tags saved in our database so the video is ready to be searched and consumed so on the consumption piece it should not come to this s3 all the time to fetch it right because remember s3 is charged based on how many times you are accessing the file and if this s3's a primary location is let's say us east right let's say north virginia and someone wants to access this video from europe or india every user should not have to come to this u.s east s3 server to fetch the video and it is not efficient to replicate all these different resolution files into s3 bucket across all over the world right because you don't know which video will go viral and will be watched from all over the world so how do you solve this you implement a content delivery network or cdn so a content delivery network or cdn refers to a geographically distributed group of servers which work together to provide fast delivery of the content so it could be a video page api etc so let's put a cdn in front of the s3 so let's say the video is stored in the not virginia s3 bucket and then someone from india accesses the video so the first time the video will be fetched from s3 and then it will be stored in that local content delivery network in india so another user from india when tries to view the same video instead of coming to the s3 it will be distributed from that local content delivery network so not only this is scalable but it provides a very low latency response to the end user so you will be probed on this one because the interviewer might ask how will you implement this s3 cdn website how is this possible right so let's dive deep on this piece so i'm going to flip the board for this part okay so how this works is you have the static website written in html or some other front-end language in another s3 bucket you have the videos stored in the s3 bucket that we encoded in the other side of the diagram so when you go to the website let's say netflix or youtube the website first is shown with the static content and then on the search bar in the netflix or youtube you put in something right you put the name of the video and then you click search at that point it is not a static content anymore so it goes to the cdn and sees if the search result for that search text is already present if yes it comes back populates the screen with bunch of videos if it is not in the cdn it calls an api hosted in api gateway that calls a lambda and then this lambda searches our elasticsearch database with the search term that you put and then it retrieves the name or the ids of the videos that matches the search term sends it back saves it in the cdm for the next user who searches the same thing and once it sends the name or the ids of those videos so your screen gets populated with with the videos related to your search term right let's say these are these videos and then you click on one video let's say you clicked on this video so this video has a particular id again the cdn it goes to cdn and see if that video is already cached or not if yes it plays from it if not the cdn goes to the origin which is the s3 bucket and grabs the video and sends it to you and it also saves that video for that resolution in the content delivery network so the next user that comes in can grab the video directly from cdn and how will the videos be delivered so in the beginning we talked about dot mp4 so the problem with mp4 is you cannot really stream it in chunks like when you try to view mp4 file it downloads whole mp4 file and then you can start viewing but in reality most of the times you don't watch the full youtube video right like i see my videos most of you don't watch till the end so that's the reality so how it works is the encoded videos are stored in a hls format so hls stands for http live streaming so all the encoded video in the s3 bucket are in hls format and the beauty of this is the hls format breaks the video the total video into chunks so maybe a 10 minute video will be broken into thousands of chunks right very small chunks and when you watch this video it delivers chunk by chunk it doesn't deliver the full video in one go so if you stop viewing after five minutes it only delivered the chunks worth of five minutes so it saves the network bandwidth it's faster and cost effective on that note please watch this video till the end all right so i'm gonna flip back the board again so i now flip the board to do this s3 to cdn but you can do this here like because in your real interview either your whiteboard will be bigger um and save some space or you can ask the interviewer hey can i wipe this part or maybe the features and then you can draw use the space effectively so going back to the cost effective feature if the interviewer is a cloud savvy person he or she might probe you how can you cut down cost even more so how you do this is s3 has different tiers um so one tier is the standard tier that's that's the by default tier where the price is highest but remember s3 charges you based on how many how much you store and how many times you access so if you tell s3 that okay some of this video will be in frequently accessed like which videos which are not viral such as most of my videos so what you can do is you can move those videos from standard tier to infrequent access tier so we call it ia or infrequent access where you pay a little less for this and you could do this in an automated way you can have a life cycle policy or you can say hey after 30 days of this video if the number of views is less less than a thousand move it to infrequent access and after it has been moved to infrequent access after 30 days if the view is less than 100 in that 30 days move it to glacier so glacier is kind of cold storage so one thing to note previously to retrieve something from glacier you needed to wait hours but that's not true anymore so you can specify whether you want something to be retrieved from glacier in minutes or hours so you can configure it so make sure you don't get fooled by glacier because for the ogs out there who is watching for their solutions architect associate you know there is a question how long it takes to retrieve from glacier and you guys probably all answers it takes hours but that's not true anymore all right so we did upload we did search we did view we did adult content on the design front we did scalable resilient and cost effective on the security piece uh always attack security in two major ways one is security of the data and second is access authorization like who can access the data security of the data again branches into two different ways one is security of data in transit number two security of data at rest if the interviewer presses you you can say you can put a login user id password layer who can upload the video and who can consume the video right so that's a separate topic on its own how to integrate uh identity providers such as cognito active directory into uploading piece and the consuming piece with api gateway let me know if you're interested i can make a separate video on that because that should be common across multiple system designs and coming back to uh encryption of data at rest all the services i mentioned elasticsearch s3 cdn data could be encrypted at rest using kms uh and for data in transit they are all encrypted using https or tls so that's how you secure this design all right so this is how you implement an end-to-end video on demand uh platform thanks again for watching this video we are very close to 10 000 subscriber mark so if you found this video helpful if you learned something new please click that like button share the video tell your friends about this channel and let's make this channel grow and learn awesome things all right guys and girls with that i end this video i'll see you in the next video bye
Info
Channel: Agent of Change
Views: 5,302
Rating: 4.9895563 out of 5
Keywords: netflix system design, netflix, system design
Id: 7hZXBrI2TjY
Channel Id: undefined
Length: 25min 18sec (1518 seconds)
Published: Tue Apr 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.