hello friends welcome to reach goals
today we are going to talk about system design of URL shortener so let us talk
about what are the discussion points so we are going to talk about what is the
URL shortener key things to know what you should not answer in the interview
URL shortner system design in detail right so let's see what's URL shortener
so URL shortener is a simple tool that Steaks a long URL and turns into short
URL so where are it is used it is used to read and it is as it is very simple
and it is used to print in the cards and t-shirts right so before going into
detail I want to discuss about key things to know right so what is base64
so basics 3 4 is nothing but a characters like capital A to Z and small
e to Z 0 to 9 and any two special characters right so this all combination
together forms a 64 characters and these characters are used in generating the
unique key the next is key generator engine so I want to discuss detail about
key generator engine the key generator engine is used in generating the unique
unique ID with the help of base64 right so we will have a separate application
which helps to build the unique characters with the help of base64
characters right so we will be using that in d building the short URL reduced
cache what is the discussion reduce cache is nothing but as it's an
in-memory database kind of a thing where you can all key value pairs key is
something which is unique and can we can use the you can use the user query and
get the key and get the corresponding values right so load balances we are
talk about the load balance in the previous videos so let's say if you have
thousands of requests coming in to the server and one server cannot handle it
right in this case we will have a load balancer in between and the load balance
of what it does is it diverts the traffic to multiple servers to make it
balanced right in this way we can scale the system and we can improve the
performance as well so the next is MySQL database MySQL Bay
database is used over here to just distort the values right or the
collection of records so the next slide is what you should not answer in the
interview so if I ask a question like how do you design a URL shortner most of
the candidates come out with this diagram so the right as usual they put
the laptops computer connecting to the CDN or and from there it connects to the
load balancer and they write couple of services like create account create
short URL and finally they write something like Java hash table right if
you look at this solution technically this will work but
practically is not possible because it cannot scale the system right so keep in
mind when the questions like these kind of questions are asked you have to write
an ax you have to write a diagram or you have to draw the picture in such a way
right it is a scalable system in this we are going to in the in this we are going
to discuss about two use cases one is unique ID generation logic and other one
is the create short URL right if you need more use cases please
comment and subscribe to get in future videos right so let us talk about what
is unique ID right you would have seen the unique ad in multiple cases so let
me give an example with YouTube URL if you look at any of the YouTube URL you
will have something like YouTube top youtube.com slash watch a question mark
then be equal to you will have some ID like this right this is a unique URL so
let us understand how this has been generated right so it has been generated
with base64 characters we already talked about what base64 character is right so
using base64 encode base64 encoding with one character we can create 64 unique
IDs for example if we take a to Z small A to Z and 0 to 9 with 1 characters you
can create 64 characters right let's say if I want to create a unique ID with 2
combinations for example a BAC a D etcetera you can create 4 0 9 6 unique
IDs slide so now if you see into the YouTube they use 11 characters so if
they are using 11 characters with the multiple combinations they can create
around 73 plus q in Chilean unique IDs right so if you
want to know what 73 per screen Chilean unique IDs it's enough for everyone on
earth to produce video every single minute for 18 thousand years right
so technically do you think that they would run out of the numbers technically
it is possible practically practically it's not going to happen right so that's
how they're generated the unique ID unique ID in YouTube similarly we need
to create a unique ID for URL shortener vessel so let's say we can use 8
characters or typically if you see most of the URL shorteners they are using 7
characters right they don't use any special characters all right so you can
use 62 characters and you can create the unique IDs right ok here is a
architectural diagram and with this we can understand how we can how we are
going to create a URL shorteners right so if you look over here you have the
phones or the laptops connecting to the CDN and it connects to the load
balancers and we have three different services like create short URL get long
URL and create account and this can be deployed in an ec2 instance or it can
put in a darker and you can contribute to scale to the level you want right and
here you have a reduced cache right and the ready sketch is nothing but it's a
key value pair which you already has discussed and we have used the table and
we have a key key generator engine then it is can and it's generates the key and
it puts it to the database which is which ASCII and expiration date and
other databases to store the mapping between the a short URL and the long URL
which can be share which can be sharded based on the expiration dates let's talk
in detail in more right so now let's go let's go into each and every services
right so create account is a simple service I'd say you go and put all your
information like your name your company name etcetera and you create an account
and it stores into the user table whenever you want to get a user
information you can query that and get the get the user information the second
thing we want to talk about is create short you are so create short URL is
like you know you create a short you are bypassing the long URL you can give
another other information system point but primarily what you want is a long
you are right so if you call this method I like you know create short URL
bypassing the long URL right so what happens here is it comes over here right
it gets a unique key from this and it also happens with a short short domain
name like you know bitly comm orbitally dot in slash the unique ID right and it
creates that and it takes that and puts into the the table where you have a
mapping between the ID the short URL and the long you are right that's it right
the second thing is second a second infamous second service which you want
to talk about this get long URL right in order to get a long URL we have to give
the short URL right so coming to the website you give the short URL right it
will generate the long URL so how is it going to happen so this call comes over
here right and it goes into the Redis crash to see if the long URL exists
right for the given short URL do they have a long URL let's assume if it is
not there in the first case now it goes into the table and it gets from the
database and it's brings back to the UI right and at the same time it also
stores into the cache right so next time with the same get long URL is called the
same along you are awarded it is already stored there it is cache and it directly
fetches from the release cache instead of going back to the server that that's
will be optimized and the performance is simple and we don't want to query the
database thing so here I have something called lookup right so the lookup is
primarily used for used to figure out which database your data is stored if
you are sharding the database let's say if you have but if you have millions of
records in the database and this database is not going to scale right so
we had a shot at one point of time so let's say we can shot based on the
expiration date so we already have expiration date of a key and you can
short based on that so this lookup this lookup can figure out based on the long
URL or based on the expiration date coming over here we can figure out at
which database the data is stored or in which date with which database the key
value mapping is over here and exactly it goes into that table and gets that
information and brings back to the long you are right so this is a very simple
design and I think most of the guys would have understand what I've been
talking about it and if you want more details you can put in your comments I
am pretty much happy to beyond that right thank you that's it
have on this topic if you have any questions please don't forget it to
comment and make sure are subscribing liking and sharing to get future
informations right thank you have a great day