Load Balancer Tutorial - What is a Load Balancer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys assalamualaikum welcome to amigos code in this video i'm going to continue the microservice series and more specific i want to teach you about load balances in this video so it's one important concept that you should be aware when building distributed systems and load balances is a must when deploying your applications to production so you don't want to access one single instance via the ip because as you can expect you know the more traffic you receive there's gotta be a way that you need to handle all of that traffic and this is what i'm gonna cover in this video before i crack on literally take two seconds one two and smash that like button if you're not part of the amigos code community go ahead and join the communities growing both discord and facebook group and uh yeah go and share some knowledge and i would love you to see you there also so that you know this course still on promotion so the price will increase in the next couple of days as i'm currently almost done with recordings i'm just recording a couple of intros but this is a course that you have requested and right now is the most popular or actually one of the most popular along with the full stack on my website so literally go ahead and enroll right now so that you can learn how to build distributed systems micro services kubernetes docker containerizing with jib package applications service discovery load balances and all that good stuff that you as a software engineer should know if you are serious about becoming one without further ado let's kick off this video in this section let's learn about load balances and i'm going to show you how to configure one and how it works in detail and also show you the best way of you taking advantage of load balances as you deploy your applications so in a typical application you have the clients this is the internet and this right here let's say that this is a vm somewhere on the cloud with this particular public ip address now so far we've been creating microservices and we have one which is customer so this handles customer registration now this right here so this setup where you've got one client or two clients or maybe three clients using one particular instance to register the application is absolutely fine if you are working on a personal project then this is fine for you to build because the setup it's really easy all you have to do is you spin up an instance somewhere on the cloud with a public ip address and then you can access this instance so let's say that your application is becoming popular day by day and it's actually receiving more traffic so let's say that you went from one or three clients to 10 000 clients using your application the problem that you can have is that one you either need one really big machine i.e one vm with 32 or 60 64 cores lots of ram and other high spec right so this might solve the problem temporarily but still doesn't scale if you go from 10 000 clients to 100 000 clients then basically you kind of have to do something else so usually what you do is you have multiple replicas of your application so in this case customer so here you can see that now i do have three instances so this is one instance another instance and then yet another instance now the problem here is that these clients right here that are connecting to your backend system right they don't know which vm to connect to so if they all connect to this one then this one will run out of resources leaving you with these other instances receiving our traffic or maybe you could actually implement some logic in here that has a list of all ip address for all of your instances so on and so forth but no one does that so how are you meant to access your applications in this scenario well this is what load balancers solve so to fully understand this let's take our architecture that we currently have and let's introduce load bonuses so you see how it works so in here this right here is my private network in here and what i have is an external load balancer so this external load balancer this is the main entry point for any client that wants to connect to your applications so instead of clients going directly into customer or fraud or notification you say the main entry point is through this external load balancer any traffic that goes from the internet directly into these instances inside of the private network will be blocked by the firewall so a request comes in through the load balancer and the load balancer is actually quite smart so there's a lot of things that you can configure within it but let's say that you want to send a request to customer so this request right here is sent to this order load balancer now this is an internal load balancer and the reason is because you might have multiple instances of customer or fraud or notification so the same problem that you are solving from the outside world connecting to your services is going to apply within the private network because these communications right here so this is an internal communication and this is a communication from service to service so you also need an internal load balancer now the configuration between this load balancer and this load balancer will be a little bit different but i'll explain more in details in a second and to be honest this is load balances now there are a lot of things that we have to take into account such as tls certificate management authentication where do you authenticate the requests to authenticate the requests within every single microservice or within this external load balancer determinate tls in here or or through these services in here how do you make your load balancer high available meaning that it's always up and running no matter what is it across region if you're running your application in multi-region or not logging caching path based routing so in here you see that instead of having multiple external load balances for each of our applications we can just have one and then based on the path we can redirect the request to the appropriate internal load balancer now you can see that this actually is quite a lot of work to do and most often when you run applications in production you should choose a managed load balancer instead of managing your own because this right here is the main entry point for your application so if this load balancer dies right so let's say you are managing the load balancer if it dies then you can see that this is a save one incident immediately right so we actually let cloud providers manage this for us so that we can just again focus on our micro services and our own business logic so let's take a look at the cloud load balancing provided by google cloud so in here you can see that they give you some information about it what it is but in here let's just take a look have a look over 1 million queries per second so you can see that if we were to manage our own or set up our own then it can be quite challenging for us to reach these numbers seamless auto scaling so can scale as your users and traffics grow including easily huge unexpected and instantaneous spike so maybe there's an advert going on so you can expect you know a bunch of traffic coming uh your way so this scales out for you without you having to do anything internal load balancer so as i was saying for internal services so you can see support for cutting edge protocols http http 2 grpc https as well load balances so you can see right here cloud logging tcp ssl uh ssl offload affinity cdn and the list goes on and on and on the same with aws so they provide either an alb or gateway load balancer or or network load balancer and they give you some information how it works in here so the same thing as the one provided by google cloud and if i click on features for example have a look so these are all the features that it provides so redirects in here path based routing i am health checks ssl offloading uh clouds as i target logging so on and so forth um http header based uh path routing so this is really important and one that we want and you can see all the protocols listeners the same with the targets whether you want to target to an ip an instance or a lambda so this is a really cool feature for serverless applications so you can see the benefits of a managed database now if we were to uh oh actually i didn't even show you so uh ssl so certificate management so all of this is actually integrated now if we were to manage all of this ourselves you can see that it's quite challenging so also um nginx so this actually provides you a load balancer as well with some of the features that you'll see and you can read more about it how to configure one with nginx if you want to learn how all of this stuff works but basically this is why you really should use a fully managed load balancer when running your applications in production next let's go ahead and learn how the load balancer works let's say that we have two identical micro services in here so they're both healthy now how does the load balancer knows to which microservice to send the request this is done through the load balancer algorithm that you choose now the most common one is round rubbing meaning that it will distribute the request sequentially according to the number of servers that you have so if you have your very first request then the load balancer will send the traffic to this customer microservice so this very first one here the next request will be sent to this order microservice the third request is sent back to this microservice the fourth request is sent back to this one so on and so forth so you can see that it kind of alternates as requests come along now this is one algorithm and it's known as the round robin so it basically distributes the requests sequentially according to the number of servers that you have obviously there are many algorithms that you can choose from so you've got round robin which requests are distributed across the groups of servers sequentially you have list connections a new request is sent to the server with the fewest current connections to clients you've got lease time send requests to the server selected by a formula that combines the fastest response time you've got hash distribute the requests based on a key you define maybe such as the client ip address which means that when the client makes a request it is almost guaranteed that the request will land on the same server then obviously you can take advantage maybe if you are storing for example a session or you are caching some data for that particular user then this could be a good approach but also there is something known as the consistent hashing so this is when the number of servers increase or decrease then obviously because the hash function will change because it's a modulus on the number of servers then you can expect some requests to end up in a different server so here i p hash so this is pretty much what i've just described and then you've got random with two choices pick servers at random so on and so forth and this right here i'm taking this from nginx which is a really great explanation but depending on the load balancer that you choose then you should check the available algorithms that they have to offer so let's say that you have two micro services behind the load balancer so one which handles pdf requests so you can generate pdfs for your customers and another one for handling customer registration now in order for these to be considered as healthy targets behind the load balancer there must be a way of this microservice and this microservice to tell the load balancer that they are in good shape to receive traffic and this mechanism is what's known as the health check so if the health check for any of the micro services is okay ie status code 200 then it means that whenever a request comes from the clients through the load balancer if the micro microservice so let's say that this microservice is healthy then the load balancer will send the request to this microservice the same with this one so if this is healthy it will send the request now in here you can have different ways of defining health checks for your application so maybe this pdf service doesn't have a database so therefore the only health check that you can do is mainly just check that it's up and running so you just send a request to the four slash health endpoint and if it returns a 200 then it's good to go maybe this one the health check might be a little bit different so because this microservice might depend on a database then the health check will be am i running okay but also can i get a connection from this database then if all of these checks pass then we send a 200 back to the load balancer otherwise we say nope we are really having trouble so i cannot accept any traffic so these are health checks all right i hope that you had fun learning about load balances so in this video actually didn't show you how to implement the actual load balancer and change the microservices around so as you'll see later when we deploy to kubernetes we'll get rid of the load balancer which is provided by spring cloud gateway so as i said if you have any questions or anything that you want to see next comment down below give me a thumbs up join the private facebook group as well as discord and uh this is all for now i'll catch you in the next one
Info
Channel: Amigoscode
Views: 82,709
Rating: undefined out of 5
Keywords: amigoscode, load balancer tutorial, load balancer in networking, load balancer configuration, load balancer header routing, load balancer aws, load balancer explained
Id: C2NLPQTvO9M
Channel Id: undefined
Length: 17min 49sec (1069 seconds)
Published: Thu Jan 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.