Step by step guide to implementing a load balancer in Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] n [Music] so hello everyone today I would like to talk to about quite interesting topic and that will be the topic of load balancing um you may or may not have heard of this but of course when um at the end of the development of your software when we've done talking about back end and front end and we have this of course serving customers like you can think of like really um big companies like Facebook for example serving customers all around the world you would you can already imagine that it goes beyond writing the actual code that runs on the back end and the front end um you would need to serve this application to people around the world and of course with unpredictable load coming from different parts of the world at different times of the day because if it is morning in the US it might be night in Asia or something like that and so a crucial aspect of um managing like these huge applications will be the concept of load balancing where you have um a fleet of servers um that have to manage incoming requests and these have to of course become U manage this request in such a way that you they don't crash so you would like to Encompass for load and of course you would like to Encompass for um to make sure that the user don't of course experience Laten in their requests and so that's why I would really want to talk to you about today about load balance of course this this is really really not something normally you would have to bother about if at least if you are like a core developer if you're not like a devop for example or doing Sr this is mostly s to probably have to um worry about but I think it's a really cool topic that I would I think it's also worthwhile for you to hear about it and of course maybe understand a little bit how it works under the hood and I have a picture over here as you can see um which I just took out of the internet but it's the whole concept is pretty clear you have a fleet of clients trying to connect to your application over the internet and you have of course on the other end a fleet of servers and what happens is that you when you have this incoming requests you would want to distri distribute this to your servers and you don't just want to let's say you have 10 servers and 10 requests comes in and each of your servers gets one you would really like to have like a robust logic under the hood that um correctly distributes this logic to your servers you don't want to put more load on a server that is already busy when there is a server which happens to be free so that's really the concept and to do this of course a lot of gen ingenus s that people have devised over the years a lot of cool algorithms that people have come up with over the years to kind of like handle how this distribution occurs this are some of the things I've talked to you about today and of course to make it make a little bit more sense we will have to implement ours it's going to be a really dummy one but um um yeah we would begin to understand how this works under the hood so let's let's Jump Right In and start writing some code actually but first of course we have to talk a little bit more about this so normally you would never have to implement your load balance that's just the simple truth of course you would not have to there is no need to reinvent the wheel when there all you have to do more or less is to work on the shoulders of giant who come before you so but we would want to do this to kind of like give us a a um an IT understanding of really what's people for example who are into systems engineer systems development actually do under the hood and again we maybe begin to appreciate the work they do actually um so when it comes to load balancing of course there are some key things I would I think it makes a lot of sense for us understanding the first place um which this will of course give us the premise of our conversation today so that we can really drive it home right so the whole point is that with load balancing more or less we kind of like it ensures efficient resource utilization we don't want to have Ido servers and servers that are way too busy than they should be we want to of course achieve High availability and we want to be able to scale so we also don't want to have 100 servers running let's say when uh if it happens that there a no user or far less traffic to our application of course would be worse in our resources so we would want to of course manage this with load balancing right and then of course the server somehow has to know if the Ser if the load balance modelist has to know of course if the server is alive or not before it can forward request to it with and most um Cloud providers for example achieve this via health checks so more or less the server is supposed to expose an endpoint the load balancer more orless pink This Server via this endpoint it doesn't have to return anywhere any um payload but more or less if it Returns the status 200 100 for example then the load balance knows that this server is alive and knows of course forward request to it when they come in and if it's not alive then of course it deregisters it and knows not to forward request to it so would try understand load balancing like I said and then we'll talk about how we could build ours and we talk about like a couple of um algorithms that people have developed over the years and of course we'll now Implement ours test it out and see if actually everything works we as we expected so I'm just going to come over here I will create a r me and then we can more or less use this to track what we would do today so we talked a little bit about introduction to load balancing um what we could talk about of course is how we can design ours um to do this we need to talk about algorithms um for load balancing and of course we would again go into our implementation and of course in the end we'll test this out to see if actually behaves as we would expect so um we would just jump right into design and then of course we would add more flesh as we PR Pro it and maybe what I can also do is maybe come down here and then before implementation we can talk a little bit about how we would set up our own um um how we would have our own setup and then just to more oress help us understand what we're doing as we do it yeah so design wise um what we will do more we will not of course Implement like a industrywide kind of like um like something you could already Maybe use and replace of course industry standard load balancers but the point of course is that we would Implement something that we can actually test out we're not going to pay too much attention about it because it's load balancing actually a really complex topic and it's trust me not something one person has developed or did develop in the past so but for our own use case we would have um a fleet of servers um and to make this a lot easier would assume we actually just have two servers just to make it make things simple so like I said we simply just going to have two servers feel free to add more um if you want and that's also totally actually we could make it three that's should not be a problem and we would of course have a struct that's going to define a server for us so it's going to have like an address weights and then um connections so we want to be able to track the number of connections every server has at every point in time and so this for this would of course need to create a server pool which which would be like um a map more or less of our servers um so but other than this will of course help us to so would basically use the sync um would more or less use the sync package um from go so so the point would be that we'll try to more or less have a mutex around our servers that's really what I'm trying to I will try to do here and then for our server pool we just Implement a couple of receiver functions so we should be able to add server we should be able to receive um remove server and we should of course be able to get servers More Les to get all the servers we have in our server pool and apart from this I think that's just all we need to implement our server and our server pool and other than that we simply just Implement our our algorithms so for the algorithms there are basically actually three and but I will of course only talk about one for our example so that is of course you might have already heard about this Rand Robin um that is list connection it sounds it means what it actually sounds like and then there is of course IP hash which of course means exactly what it sounds like so we'll talk a little bit about round robbing so for the round robbing algorithm so more or less what this does is is it tries to distribute um request sequentially across across list of servers and um basically it Cycles through the list and then assigns each new request to like your next server in the list you know it's pretty straightforward pretty simple to implement it tries to of course achieve an even distribution to of load of incoming load across your servers but the only down part is that it does not already consider or it doesn't consider the load the current load or the connections that each of the servers has so it simply gets 10 incoming requests and then then you have five servers and then it distributes to each of them two requests each regardless of if there are already servers in your Fleet that are busy and of course with list connection like I said it sounds like um what's what's what the name actually um connotes so it's it assigns each new request to your server um from your fewer active connection your servers with the few active connections so it considers the amount of connections each of your servers already has then tries to consider which load each server should get and so basically um the hash AG more or less um is a hash function um it uses a hash function more or less to map client IPS to specific servers you know so more or less what this does is it tries to ensure that the requests from the same client are always directed to the same server um and of course you can already imagine this could be beneficial for session persistence and c and stuff like that and but of course you can also already imagine that this will not really guarantee um even distribution of load um so but those are some of the um I think these are mostly the the most mostly prevalent algorithms I wanted to talk to you about and but I think for our own case I think what would make more sense for us would be the Leist connection algorithm and that's basically what we're going to implement today so I'm just going to go right right ahead and then we start implementation enough talks already um so what we can really do is we come in here and I will create I'll create a folder here I can call this server one um like this and um maybe I create a new one I call This Server two and um let's just make it interesting you can have three servers more or less um and that's our server three so it's this part is actually pretty straightforward as you can already imagine there's really not much going on here and what I can simply do is I can see it into server one just to implement um the logic that needs to go in there and now I can say go go mode init I can simply call it s one that's fine we actually not going to be doing much in here and so we have our mod file and I can create our main. go it's going to be pretty search forward so package Main and what we want to now do more or less to is Implement our main method and then we simply just copy it to all our servers and then we are done with our servers the most interesting part of course would be to implement our load balance um but anyway so what I would create here I'm simply just going to use the netp net HTTP package and go I'm not using any framework because that's really not the point of this video um so I can create um I can create let's say I can call this home Handler so more or less would we just expose one endpoint and then we would use this Handler just just in singular Handler to handle every incoming request to that to that um Endo that's pretty that's all I would be doing so HTTP do response response writer and of course requ would be a point to HTP do request and we implement the body is really going to be straightforward so I'm just going to do IO do WR string come on help me out here IO do right string thank you and of course I need to just pass the sky and then maybe I pass some string and I say maybe hello word and then I try to print this into a new line and maybe what I can also do is to add some login so we can differentiate um from which server our log is coming from so I'm just going to use the log function and go to keep it simple so I can call this L and then this should be log. new thank you and I N my STD out come on help me out here thank you and then I need to add some fancy string in here maybe I add some square brackets I don't know and I give it a name I call this Z one like this and maybe now I just add some dates like this and what else and maybe I also add some time I think this should be fine out time like this and now what I can simply do is I come down here and I say l. print F and I simply just pass a string and I say running I don't know something like this we just add some dot dot dot like this and I think that's we are done with our hand that's it and now let's just quickly implement the Handler pretty straightforward so is HTTP do handle Funk and of course like I said we just expose one empo which should be the roots and we add our home not homelander home Handler and of course maybe I just throw in here some some message print print line and initially I'm just going to say maybe um something like 1 server one dot dot dot and I'm going to run this at um maybe I say at ports 8081 so this is where our first server would be running and now down here I just do a log. pH come on log. ph and then in here I call my listen and serve listen and serve we not serving ltts today and of course I need to be consistent so 8081 and I'm just going to pass na so the Handler should be fine like this so more or less we are done with our first server that's all the code we need actually and what I can simply do is to copy everything I think this should be fine because in here can do go run men. go and our server should be over up at part one if you doubt me we can open a new tab and call Local Host 8081 um what did we do of course 8081 come on Tony and of course we get hello back so everything is working as expected and let's close that up and close this one as well and so now I can simply come out of here and go into server two say go mode init server two and I go into server two and I create a new file and I call this man go and simply copy this and paste it in here I don't care I don't want to repeat myself and just change some things so this will be server two this will be running at SP 8082 um 8082 and of course we need to change this to two so we are sure and then of course we copy everything again and then go inside of um let's come back again and now we go into server three and we do exactly the same go mode in it server three and in here we just again create our main. go and of course we change this to three and then this would run at 883 this would also run at 83 and we rename this to three and we actually done with our servers we don't care about our servers we only care about our loot balancer it doesn't have to do much here I'll leave that to you to play around but that's really not my concern so now we are done with our servers and now I'm just going to come in here and create a new file this will be amend. go this will actually contain all our um our our logic more or less our load balance so I come out here and I can do go mode in it I just simply call this okay maybe I be professional a little bit Tony so Basilica and load balancer and now do go of course package men and now we can actually start our implementation which of course would be the interesting part so let's go so like I told you I would like to create two structs so one would Define our server and the next will Define our server pool and this of course is really going to be pretty straightforward I call the servers um it's going to be like I told you I'm going to use sync map like this and so in here our server like I said would have just few attributes we don't want to go all crazy here we just want to focus on Implement our simple list connection algorithm for our load balance and that's it that's all we want to do here so I provide an address this address is going to be a string so this is how our load balance is going to add the address to those our server we created over here server one two and three those are the addresses we're talking about here not in fancy because it needs to know where to direct the request to so I add a weight um I'm not sure would uh let's let's just keep going and I have connections and this would be in 64 for now I'm just going to make it simple like this and like I said I would like to implement some receiver functions on our server pool so fun SP for Server pool and I need to implement add server it should take an address of type string and take a weight of int let let's see so this would be this and of course um I would need to add another method called um let's say like say remove server and maybe another one called get servers that's mostly what I want and now let's go in and filling the the logic so for ad server again we're going to really keep it simple so we go through our servers and we simply call the star function we provide the address and now we of course need to add um a semblance of our server more less a type that's going to hold our server and then we simply fill in the attributes say ADR that would be the address and weight of course would be weights like this guys that's all that's really all we want to do here um for now and of course for remove it's also going to be pretty straightforward I um ser and I call the delit method and I provide a key in this case the key is going to be our address that's that's really all and then to get our servers as you can already imagine this is pretty straightforward we are not not done anything so complex here so to get servers of course we need to Pro return return um um a slice of servers so and to do that what I'm probably going to do is more or less to um make an empty slice first um because it might also happen that that um we don't have any servers live that are live more or less um or that are already ready to receive requests so that's really it so now we need to look through Loop through our our sorry our servers and I need to provide a function as you can also look over here and this function is basically what we're going to do to more or less get our servers append them into this MTR slice that we created over here and then we return it to fulfill the requirements of this function pretty St forward so I provide a function like this and so you should take a key and a value um so I'm just going to make this interface I'm going to make this design interface and so let's go in here so what I would need to do is more or less servers I I call the one above and of course I do an append as you can already emerine and then I just simply provide the value but of course I need to more or less type this so I'm just going to provide server this way and actually I don't think I need the key and this will be the call back of course and I can simply return through if everything goes well until this point and then down here we simply return our servers that's it and we are done with the the the the methods we wanted to implement on our server pool so now the interesting thing we want to do more or less would be to implement our list connections um our list connections algorithm and this is going to be pretty straightforward trust me before you close your eyes and open them we are done so I call this lists connections if I can spell and this is going to take um a slice of servers and I'm just going to pass by reference this way and it should also return it takes a slice and it returns one server um yep that's that's what I we're going to do and now I would like to to create a variable that's going to that would tell us the best server to Ser our request I'm simply going to call this best best server and it's going to be of course the type will be of type server and I also need to create another variable I can call this list cons for list connections and um what can I do to this one second so maybe I can actually do is to H I need to find a way that's no matter what we do would never be able to assign a wrong value so maybe what I can do is to do a bit shift for in 64 to more or less get like the maximum value could ever store in in 64 so that would never be wrong so I can do something like this in 64 and I do a bit shift like this um should be use a less than sign of course we use 63 and I subtract by one and this should of course give me the possible Max file I could ever get with this but anyways the what I would want to do now more or less is to Loop through our servers um and now we'll range over our servers of course and why are you complaining no new variables are you of of course Tony that's bad and so what we want to do now is we simply change check the connections of the server so we say if the server that connections if it is less than um our list cons like this um we more or less will time this our best server and and the list connection will simply update it so we list connections becomes the connection of the server like this that's really all we have to do trust me and then what do we do here of course we return our best server that's really all we have to do to implement our list connection logic that's really more or less it um so what we more or less do now is I I also like to implement a health check which is simply just going to Pink our server return a response for us to tell us if it is alive or not and then of course the the last part would be to serve um because again this in the end would be a server ofice own so we then implement the logic that gets the incoming payload use the list um connection logic to determine the best s to sell the request and of course for what this in the end to that server so that's just what're going to do next so now let's Implement our health check again this is pretty straightforward um go 10 one I would say this should not be actually the the the B of what would want to do to this so simply just show this anyways so but what what our server our health check would do more or less would be to make an HTTP get to a given server that's really it so we need to have a function that takes um server as an input and returns a Boolean so if the pink is successful it returns true of course and false otherwise so we need to do a URL pass first I need I would like to pass the URL first just to be sure and then I would of course make this um now I'm working locally of course so I'm going to go with HP this should be totally fine and now I need to just pass my address from the server so that way this is just going to take care of I don't have to worry about if I had this in the first place or not whatever I don't have to worry about this so the person will take care of it for me anyways and they return two things a URL and an error so we can of course handle that so have URL error and you can already guess we have to undo the error of course if this error is not new we return for this is already a failure for us anyways if everything is fine to this point I like to of course create an an HTTP client so this is pretty straightforward so I use the HTTP p HTTP client like this and then I provide um I simply provide a simple timeout this is going to be 5 seconds so this will be five times second like this that's our clients and now we simply do a get so client. get and we provide our URL of course we need the string attribute of it and of course this should return to things again as can imagine so we have here response and error again I'm just going to copy this guy bring it down here so if we have an error of course we return false that's that's that should be it and then I'm going to defer closing this response body so body. close and now what really what I can do here is more or less to focus on the status code I'm going to make sure of course that all our servers return is status 200 okay so now here I return um the response. status code and it needs to be equal to our HTP do status okay like this so this is just one easy way of saying if this equal to this return true otherwise false so we can do all of this with a one lineer that's totally fine and so that's really all we have to do let's just make sure that we are returning the status 200 and yeah this should be fine this should be fine fine okay um so we've implemented our health check so now I need to implement the function that's going to help me actually run this health check so I can call this run health check and it's going to take um our um reference our server pool and I would also like to more or less control the interval of these checks because also if you if you work with a cloud let's say with Azure you get of course to modify configure um how much if you're let's say using like a an application load balancer and you would want to constantly of course ping your application to check if you're life or not you also get to provide the interval so here I would also like to provide an interval it's going to be of type duration I don't want to constantly pink my server and then Bard them out that would be that would be super annoying and the first thing I would like to do more lesia is to sleep for this interval and when I've slipped um when I've slept sorry and woken up I like to look through my servers of course I use range again like we did before and then now I need to again provide a function like this and so I can call this [Music] um um key value again I'm going to make this interface so let's see and then I can decide on what to call them appropriately so I will do um let's say healthy and this would be me actually running the health check so here I provide a server and of course I need to type it so server like this and now I can actually maybe change this to address common and this would be server like this and now we simply say if not Healy so what I'm going to do more or less is if it's not healthy I'm simply just going to remove that server by providing the address um like this and of course again I need to type it like this again um so but the points would be of course um for our own use case it may not really um what's we get an error here one second let me see um this do we have weights let me see oh my bad of course this was not never used here oh let me see this also not used here so best remove this from here my bad um okay so um like I was saying so what I'm doing here as you could see is if this is not healthy I pop this from my um my server Pole again this is not how actual load balancers work uh because you don't want to um of course um again like I said it's much more complex so it's not anyways the load balance is not anyway storing the address pool or the server pool in memory of course it's this is thoughted somewhere somehow within the um the application itself the load balancer so but our own case of course we are storing this in memory but again the problem with popping this this way would be of course if it eventually becomes healthy or If eventually comes back online we don't have that address pool anymore so maybe what you could also do just to help F maybe is to have another um way to kind of like maybe pop it from the healthy list maybe you have to list healthy and unhealthy ones you pop it from the healthy list and put it in an unhealthy one and then maybe for the unhealthy one you kind of like check their health maybe let's say once every two five minutes I don't know whatever you decide and then if it's healthy you put it back into the pool and you could use channels like I've shown you in this tutorial there are so many this this can really be fun but I really want you to experiment this on your own and have fun in the process just do whatever you want to do share with me what um your outcomes are anyways at this point I'll simply return true after I've run my check so that should be it for now so now let's actually go to the main and this would be our load balancer it's going to be pretty straightforward so for our load balancer I would create a struct I'll call this load balancer of course which better name to give it what better name to give it so I need a server pool which of course we already have over here I need an algorithm which is basically going to have this signature here like where is our algorithm this one is this one so I'm simply just going to copy this um next so basically for now anything that has this signature would be our algorithm that's that's really what we're doing we're keeping it simple guys and now I have I need a health check um I just simply call it interval and again it's going to be of type duration like we already did before and now um I need to actually Implement um two methods more one method actually so one would be that method will be Simply Be that's the method that's going to serve our HTTP so I need to create a receiver function on our load balancer struct so this would be load balancer and like I said I'm just going to call this serve HTTP like this um it's still going to have the same signature as our regular function when we try to create a a Handler for our request so it's going to I'm going to provide here um sponse writer and of course um our that's of course our request I can say call this wreck like this wreck like this and now of course it's not going to return anything anyways it's simply just going to serve our HTTP server of our load balancer so our servers um I simply get this from our server pool lb that's common server pool and I call the get service [Music] method I call the get service method and um let's look at our get service method um of course this was not needed this is why you should not do copy and paste guys it's it's bad it can mess you up and anyways what I would want to do of course would be to check check the length of the servers that we returned if nothing was return there's really no way to proceed at this point so I'm just going to do an HTTP error here I do HTTP error I provide my response writer and then I provide a string and I can see something like there are no servers available like this and of course I need to maybe provide a status code I need a service unavailable like this so if there are no um servers if we can get any servers we simply of course do this um but if we do then here I need to create a server um this would be um I need my algorithm and I need to pass my servers like this so this will of course um return our a server for us so but of course if you remember we have this algorithm over here so this going to um return in this case our lists this guy list connections algorithm that's what it's going to return for us for this server and so of course we created our servers as pointers so we should be able to check if they are na if they are knowable so if it is if it is not not equal to right now if it is new if it is new then I'm also going to do something like this and of course I need to remember return just so our code doesn't keep proceeding after it hits this Arrow so again I'm also just going to return like this here so what I'm going to do more or less is to um um more or less adjust the connection of this server and of course in the end I I will also um return this to his actual value so I'm just going to adjust the value by one and then in the end I'm going to return it to this actual value so I use the atomic um package in in go and then I need to add because my type was 64 again there are a couple of things you have to pay attention about because this again specific to uh yeah you can also see it over here so if you target 32bit platforms there is a bug but um that's not the case for me but of course it's good to know that this would be what that's you could run into a problem if you're R this on it to bit system just in case you start wondering if something went wrong anyways I need to provide a point that's to what I want to adjust so I'm going to adjust the server connections and I'm going to adjust this by one and of course in the end I'm just going to do the exact same thing and return it back to his actual value so I'm just going to copy this guy and down here I'll defer this and then I change this to minus one and now so what I would simply want to do more or less is now take care of our serving of HTTP and then that's it we are done so um the last thing of course would be to use a reverse proxy to then forward the request to where we want it to so I can use the HTTP utils help me here package and then I use the new um single this one and of course I need to provide the URL and in here of course I can now provide scheme um come on um since we are running this on Local Host so our scheme is just going to be HTTP and the host would be of course our server address like this and now when we have this we simply um of course this returns a proxy if I H over this you also see it returns a reverse proxy and I simply just call it a proxy so with our proxy we now serve our request so proxy dot serve HTTP and then provide our response writer and our request like this and that's mostly it and I think that's mostly what I would want to do I'm probably also going to add some metrics but I think that's of course does not it's not going to affect what we're doing so now we'll bring it all together so I create a men function over here and it's going to be pretty straightforward guys so I have a server pool I create a server pool and of course I'm going to create um instantiate it this way so we have a server pool like this and the algorithm Alor redem that I'll be using would be my list connections I don't want to invoke it and I need my interval so my interval would be um let's let's make it 5 Seconds um come on let's make it 5 Seconds our interval would be 5 seconds and I need to add some address pool to our to addresses our address pool more or less so now I can provide um so I need to use my server pool this way and call the ad method that we created and I need to provide an address and I give it um weight so I'm just going to give this one one so but what I would put here would of course would be the address of the servers we created initially so this would be HTTP um or maybe I'll keep it simple I say 1 127 because again I remember what I did with the URL person so that's totally fine 0.0.1 and the first one was at 881 and I'm just going to copy this a couple of times actually three times um so this would be two and this would be three and of course this would be two two again and this would be three so we have all our servers and we've added them and I need to create a load balancer like this um come on help me here load balancer like this and then my server pool of course will be server pool like this and algorithm would be algorithm and interval would be come on interval will be interval like this and now we have our load balance and what I would want to do more or less is to use go routine to run our health check in the background so I say run health check I provide my server pool like this and I provide the interval and that's really all we want to do and then at the end of course we need to now serve our request so I say http handle I provide of course I'm going to for now we will allow an incoming request of course to the roots and then we forward this our loot balancer like this pretty straightforward guys and now I'll just want to put some messages down here so I do something like print come on print line and then we just so we know when something is happening and we can say star load balancer server at Port so this is going to run at 8080 and of course we now have to start our server so again I do a l. pH and in here do HTP that's listing and serve and again we said it's going to run at P 8080 and it's going to have hand for hand just going to provide nail and really that's basically all we want to do I'm just going to show this down and so we can take a look so if you look in our main method it's pretty straightforward again we could add some metrics um I already did um I did this of course and I can also share this with you if you're interested but of course that does not in any way affect what we want to do which is of course load balance so we have this here and now what I would want to do in the end would be to maybe create a new folder I call this client and so I'll CD into client so what I want want to do here is to create a client that's going to infinitely forward request to our load balancer server and then we see how it distributes request to our load balance our servers our Fleet of servers then in this case of course are just three so that's that's really what we want to do here and of course it's really going to be pretty straightforward I'm just going to actually go to C over here and copy this heal check I don't care I don't want to repeat myself and then in here I do a go go mode init and I call this clients like this and I can touch amen. goo of course and then inside here package men and I provide this health check here and of course I need to provide a m method so we can handle all of this and our head check is going to um ass simply just going to we don't care simply just going to take an address which is going to be a string that's really all it's going to do it's going to take an address and then the rest is going to continue so here will just be address that's totally fine we don't want to make this complicated you don't have to there's really no point and now we can maybe say um maybe what I can do here to track the number of request we are serving we are making I can create one here a value here and I just call this one and now I create my infinite Loop so we would infinitely make request to our load balances server and then watch distribute the request that's what I want to do so print Line Print line and I can say serving I say total number of requests served and I can either way I don't want to format I don't want to waste my time anyway so like this and we now do a health check simply call a health check and I'm just going to provide this Local Host 8080 remember the comment request comes into our load balancer server and then load balancer server takes care of the distribution under the hood we don't want to over concern ourselves with all of that anyway and here I simply um increase our n like this so that's totally fine and of course in the end I think we are done and maybe what I can do is of course make this a little bit smaller so we can have a lot more room so you can see what we are doing and so now I can of course cllean my tab I can split my my screen this way and maybe even split it further and maybe of course split it even further yeah I have to I can so now let's go into server one and we run our server men. go and now I can actually put this up so you see it's actually running at part 8081 and we go into server two and again we run go run men. goo and this should of course run at 8082 and of course we go into our third server and again go run men. go and this is up so now what we can simply do is maybe run our men so go run men and then Watch What Happens let's see if we run into some errors so it is up and now let's see if actually makes um so now we see it's actually pinging our servers so it's is doing the health checks as you can see over here and that's totally fine so maybe of course so now test it out we would maybe open a client and then watch all of this together and see if it's actually doing exactly what we wanted to do so if you look over here you would see that of course we are still s our request still ping in our servers as you can see and now what I can do M would be to bring this over here and now I see the into load balancer and then here of course I should see our clients I go into C cents and now let's run our clients and now you see of course this is forwarding the request to this guy and then this guy is then of course Distributing the request to our three servers over here if I terminate if you look over here of course you can see the number of requests being served if I cancel this everything stops again but the cool thing as you can already see here is that no one server is allowed or or handles all our request if I make a simple um curl here so let's make a simple curl to local hosts 8080 one of them simply handles the request right so um if you doubt me we can maybe um go into each of them so let's go into server one so let's say Hello World from server one for example and then we go to server two and we simply do the same server two and now let's go to server three and then we also do the same hello word server 3 like this and now what we can do of course is I will stop the load balancer server and and I stop each of these servers this so let's start this guy and let's start this guy as well so and now let's start this one and then we watch it pain our servers and just give it a couple F now we see it Pinks All the servers as before and now let's make the Cur again and now we get a request from server two now we get from one from one from two from one from two from three you see here like no we don't get the same request from one particular server we it's really completely distributed and again we can of course run our clients this way and then again should be able to distribute our request again I am not doing this so we replace this with like industry standard load balancers but I just felt it made sense that if that we know about load balancers we have an idea of how it works um and then we can maybe like I said begin to appreciate the work system um L system Engineers actually do anyways guys that's all I wanted to show you in this video Stay learning and of course keep following us subscribe of course if you're new I try to bring to you quite interesting topics and um you will there's always something for everyone to learn feel free to leave your comments leave your questions if you have any and um I will respond I always respond I try to do that actually so I will always respond as fast as I can and yeah guys that will be it for today until next time have a lovely evening stay positive stay hungry for knowledge and yeah bye-bye [Music]
Info
Channel: Easy Dev For All
Views: 50
Rating: undefined out of 5
Keywords:
Id: eSVP7VbSLUI
Channel Id: undefined
Length: 55min 26sec (3326 seconds)
Published: Wed Jun 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.