The #1 Mistake Devs Make When Moving To Microservices

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there this is Julio and today I want to talk about the number one mistake that teams are making when moving into microservices I have seen many things falling to this same mistake over and over again and it's only going to cause trouble down the way so check this out imagine that we are working in kind of a gaming system where we have a bunch of players and players want to be able to send friend requests to each other right so for instance we have displayed over here he wants to friend another player so what we do is we're going to be sending a post request to what we call the friends microservice this is the one microservice that has just one press API to send to receive friend requests and his only purpose is to be able to keep track of these friend requests right but unfortunately it really doesn't know how to send notifications to the users or to the lower players right so for that we have stand up another Mega service that's called the notifications mic service right so this is the one back service that really knows how to send these notifications either via email or via a websockets or whatever it makes sense uh it knows how to send these notifications so of course now now we need to have a difference Mega service somehow communicate the request into notifications right so that we can send notification and so the teams behind each of these effects Services talk to each other and they decided the best way to do this is to stand up just another rest API in this case a notifications maker service right via which the French microservice can send the request into notifications and then of course notifications will take that request I will go ahead and send the notification let's say via just an email and that looks just fine right and this is what teams will usually do to start with uh but I can tell you that right here we have the number one mistake and what is the mistake Goblin right so coupling is a big mistake that people is making when it's starting to make communication across microservice and that is because this is not I mean this is not just an in-proc system where we have just one class talking to another class or one dll talking to an audio level everything just running nicely in just one box now we have a distributed system where many things could go wrong so many things are going to cause that notifications microservice is just going to be down at some point right so now what kind of things I could cause this right so you say well few things for instance a startup box right so have you ever made a mistake so that you have a missing configuration in your microservice right so whenever it starts it right away it just crashes right and you only discovered that in production so that's one of the things that could happen right and that could cause the other microservice to not be able to talk to the notifications maker service for instance uh we could have also networking issues because this is a I mean this is this is the cloud right so there could be multiple issues why for some reason we cannot reach notification microservice or for some reasons the phrase microservice cannot just get out into the network and reach out anything by itself or something in the mail right so network is just unreliable as as we all know and another issue could be dependency issues right so what is notification microservice has an dependency let's say in a database service and a database service for some reason is just overwhelmed with requests so it starts throttling the notifications microservice right at that point notification is just going to slow down completely and it's going to prevent the French microservice from being able to actually get any results from right then we can also have scalability issues right so now we could have notifications microservice being bombarded with a lot of requests from not just this microservice but many other microservices right and so at that point it just it can reply on time and sometimes it cannot reply at all right and so just like that there could be multiple issues that could cause notification microservice from not being able to work properly and of course what that's going to cost is that well the address API call fails and then our friends microservice is just going to fail because it cannot really get the correct response and potentially that's going to result in a in a problem back into the client and the client will now we have a a sad client right so budget problem so how do we get out of this a problem with this big mistake of having coupling between these microservices well let's see so let's say we have again already scenario here but this time our friend like a service is not going to just go ahead and make our best API call into notifications what we're going to do is to introduce another player here which is going to be let's say a queue it's just one one idea right so and the queue allows us to have this kind of decoupling between the center of the request and whoever is going to receive it right in this case we can have now the French micro service send a message via this queue right with the friend requests right so we send a message into the friend request into sorry into the queue and then the queue eventually whenever notifications like the service is ready we'll go ahead and send down the notifications so notifications because we can go ahead and do the job and why this this allows us to do the couple things because well now if notification microservice is just down right which could happen it will happen for sure many times now it is not really a problem for the French microservice but it's still up because now it can keep sending the friend requests uh that we but they are just going to start piling up into our queue right so the cube will always be available right so make sure you get a highly available queue but uh for sure the message will keep piling up there and at this point uh of course I mean we can still send requests and our client can still be keep sending requests interference and nobody is going to be complaining about everything because the system keeps working for the point of view of the friends microservice right and then of course eventually the notification for the service will be back up I mean people fix the issue or the networking has been fixed or whatever it is it can fix it and then we are back into a good a good shape so this is how you fix that that one mistake and this is the approach that people don't really think about too much when they start embracing microservices right because it's logical and media confident that other a world where everything is connected but in this in this case you do want to move into this multi-copley approach and of course that's going to bring in this this one concept which is autonomy which is the main thing that you want to achieve for microservices each microservice wants to be as autonomous as possible right so that you can so you can move fast and you know go into these issues where if one micro service is not behaving well the other Microsoft will not be behaving well as well all right and so now we know what is the right way to to fix this issue let's go into a practical example and see in code how a or what what are the actual issues that you can you could have and how to solving by moving into this other way of working right so let's go into here into my vs code instance over here and so I have a little small simple implementation of the French maker service and notification maker service right so let me show you that very quickly so we have friends here and then we have a our product Cs and as you can see all we have is just one um one endpoint here right this one info here we just listens in friend requests and what he's receiving is just one dto here where we're calling it add friend request add friend request is defined down here it only receives the asking player so who wants to friend and then Target player who which is the player that we want to send the request into right and so that's all that it receives then as soon as as soon as we receive that request we will go ahead and create a The Entity which we have stored over here the friend request entity we assign an ID to The Entity and then just store the request into what is right now just a simple in-memory array right imagine this is a database but uh for now it's just a little in-memory list and so we store that and then we will go ahead and actually communicate uh to our at the other microservice right and we're doing that via this client here which as you can see it's been injected into this endpoint the notifications client and so what's going on in the notifications client so let's do F12 over there and as you can see analytical access for a moment notifications client is just a simple client that's receiving HTTP client avian Independence injection and then all it does is receives the asking player receives the target player and then It prepares a the message or the actor that the request is going to be sent into the other microservice and makes a post called a res API post call uh into notification microservice and of course this is the one thing that that we don't want right so we are making in that Goblin by doing this but then what's going on on the other side so let's take a quick look at uh and by the way we do have notifications contracts which is a place where we have we are I defined in this send email notification a request that our contract that's been sent from one microservice to the other right but then we have the notification microservice down here and this one here all it has is again one endpoint as you can see here's just one map host endpoint right which is under notifications it receives that uh that request right and then it will go ahead and just just logs in the message and then here what we're doing is just using the send grid a library and that allows you to use the send grid Azure Service uh that I mean pretty simply it just connects it to the service prepares a message with a an email message with a from and then a subject and then the actual body right and then who else is going to be sent to and then we'll go ahead and just send the email using sendgrid down into the client right and so and that's the end of it uh but like we said uh this does have problems right so let me go ahead and and close these and close that and let's go ahead and open our terminal here and I'm going to collapse that for a moment and I have two terminals here so one of them is for the friends back service on the left side on the right side we have notifications microservice so let's see what could happen so let's go ahead and Dot net run on friends and let's dot net run all notifications and now we have both microservices up and running and we want to now try out this request so I have prepared a little request here so let me open that up right there and so I'm going to send a request into friends right so that a player one here it wants to send a friend request to player two right so I'm going to go ahead and hit send and well things are working properly and you can see that uh the request was sent on the left side into for player two and then on the right side we saw that we received a notification for player 2 and the notification I mean notification request for player 2 and then the notification was sent via email into player 2 and play.com in this case right so everything looks good so uh so and this is this is what most people will just do but uh here's a problem what if for any reason uh notification maker service is down right so stopping it so now it is completely down so then what happens well let's go ahead and try this out again so now we're going to send the request into French micro service now is trying to reach out into notifications but it is not there so what happens well we get this very nice error on the right side as you can see uh no connection could be made because the target machine refused it right I cannot reach it because well the service is down so it's expected and because of that now we have this awful error that's going to go all the way back into the client and it just broke the entire experience for no reason because in French France was actually able to keep track of that request but it's not it was not able to send a send notification and of course this is not the the only case where things can go wrong right so for instance if we'd also have and let me let me go ahead and stop my servers for a moment let me go now now into notifications into param CS over here and we're going to go ahead and just add a little bit of a delay uh let's say over here to simulate the the fact that for instance notifications could be a having a some sort of a issue that's causing it to just slow down because perhaps there's just so many requests and so it just can't uh can keep a handling the request at the speed that they should so what we're going to do here is just say um weight does that delay right so we're going to be delaying um for time span Dot from seconds I'm going to do from seconds and let's say we're going to delay for 10 seconds right so just 10 seconds just like that all right and now let's see what happens right so we're going back into our terminals over here right and we're going to go ahead and clean this we'll do.net run and then we'll clean this and we'll do the net run also right and so we're going back into our request.hdp file over here and let's go ahead and send a request right so we send request okay into uh our framework service but this time I mean notification Mega service is working on it but it's just taking too much time and foreign not happy with waiting so much time because we had configured a specific timeout right I think it's like three or five seconds and so the time that notification is taken is way too much and of course now we're getting these task contraception the request was canceled due to the configured timeout five yeah five seconds elapsing right so now once again friends microservice was broken because the notification microservice just took too much time to process a request right which is fairly common and so just like this there could be many many other issues that could happen so you don't want to have this coupling at all you want to decouple things completely so let's see what we can do to actually do that decoupling so I'm going to stop my servers over here close my terminal close this and then perhaps close this and close that okay so let's collapse all of this so we're going to do is to use two digs we're going to be using a message broker right and the one I'm going to be using here is called routing queue it's I think it's one of the easiest message Brokers to get started with and one of the easiest ones also to work with your with in your local machine right of course you could be using anything else like service boss or you could use Kafka you could use many many other stuff uh but let's use raving mq in this case and I already have that up and running in my box in Assad Docker container so I want to do now is to actually connect our microservices into private in queue and for that we're going to be using a very nice library that is known as mass transit and it's a library allows you to very easily start interacting with your message work without having to write too much code so let me show you how that works so let's start with the notifications microservice this is the one that's going to receive the message right so let's go ahead and open our terminal over here and say let me close this for a moment and expand this and in notification style I'm going to just say dotnet add package must runs it that Revenue queue so that's the one package that allows you to start interacting with the dragon queue using the mass transit Library so I'll hit enter so that's going to add the reference and we should have the reference in place now so let's collapse this now and now let's go into notifications right so how do you actually consume a message in your in your mega Service uh that is by implementing actually what is known as a consumer in terms of as Transit right so you create one consumer for each of the messages that you want to consume in your receiving a microservice so let's go ahead and create one folder we're going to name consumers like that and then in that one we're going to implement our consumer and so remember that the we will be using the same contract that we'll be using for a as a dto for the request response a contract we're going to be using the same one over here and so to follow the convention we're going to be naming our consumer as the name of our contract plus the consumer suffix so in this case the contract is sent in a notification right so I'm going to copy that name and then on the consumers I'm going to just create new C sharp class and it's going to be send email notification consumer all right and so on this one let me collapse things for a bit uh first thing let's just clean up this stuff let's compare to Fast Company space let's remove this and these are using and then to make this a class a natural consumer what you want to do is just Implement one interface that is called iconsume so I consumer okay and for that one let's do control dot let's use Master acid right there and then you have to write a type parameter here and that type parameter is of the type of the actual contract or the actual message that you're going to consume so in this case that is send email notification okay uh notification just like that okay so I'll do a control dot here and we're going to be using notifications that contracts right so we got that let's go ahead and do a control dot to implement the interface right there so you only have to implement really just one method which is consumed and this is a method that in this context over here you're going to receive the actual message that's coming from a friendly message broker so before we can actually implement the consume method we're going to need two things the first thing is going to be our logger because we need some some objects to start logging messages right so we can see something in the console so let's declare private read-only it's going to be I logger and then off type of course send email notification consumer and it's going to be art logger okay and then the other thing what you need is an instance of eye configuration because we're going to be retrieving uh the key to connect to send grid and other things uh from configuration so let's do private read-only High configuration and then discipline configuration uh like that okay and then we want to of course inject both of them in the constructors let me go ahead and do that quickly generate Constructor okay so that has the logger and then for some reason configuration is down here let's put it back where it should and then Ctrl Dot let's go ahead and add parameters so there you go so now we have both of our things injected right there configuration and logger right there so we are ready to start using them okay so now let's go ahead and Implement our consume method right and so if you scroll down a little bit first thing we're going to do is to actually pull out that message from the context and into our into our class enter method over here and so to do that we're going to just say VAR let's say notification equals context Dot message right and so by the way let's just remember that this this is going to be an async method so async has consume all right so it's going to be like that and now we're pretty much ready to transfer the previous logic that we had uh for the rest endpoint into our consume method so let's go back into program.cs over here and let's go ahead and copy I think we're going to be copying all of this so let's copy all of this right from from CS and into here there okay and so now we just have to fix a few things right so the logger is now the actual logger that we have as available now the Centigrade client let's do control dot so we can do send grid like that for configuration we now have our configuration variable and then for send grid message we'll use this order name space right and once again we'll use the configuration like that and I think we should be pretty much done uh one more thing logger at the end okay but that's that's pretty much it so now we have our uh consume method fully implemented right and so we are not going to be using that rest API endpoint anymore so now but we still need to configure this consumer and actually configure mass transit to the economically activate the bus as communication between this maker service and reviting queue so to do that we're going to go back into param CS over here and so just uh between our Builder decoration and our Optical duration there we're going to be declaring how is we're going to be adding the mass transit services so for that we're going to do builder.services.padmastransit restaurants hit like this okay and then for this one we'll have to do control dot using mass transit select that and then the parameter you're going to receive Here is known as the bus configurator that we're going to be using to configure the bus so bus configurator right so Lambda function here all right and so with that was configurator you want to do two things the first thing is to add your consumers you want to register your consumer so it can be actually use it so for that we're going to do bus configurator dot add consumer all right so at consumer and then here the type of the parameter is going to be the name of your class so in this case it is send email notification consumer all right for that one we're going to do control dot using notifications that consumers there you go so that configures the consumer but then we also need to actually specify that we do want to configure all of the conventions and all of the endpoints and types and objects for having queue all right so this is the this is one thing why must Transit is better than just than just using the revenue queue client because you don't have to be dealing with exchanges and top I mean exchanges and queues and other things a manually mass transit will do all that stuff for you so all you have to do is the following both configurator using rapid and queue and then on this one you want to specify two parameters the first one is going to be the context and the second one is going to be let's call it private mq configurator all right and so for this this is going to open another Lambda function right there all right and so on this one all you have to do is say have it mq configurator dot configure endpoints and then you're going to be sending the the context all right so this line here is what's going to actually like I said create all of those uh objects in the rabbiting queue so that we can actually create that queue that is going to be receiving those messages right base it on there is an email configuration consumer okay and so with that in place uh we can actually go ahead and scroll down and we well we don't need this post over here right so it's time to clean up things a bit see this entire endpoint goes away okay that goes away and this is how uh your class is going to look like I'm actually going to clean up this a bit more then this is all we have in program.cs okay so with any place let's go ahead and rerun our notification microservice to see if we actually get a queue where we can start receiving messages so let's go ahead and do Ctrl J and I'm going to I'm going to clean both sides and let's just do dot net run on notifications for now and so it is going to start and as you can see the bus has started as you can see down here bus has started and so I already opened they're having queue a UI over here okay so like I said I already have the docker container for having you running in my box so that's what allows me to just go into driving you like this okay and if we go into queues now you're going to see that now we have our very first queue so send email notification as you can see it just grabbed the same name for the consumer it grab that name for the queue that it created and so that queue is there just waiting for us to start sending messages from the other side okay so that's that's great and let's now go into the other microservice and also make it so that it can start sending messages so let's go back into Visual Studio code over here and I'm going to collapse this and so let's go of course into friends over here and first thing we're going to do is actually to add the Nugget package right same nugget package that we added a moment ago for the other microservice so let's go on the left side and we're going to do.net at package by transit operating queue so this asset package for the friends microservice okay so Club stat and then what we're going to do if you remember if you go back into prom.cs is that in order to send a message from Friends into notifications we've been using these notifications client right so this this guy over here is doing this what we're doing in line 29 over here so we're going to do is just transform a little bit that class that notifications client so that it becomes a client but to actually send messages to having qbms Transit so let's go into notifications client over here I'm going to do a few thanks so firstly we're going to we're not going to be using HTTP client uh but we're going to be needing a couple of things the first thing is going to be the address the address of the queue where we want to send messages into right so that address is going to be private cons string Q address okay and now well what is the address right so there's a convention in mass transit that allows you to Define uh the the addresses of things like use with a short name so all you have to do is just say queue right and then you want to put the name of the queue so what's the name of the queue let's go back into having Cube over here here is the name of our queue I'm just going to click on it so we can see it can we see it better uh well it's right here actually let me just copy that so send a notification okay so back over here I'm going to just paste that over there okay so that is the name of our queue and that's going to be our Q adders and now that we have that let's also go ahead and bring in the other element which is known as the boss so the boss is the object that you can use to send or poly messages so that others can consume it so private read only right and this is going to be ibos and let's add the missing space using Mr Nancy there and it's going to be just boss okay and of course we need to inject bus over here instead of the old HTTP client so this is the bus equals plus all right so now we have our bus right there and now we need to go ahead and modify our logic that sends the ear notification so that we can actually do it via this bus okay so the first thing to do here is to acquire what is known as an endpoint in the terms of mass transit which is the actual object we're going to be using to send the the message so to do that we're going to do the following so far endpoint equals a weight and here we're going to be using the the boss right so we're going to say buzz and that get send endpoint okay guess an endpoint and this guy what it was is just the address of the of that endpoint right in this case is the address of the queue and that address is going to be just new URI and then here we're going to just say QRS right the variable that we just declared where we have the two others and now that we have that the next thing to do here is to uh Define the well it's going to be the message to send right the message we're going to send the other way but the hardest to be that is the same message that we've been using as a contractio to communicate across the rest endpoints right so same message here nothing to change there and then all we have to do now is to actually change a how we send a message so to send a message what you want to do is just await endpoint Dot sent and onset you're just going to send the notification like that all right and then we remove that and our class has been now fully changed so that we can a you know send messages via mass transit so now let's go back into program Cs on friends right because we have to also make a few changes over here so the first change is that in our Builder Services we have to now register of course our notifications client right because before we were doing this right which is the way that you add a typed HTTP client which is is a good way to do it but now we need to add an actual a Singleton into the service container for the client so we're going to do the follow we're going to do builder.services.ad Singleton and then notifications client all right just like that and of course best practice will be to have an interface for notifications clients so you don't have this concrete class right there but but that is fine for this example right so we just get a Singleton from notifications client and then the next thing is that we need to configure mass transit right just like we did for the other microservice we have to add the mass transit services but luckily it's going to be a bit easier for this microservices but let me go back in the notifications and then I'm just going to copy this code here for notifications into friends and I'm going to replace this with this so for this of course we have to do control dot to add mass transit using mass transit that's cool but in this case we don't have a consumer right because we are not consuming any messages we're just publishing the message and we already did the code for that so we don't need this line over here and because we don't have to configure any consumer we can go back and use the the super default configuration for for rabbiting queue so just by doing this we're using all the different default endpoints a default host default port for Revenue which makes super simple at least for for our demo of course here's where you will configure host port and any other uh things for connecting to routing queue and I think with that we are pretty much done uh our endpoint is still going to receive notifications client because we just registered it via as a Singleton like like that and then the rest should be pretty much the same so with that in place let's go back into our terminal and let's see what happens let's see if we can make this work so I'm going to clean up this and I'll do turn that run okay and we can see that friends is now also connected into the bus right the boss has started four frames and so that means that we should be able to send a request so let's go back into our requested HTTP file over here let's put it at the start over there and then perhaps yes collapse a little bit this so we can see better let's go ahead and send a request from um from friends I need notifications BMS Transit so let's click on send request and as you can see right away the notification was sent so notice on the left side send email notification and on the right side we were able to receive it right away without any issues but this time it went through the rabbit and q bus and perhaps we can confirm that let's let's just confirm that yeah it's all success as you can see this is a tool to accept it all success and then we can also if we go back into writing queue we're going to see that there's a little bit of a spike over here as you can see right here that is the one message that just went through routing queue and then into the automatic service in no time it was super super fast right and so at this point we do have our microservices decoupled so now one of them does not depend on the other and to prove that what we can do is just go back over here and let's go ahead and do the same exercise we did before so let's just stop the notification microservice and let's see what happens so I'm going to stop this right and then let's try to start sending more requests from our friends back server so I'm going to go ahead and send one another send two three four five requests right and so notice that French microservice is not sending any errors right so we keep getting uh our nice 202 accepted as you can see here dual to accept it there are no errors so from the point of view of friends microservice everything is great right which is what we want to see and then if we go down into our having Q portal what you're going to see is that now we have a queue where the messages keep piling up as you can see here we have five messages piling up uh which is which is fantastic right so the queue keeps track of all those messages and whenever a notification Mega service comes back to life those will be processed right just like we talked about before so if you now go back once again over here and we do and let me take this we're going to dotnet run all notifications right.net run notify that happens right away uh our notification maker service receives all of those messages that we're pending right and it will go ahead and send all of those emails which is which is fine right because in this case I mean who cares is an education gets sent to the for the friend request right away right it's not really that that much of an issue here so it's fine uh to let that happen whenever there's time right uh of course this approach is not going to be fine for every single scenario right there are some scenarios where you really want to talk immediately to the microservice and not be a massive message Brokers uh but just raise HTTP request and get an actual response uh but what I find wrong is that most people just jump into that approach right away without even thinking that there's there's other ways to do this right so please consider this other approach as your first option for communication between your services to have full Depot right and if this approach it really doesn't work for you for any reason then start thinking in other ways that you could do that right and so yeah so that's that's what I wanted to talk to you all today so I hope you liked it and if you're liking this video please watch out other videos in my channel and uh that I think are great for professional.net developers so yeah thanks for watching and I'll see you next time
Info
Channel: Julio Casal
Views: 1,738
Rating: undefined out of 5
Keywords:
Id: DPSaHhuSiA0
Channel Id: undefined
Length: 31min 42sec (1902 seconds)
Published: Tue Apr 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.