Software Architecture in Golang: Choreography Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is mary welcome to another software architecture and go video in today's episode i'm sharing with you the choreography pattern so what is the choreography pattern in his book building event driven microservices by adam bellamir he describes this pattern as the one where microservices react to the input events as they arrive without blocking or waiting in a manner fully independent from any upstream producers or subsequent downstream consumers the name comes from the real life dance performance where each dancer knows their own role and performs independently without being explicitly told what to do when dancing therefore the producer of those events is isolated from the consumers receiving the events what are some of the pros of using choreography it allows for loosely coupled systems to interact with each other this means consumers can come and go without any disruption of the upstream workflow another pro is that producer business logic is fully isolated from the downstream consumers what are some of the cons of using choreography it could be hard to debug in cases where the workflow is complex another one is consumers may not need all synth events making the consumers use resources they don't need as usual the link to the code is in the description of this video so feel free to check it out i want to be really honest with you this what i'm going to be showing you is just for eight occasional purposes i don't really recommend it for using it or outside of learning how the choreography pattern works in cases where you were actually using core planning to use this in the real real world you will be using something like maybe apache kafka or amazon kinesis just to mention a few examples a distributed message message box bus so to speak which will be the one in charge of handling events that produces the consumer's concurrency and all of that fun stuff so let me show you this code like i said is in the description the important bit that i want to show you first is the choreographer type that i implemented is actually right here this choreographer type will be handling two things that i want to demonstrate one will be the subscription and publishing of events and a way for the subscribers to communicate back so they can demonstrate that we are receiving the events or they are receiving the events and also doing something with those values so this type like i said it has two different important conventions that are typically when you're working with mutex mutexes and one of them will be the usage of the sink which is right here the mutex is next to the variable that is going to be protecting in those cases if you want the other example will be the one that i have right here like i told you before one is for the subscribers and producer from the producer itself for sending messages and the other one is just for adding and updating a variable let's jump into the actual limitations of this method in this case let's start with subscribe subscribe here uses this important thing that allows you to block or lock or protect the variable that you want to in this case in the section of the method to be only used concurrently by one goal routine at the time so that's why if you notice right here compared to the one that we have below one of them is using lock and the other one is using r lock the biggest difference is that in this case i'm actually modifying the value if you notice right here this channels slice i'm modifying it on the other side if you notice i'm only sending a message using the channels for sending a message to them so with that being said what does this method do is i think it's pretty straightforward it receives no arguments and it returns a channel and like i said it updates the values right here if we look at the publish you will notice that is a bit similar and it uses the things i told you before i read log and run read unlock and it loops through all of the channels that are meant to represent subscribers and it sends a message the other important method that we need to look at it's the one used for closing and what will close is going to be doing is literally close all the open channels that are representing the subscribers and for doing that you just have to loop through all of them closed and then finally just assign the channels to nil to indicate that we don't have any more in case if the user calls publish again okay this is important bit that we have to understand about this type in in particular now the other two events that i want though there are two methods that i want to show you will be the ones that i right here which are the ones that i told you are used for updating the value that we're going to be over there in integral routines that we're going to be implementing in a moment so this one adds a value and similar to what i told you a while ago it's locking using the clock instead of using the r unlock but if we compare it to the other one you will notice that it's using our unlock which is a similar situation again you will see what i mean when we're implementing all of this all right let's jump into actual implementation of this code and we'll tell you step by step what this is supposed to be doing okay let's see so if we jump at main and we start using the type we just have to create let's leave choreographer let's call it o well that's not a good name c and then we want to do something like publish which is a method that we're going to be send using and sending a message we call it message we want to close to indicate everything is completed and then we have to use that actually and implement consumers of those events for doing that i'm going to be using another type called weight group let's call it wg and the idea of this is that we're going to be using it for launching our routines waiting until those go routines at launch and then publishing the message to avoid any race condition and issues with the channels so for doing that we're going to be defining this weight group we call it supes to indicate the subscribers so we just do a four and let's do just three times so zero less than three i plus plus we wait again that's what i was trying to say we wait until all of them are launched we add here the value that we need which will in this case will be one because for each one of the loops we are creating or we are going to be creating a new go routine next we create the routine that is going to be consuming all of that fun stuff we're going to be calling the choreographer and we're going to be calling subscribe so this subscribe method will return the channel that we're going to be consuming now if you remember in one of my videos in order to consume you can use these directives and read the channel until it goes using the four keywords so in this case what i'm going to be do a message range channel and we're going to be only printing f or printing i'll print the ln the message and i want to also print the value of i one important thing with echo routines inside for loops is that you need to pass in that value explicitly or create a new variable scope to the section where you're using that variable so in this case what i'm going to be doing instead of doing that we're going to be changing this function to receive a value and in 64 which will be the value that we're receiving right here and we're going to be passing in an in 64 which is the value in the for loop i'm going to passing down right here so it maps to the received value that is coming from the loop you will see that in a moment so we just added one to make it different and instead of using i we use file so what this is going to be doing is going to be opening trigger routines each gear routine is going to be subscribed to the choreographer and it's going to be printing out messages as soon as they are received from the channel in this case so if we run this it actually before we do this in order to indicate that we are launched already we need to call subs done this way we can tell the weight group that we are finished in this case and you can launch what we were expecting to launch so if you run this code you will notice that nothing is actually happening so why is this not working well typically because the main go routine is finishing before the new routines are actually launched so what we need to do now to add will be to add yet another weight group let's call it 2wg and what this weight group is going to be doing is something similar we're going to be waiting and adding until all routines are completed and all of them are finished consuming all the data that we need so we do something similar we add the value here we wait until we move the weight here and more importantly when this is closed therefore the 4 is going to be finished we called done all right so with that we created a sync weight group for the quarantines to launch in the first place all three and then with another one for waiting for those routines to close after we called the close method okay so if we launch this we should see a message and there it is we have the three messages that we are sending it through they go routine and there were there were trigger because we published the message message and now everything seems to be working so how can we send that data back we have to use the other two methods that we have right there one will be add and the other one will be value let me show you so now the routines are receiving the values from the publisher now if we want to use the other two methods which add a value we just have to update the corruptings to send the data back for doing that we just have to modify this call right here we're going to be calling the c add and passing the value and alternator value because that's the name of the variable and then we just go all the way down here after the weight and we print a length the value the final value will be c value and that's just to demonstrate that we're presenting the values uh back to the original publisher if you run this again you will notice that now the value is the collection all all these which in this case will be six and that's it this is the choreography pattern go hopefully this is useful i'll talk to you next time take care stay safe see
Info
Channel: Mario Carrion
Views: 2,464
Rating: undefined out of 5
Keywords: golang, go lang, golang tutorial, go lang tutorial, golang beginners, golang for beginners, learn golang, golang software architecture, microservices orchestrator, microservices orchestration, orchestration vs choreography, microservices choreography
Id: j0xFVsftG2Y
Channel Id: undefined
Length: 11min 40sec (700 seconds)
Published: Fri Jun 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.