Microservice | CQRS Design Pattern with SpringBoot & Apache Kafka | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Java techie in today's market microservices have become the go to solutions to build an application however the execution of microservice architecture has several challenges which can be only solved by a design pattern so in this tutorial we'll demonstrate cqrs design patterns which basically helps to scale independently read and write workload of an application and a well optimized data schema okay so we'll go with the detailed explanation including problems use case and example all right so without any further delay let's get started [Music] thank you foreign [Music] stands for command and query responsibility segregation if I'll simplify the statement further it clearly says that segregate query responsibility which is nothing read operation and common responsibility which is nothing all the right operation in your micro service so in simple word cqrs suggests us to segregate read and write operation to different service rather than mix up in a single micro service okay but what is the need of it why do I duplicate the service to just serve different responsibility isn't it so to answer this let's understand the challenges we faced without cqrs then we'll get a complete picture why do we need this secure s design pattern so let's take an example of flip cut e-commerce application where it has one product microservice with all kind of crowd feature like purchase the product view the product update and delete the product okay as you know it has a large user base who frequently access the Flipkart now let's say Flipkart started to sell on big billion days or with a big discount then that time out of 100 users 30 percent users will visit the app to purchase the order and the rest 70 percent will just search or view the products to make a decision letter this is obvious right we don't log into Flipkart to only purchase I have options to view them as well if I like the product I'll go for it otherwise I will not but being a developer if you try to figure out the ratio of view product and purchase product it will something look like this or in other words Flipkart has more read requests than write request so here if Flipkart would like to handle the high load of trade request separately then the relatively low right request then that's not possible because we have already mixed up both read and write operation in same micro services that is product microservice so here is the problem you can't scale your application independently for read and write request not only e-commerce application let's think about any social media apps like we used in our day-to-day life like Facebook Twitter and Instagram every day we search or view the feeds from these social media rather than add a new post so here also we have more read requests than write request right now the problem here we cannot independently handle the load or we cannot independently scale our application now how we can overcome that in microservice architecture only by using cqrs design pattern I will come to that point how will implement it before that let's move into the next problem in current architecture okay so let's assume the above product microservice also integrated with other microservice like user service and purchase order service then all the three microservices so the mapping with a unique field let's consider order ID as a unique field for now now when I want to fetch all the order details of an user then I need to either write multiple join query or I need to do multiple rest API call which in turn affect the overall rate performance so if you need to write complex queries to read the data and do this on the same database you write to then this will impact its performance again this is another challenges now let's move to the next challenges let's say you want to add additional security while writing the data to the database again you need to compress and decompress your payload for both read and write operation which will definitely lead into performance issue so this is another challenges now if you list down all the challenges we found in this current architecture we cannot handle the high load for a specific requirement we cannot deal with the complex query and we cannot add the additional security or transaction related stuff if you will follow this particular architecture now how we can overcome the challenges or how do you design your system to cater these specific use cases that is where the cqrs design pattern come into the picture okay now let me tell you how you can Implement cqrs in your microservice it's very simple you just need to segregate read and write operations so for that we'll create two different microservice one for read operation and one for write operation now we just need to Define all the gate or read operation like face the product API inside this product query microservice which will query to your DV okay all the read operation related stop will keep inside the product query microservices and Define all the right operation in product command microservices like post put and delete API where you can give commands to modify the data into the database so in simple word you can keep all http method in common service who usually modify your DV if not then keep it inside the query service that's simple isn't it now since we create two different microservices they both will host to the different server but if You observe we do all the right operation in product command microservice and it use its own database right now in product query microservice if I'll try to fetch all the products then I won't get any record because our Command and query micro service are not in sync they use their own database so basically we need to sync both the micro service to our data in consistency so for that we can use any messaging system like Kafka rabbit mq or redis pops up but let's use Kafka for our use case so now whenever you do any write operation in common service then immediately publish that event like either create product event or update product event to the Kafka so that query service will consume that event and will store those information to its own database so in that approach going forward your query service have all the up-to-date information with him that's pretty simple right this is how we can Implement cqrs design pattern in our microservice so I believe this cqrs design pattern is pretty clear for all of you if yes let's go one step ahead and start implementing the cqrs so we'll follow the same steps as per the diagram we understand okay so let's go to the IntelliJ IDEA then as per the architecture we need to create two different microservice product command microservice and product query micro service okay so in query microservice you need to write all the get specific HTTP method and in command microservice you need to write all the HTTP method which will modify your database I mean post put and delete okay now I will open my intelligent idea then we'll quickly create a new project specify all the required field now let's add all the required dependency so I will add web dependency which is spring web then we'll add the GPA dependency because I want to interact with the DV I want to use MySQL here if you have any other database you can give a try with that and also I will specify the lumbar dependency next finish looks good so we created the command microservice now let's create the new query micro service just create another project now again you can add all the required dependencies so I'll add lumbok I will add web dependency then here also I want to integrate with the database so I need jpa I can use the MySQL since it is already installed on my machine so if You observe in the architecture your command microservice and query micro service can point to any database irrespective whether it is rdbms or nosql does not matter okay because both the micro service having their own database they can design in such a way that it can manage on all the perspective okay I mean let's say for the query service usually you do the get call or we just retrieve the data from the DV right so in that case the nosql database is the better choice here so you can use that but for this example I am using MySQL in both the case go to the idea now all good let me import this project so build is succeed as per the architecture we have created two different micro service right now in the command microservice we'll Define this endpoint so first let's start with the command microservice then we'll write the query micro service just go here go to the command let me create the package couple of package here service that's it right so first let me create the entity new Java class I'll name it product we'll Define couple of field here ID name description and price since this is my entity I need to annotate at the rate entity then at the red table so I will specify the table name as this is my common service right so I will just Define product command name of the table that's it okay and I need to Define ID as a primary key so I'll Define ID and I want it to be Auto generated so generated value I mean this is the simple crowd operation we are writing here okay so we already Define couple of crot in other videos so there is not something new I am doing here so we created The Entity now let me quickly create the repository so first create the package create a class here let's say product Repository okay I need to define the class now this need to be extends from jpa Repository and different type as a product which is your model and ID up type long fine now since we have defined the entity and repository let's configure the data source properties in our application.properties file whatever is comfortable for you just add it here so we have defined the driver class name URL username and password so SQL hibernate ddl Auto and dialect and server.port that's fine so now let's create the service I mean it's fine you can directly play with the controller but since we created the service package let let's create a service class okay so I'll name it something like product command service so modify the name here there is a spelling mistake now here you will write the method which will interact with your DV which is nothing your repository right so first step I need to annotate this at the red service then okay let me Zoom this then I will just inject private repository which is product Repository using Auto add now I'll write a method who will save the object to DV okay because this is the command service here only you need to write those methods which will modify your DB okay so I will just write a method product which will return me the product or better let's let's name it create product take the product object as input then simply call return repository dot save the product object give the product object that's it right similarly you can write for update and delete let me write for update public written with the product object update product get the ID based on what you want to get the existing product and then you want to give the updated product value which you want to update right so first step you need to get the existing product from the DV then you need to modify that so what I will do dot find by ID give the ID then it will return you the optional so just get the object of it okay so we need to send the long here not ID I mean int so it will return you the existing product object which is there in your DB so just rename it existing product now you want to modify this product object by taking the latest value from this input argument and then you want to save it so what I'll do I'll just add existing product dot set why this Setter method is not coming okay so in our model class we have not defined the lumber specific annotation right so we need to annotate your other data argument Constructor at the read no argument Constructor fine now if you go to the service if you type set name yeah we are getting the setter method get the value from the product and set it product dot get name now similarly set the other field set the price and set the description once you update the product existing product then Simply Save It that's it right so we have defined the service now let's quickly create the controller class go to the project create a new class now just annotate here at the rate rest controller then I'll Define the root URL request mapping then I need to inject the service here private inject using Auto add fine now I just need to Define two endpoint to create the product and to update the product so what I'll do I will copy the method name to save our time I will copy the method signature and I'll just paste it here now this should be common service dot create product and give the product object now similarly let me copy for the update product I will copy the signature then simply we'll call return command service update product and give the value that's it right now I just need to Define The annotation so this will be my post mapping because I am inserting the object to the DV I am inserting the product to the DV since I will pass the Json object I need to annotate here at the rate request body then I need to annotate here at the rate boot mapping because I am going to update the product so this I need to annotate are the red request body this will be path variable because I want to pass the input as part of the request URL itself so I will just Define ID that is the value I am going to pass as part of the request URL all good so we are done with the command Service as per the architecture we have created one microservices which is product command microservice and we have defined post and put method now let's create another microservice product query microservice then we will just Define the get endpoint okay just go to the project product query service here also I just need to use the entity and repository so what I'll do I will just copy the packages from command service to save our time so I will just copy this entity and reposited these two packages copy this go to the query service just paste here fine we have the entity class here product and we have the repository class here let me input this so it seems the package is incorrect so let me remove this and fix it in the comment service okay so we have not changed it right so yeah in whole project com.java techkey that is what we usually Define our root pack structure refactor it everywhere okay com dot Java techy then we have all the packages so it should be the same structure in our query service that is what usually we follow right fine so you have entity and we have repository now in the entity we just need to change it to the table name so what we need to keep here query because this particular entity will be deal with the query service okay so there is no such rule that both in common service and query service The Entity structure should be same nothing like that okay so whatever the field you want to query those field only you need to persist in your DV so there could be a chance from the command service in your entity you could send 100 field or you are persisting 100 field only send those field to the query service which you want to display or which you want your user can get those value okay so that is how you can design so that is what the advantages of using this cqrs right to avoid the complex query you can design your schema you can design your entity your own way okay so in query service also we are just keeping the same value to just show you the demo that's it okay now we have the repository all good let's quickly create a service class now I need to annotate this at the red service then simply here I just need to inject the Repository fine now here I will simply write a method to return the list of product object okay repository dot find all you can also write couple of Select method I mean get the product by ID or some aggregate function you can write but let's make it simple I have just written one method to get all the product from DB now I'll quickly create a controller package then create a controller class and it run it here at the rate request mapping which is root URL slash products then I need to annotate at the red rest controller fine then I need to inject the service here private product query service then I need to write the end point Okay so public who will return me the list of product get products or you can give any name okay fetch products let's change it to the fetch all products service Dot get products and you need to Define your gate mapping fine so root we release this all good we have created The Entity we have created the repo but we need to configure the data source right so I'll copy the properties from application.properties of command service just go here open the properties file paste it so everything is same only I need to change the port 9191 we are running our Command service 9192 we are running our query service okay or to make it simple I'll make it 9292 that's it so we are good as for the architecture we have created these two micro Services query micro service and common micro services and we also segregate the methods in different micro Services right create product and update product which usually modify our DB we keep them inside the command service and where in the query service we keep the method to only do the retrieval operation right so now what I'll do since we created both the microservice Let's test these two micro service then we will understand what is the problem and what could be the way to figure it out so just start both the micro services command so go to the main class start it similarly go to the query micro service go to the main class and just start it so both service are up and running 9292 is our query service and command Services 9191 okay now in the comment service we have written post and put method okay so let's verify them then from the query service we will face the product and we'll see how the result is getting okay go to the postman create a new request post http this is the URL and we need to pass the body so change it to the Json we need to pass the name description and price and ID will be Auto generated right so let me send the request yeah record got inserted and we got the result with the id1 let me add some new field let's add something like mobile electron is device price is let's say 10 000. send the request the second object is also inserted now to verify that I will open my dv will close this and if I will open this Java tech key then database tables can you see here I have table called Product command and product query so let me take the result of that select star from can I zoom this product command we have added the two object right book and mobile now let me test the update one okay so just put I mean I just want to execute the update for any of the record let's say this one I need to pass the ID here right ID is 2 that's it remove the ID from here what I want to update I just want to update the price okay now send the request the record got updated go and verify in the DV the record is updated in the DV Azure okay so it means our common service is working as expected now let's try the query service so what I'll do I'll duplicate this then I'll just change the url to 9292 the method is get right we want to fetch all the products now if I'll send the request we are not getting anything if you see the output it's giving the empty result I will tell you why and how to resolve now let's verify in the DV select star from product query just execute it there is nothing in the DV that is what we are understanding about the cqrs right we segregate the responsibility for the command and for the query so this common service is using its own database similarly queries using its own database so whatever the data is there in the command service is not present in query service because both are pointing to different DV see Here If You observe the architecture in command service DB we inserted two record but in the query service we are trying to face the record from query microservice itself right that is not in the sync so what we need to do whatever the things you are inserting in the command service are updating in the command service that you need to send to the query service so that is the reason they both will be in the sync and what we understand how we can sync both the micro Services by using the Kafka so if you if you are creating any event or if you are updating any event immediately we need to pump publish that event to the Kafka so that this query microservice can consume that event and can update their DV okay that is the reason when we are trying to hit the query micro service we are not getting any result because no data present in the DV all the operation we have done in the command service and we are trying to face the record from query service so that is the reason it is not available so you need to make both the micro service in a sync so to think that we need to play with the kapka so first let's add the Kafka dependency in both the micro Services then from the common service will produce the message to the topic and from query service will consume that messages okay so what I will do let me stop this to microservices fine so first let me add the Kafka dependency so that is the reason if You observe my YouTube channel the last video I upload about the Kafka serialization and deserialization that concept will helpful now here okay so I am using the same example so I just need the dependency I need this dependency are the dependency in both query microservice and common micro service then configure Kafka related properties so I mean go to the demo go to the application.aml so I will copy this see nothing is there so the common service will act as a producer for us and the query service will act as a consumer for us so I just need to define the server where is my cup and running and which kind of object I am sending here Json okay and which kind of object My Consumer is consuming Json that's it so what I'll do I'll just copy this then we'll change the part to the producer and consumer copy this now just go to the command service okay we are there I'll create a file then simply I will paste the key and value here since this command service will be my producer app who is producing the messages I don't need these information okay so I can remove everything now go to the query microservice query microservice will act as a consumer who will consume the messages to up to date the own DV right so I'll do create a file new file then you can name it define the properties here so this will act as a consumer so I don't need this producer part remove it so here you can change the trusted packages to all com.javaticket.star or else in our case I will Define entity because this is the particular object I will get over the Kafka or I will consume this particular object so I have defined the entity but it is up to you if you want to allow for all just Define the star that's it fine now let's writing the actual Logic for communication or to make both the microservice in a sync go to the command service from this command service we want to produce the event and that event could be either create event or update event so what I'll do I will just create another DTU class let's say product event so here simply I will Define private tring event type which kind of event you are going to perform whether it is create event or update the event then next I will just Define the product object because that is the object I want to send to the consumer which is nothing the query micro service that's it okay so based on this event type query microservice can easily identify OK this is the create event I need to add this object to my DB or this is the update event so I need to modify the DV okay based on that he can take the action so that is the reason I have defined the event type so since we added the lumber we can annotate at the Red Data at the rate all argument Constructor no argument Constructor fine now I need to send this over the Kafka so go to the service class or you can write in the controller but better let's write in the service itself so just open the Service and here what you can do you can inject the Kafka template private template keys of string values of type object and simply just do the auto add that's it now using this Kafka template while persisting it to the DV in the command service immediately publish the event okay so what I will do I will just get the return type of it okay product Duo now what you can do very simple step Ka template dot send you need to create the topic name so same topic both your producer and consumer are nothing comment service and query service need to listen Okay so I will give the topic name product event topic and what data you want to send you want to send product event what we have created just now right so what I will do I'll change it to the product event now I will get the product from the product event dot get product save this product object to the DV and then give this entire product product even to the Kafka so here rather than give this object what you can do you need to set which kind of event and this particular object after store into the DV so very simple step you can create the object of product event and you can give the event type okay not this one event type is create product and what object you want to send this one which is already purchased to the DB in the comment service fine now this event object you need to send here now similarly you need to set the event for your update method okay so once you update into the DV just assign the variable just create the product event of type update update product and this is the object we want to send through the Kafka okay so simply just call the LL copy this okay then return the product Duo do the same change in your controller class because from the controller you are calling the service and we are sending the different parameter so just go to the controller class where service is crying is there any error okay here we have missed the return statement cool go to the controller class so just change the request to product event here also change to product event all good now again what is the issue okay here also you need to change it to the product event I mean earlier we are sending the product as a input but now we are sending the product event and that product is wrapped inside this product event right so what I can do I'll just Define product object new product equal to product event dot get product that is what the new value we want to update right so I will just simply change here all good no error so what we are doing from the command service immediately after taking the action on the command service we are sending that event with the type to the Kafka topic now its query service responsibility he always need to listen to this particular topic to get the updated value so what I will do I need this class inside the consumer because this is what the class we are just communicating with a producer and consumer so I'll copy this I'll go to my query service I will create a package dto paste the class fine now in the application.aml I need to specify The Trusted package com.javatick.dto because this is this is what the object I will get or I will consume it fine all good now the next part you need to write the query service so if You observe here from the comment service product command microservice we publish the event hey I am going to create the event hey I am updating the event everything will send to the Kafka now the query microservice need to consume that event and based on that you need to do the action so that is the last part is pending in our code go to the micro service then what you can do either you can write in the controller or you can create separate listener class but let's write it here itself so we'll write a method public so I don't want to return anything but process product events this need to be Capital that's it now which kind of object you will consume product event better let's move this piece of code to the service rather than writing in controller go to the service product query service so just add here now based on the product event you need to take the action so what you can do simply you can write if product event Dot get event type if it is dot equals create event so let me copy the name correctly from the command service I mean how we are sending the key create product right if the event type is create product then simply add the new product object to the DV in query service so very simple step Repository dot save get the product from this particular event product event dot get product okay very simple step we are just okay let me add a break we are just checking if the action or the event type is create product then get the product object and store it into the DV because if You observe our query service DB is empty we are not storing anything everything we are storing here now what we are doing once we stored in the comment service we are publishing that event to the this query service now query service we are just checking which type of event it is if it is a create product don't worry add that to my DB so that I will be sync with the comment service now similarly you can check here if you can also write the switch case which is comfortable for you I am just adding the small demo the event type if it is updated let me copy the key if it is your update product then again you need to get the old product and modify it so let me do that as well okay I mean same update logic what we have written in the comment service but let me do it Repository dot find my ID keep the ID so the ID will be product event better what I'll do I'll just get the object okay multiple times we don't need to call this now I can simply pass the product here fine now repository dot find by ID I need to give the ID based on what you want to filter so product Dot get ID you will get the existing product object from the DV so better do the get so once you get the existing product now modify the value with the new product object okay simple step let's copy it from the existing code command service we have already written the code right so I'll just copy this go to the query service or just change it to the new product okay better let's don't change that's it once you are done with that simply call repository dot save that modify object that's it okay now in this approach any event you are publishing from the command service will be consumed by this method and you will check whether you are creating a new product okay if you are creating a new product simply it will add in the DV if you are updating the product it will validate with the existing product and modify its value and will save it this is what the create and update right now how I can tell to the Kafka this is my consumer simple there is annotation you can use listener and you can Define here group ID and topic name from which topic you want to listen the topics would be same go to the common service line here we have defined the topic copy the topic name go to the query service specify here now you can specify the group ID as well every consumer need to be Associated to a specific group ID that is how something called consumer group concept right I have covered this topic in my Kafka playlist if anyone not aware about that they can check out my Kafka playlist where I explain from the basic level so group ID will specify let's say product event group that's it right now as per the architecture we are publishing the event from the command service and we are consuming that event and accordingly we are modifying in our DV now if I'll do the get fetch products I will get the response from this particular database because we are now syncing the value from common to query okay so this is pretty clear guys there is there is nothing to confuse the architecture itself clearly says what you need to do I mean if You observe this particular architecture carefully without seeing any code you can write it so that is what I believe but let's see whether it is working or not we'll verify it right away all good so first of all let me delete this to these two table first because already the record is there but it's fine with the same record we can also try I mean we'll try another record and we'll see whether it is there in the product query DV or not so for that to start with the Kafka first I need to start my zookeeper and Kafka server right it is there inside the subtitles new terminal I want another terminal first let me start the zookeeper then I will start my Kafka server okay all good these two are started now I need to start my application okay let me start the command service let me start the query service looks good so consumer is up and running you can see here this is the event group we have specified assigned to this particular topic of partition 0 since we are not create the topic manually spring boot will create the topic with the same name with the single partition okay and in the producer If You observe I mean command service this is also up and running let me clear the console so go to the postman and we'll hit the post will add something new let's say I want to add watch prices 23 000. okay now we are sending this particular record to the DV first we are storing in our common service then immediately we are publishing the event so we'll verify that meanwhile I will open the upset Explorer here we can verify whether the Kafka communication is going correct or not okay so yeah so topic is created will verify the data okay there is no data because we have not published anything just go to the postman send the request there is some error let's see in the comment service so this error says that entity must not be null okay okay got it let me clear this so the problem is that we recently changed the controller payload structure right earlier we are sending the product object now we are sending the product event so we need to structure our Json payload like this so I'll go to the postman let me copy this then the first argument is type so type is let's say create product then I need to send my product object beautify this yeah this is how we need to send the payload now let me send the request we'll see okay can you see here I don't know why the ID is coming 52 it's fine that that's we are not going to focus name description so we verifying the DB first will verify in the command service okay we have added the new record after ID 1 and 2 it immediately came 52. something wrong with the sequence table that's fine now what I can do I will add another object let's say something like that your phone JBL price is let's say 2500. so what event I am publishing create product okay send the request ID is 53 the record is inserted go to the DV and verified now 3 and 4 these are the two records we added just now now let's see in the Kafka upset whether the event is published or not can you see here I cannot zoom these but let me try to format it in the Json this is the event we published it is there in our topic okay I mean from the command we are able to publish this create product event this is okay now let's verify in the query service whether we are able to fetch these two product or not we have four product in our DB but we can able to only see the third and fourth product because while executing the first and second we have not sync our query service right so 3 and 4 should be display so let's hit the endpoint great watch and your phone I mean the ID is changed because that is how the new record we are inserting in the query table right so if You observe here we are able to see the record one and two because this will act as a new entry in the query table okay product query table now we are good here my query service is able to sync with command service now what next let's verify the update scenario go here I will update the same object okay this will not be available what I can do what we have recently updated or the what we have recently sent the messages let's modify that this one right I'll copy this first I will change the uh okay I didn't even remember the idea of it let's verify in the DV this one ID is 53 all good so I will give the ideas 53 and what action we are performing here we are updating the event update okay update product this is the event type and I just want to update the price that's it let's say I will make it eight thousand five hundred now when I hit this particular endpoint I will change it to the put first when I will hit this particular endpoint first this record will be update in my command service DB once it will update immediately it will publish the event to the query service now query service will consume that particular event and you will modify its DB let's verify that what is there in the query service 2500 okay now let's hit the command service and we'll see whether the value is getting updated in the query service or not if yes then both the micro service are in correctly sync let's send the request yeah it's updated in the command service let's verify here the event type is update product we have already published it go and verify in the DV directly it's not updating something going wrong let's let's verify it right away no looks good I mean it's successfully able to update in the command service it means the issue is there in the query service yeah so we are getting the exception what is that no such element exception no value present okay got it so if You observe what is the value you are giving ID to update 53 right but it is not available in the DV right because in the product query the ID is 1 and 2. so because of the sequence table issue the ID is not in sync okay so to verify that for the safer side I will delete first let me stop both the application I mean this is the simple mistake guys okay I mean it's not programmatical mistake it's because of the sequence issue so because if You observe in the code in the consumer we are trying to face the existing product from DV now here when I am giving the 53 because we are sending the 53 right when I'll give the 53 then you don't find anything in the DV because in DB the sequence start from 1 and 2 in the query micro service so that is what the problem so for that I have stopped both the services now let me delete all the table okay can't I delete all yeah yes now I just want to create the table from scratch and will quickly review the create event and update event so let's start the application command service also let me start the query service so it seems it started this is the query service let me clear this and go to the comment service clear this it created the table again let's verify no data because you have not added anything now both are empty okay so first let's start with the create event let's go to the Post and send the request check the body create product let me send the request it inserted in the command service then immediately it publish the event so we'll verify it right away this is the new event right create product and then if you will verify in the DV in the command it is there the ID is one and also it is there in the query can you see here now let's verify that by hitting the query endpoint query micro service endpoint are able to get the result now let me add another object watch is done what is the value here no not this one okay I will manually modify it okay let's say books KK publication ignored about this feeling triple 9 send the request the record is inserted to now if I'll do the get call this will hit the query microservice product query micro service right if I'll send the request I am able to see both the two record just insert it right if we'll verify in the DV with all IDs in sync and command Services also having to record right now let's try the update event now the ID is in sync so that we won't get any issue at this time I believe let's verify it right away so what I can do go to the put first let me copy this object I want to update this watch okay go to the put ID is one body update product and the product object is this one so what I can do simply let me change the price to 23 000 to 58 000 okay now let's send the request it is updated in command service now let's verify in the DV common service it updated to 58 000 now let's check in the query service it updated in the query Service as well right there is no exception in the query service can you see here there is no exception it triggered the update command now let's verify the Same by hitting the end point to get all the products from the query service itself let's send the request we are able to see the two record and the updated value is also there 58 000 and if You observe in the Kafka the last event this is what we have published update products and price we have updated these okay so if You observe again from the postman we are getting both the result now as per architecture my both the micro services are in sync and whatever the event I am doing in the comment service that my query microservice is able to consume and up to date with its DV okay so this is what exactly the cqrs design pattern now if you think practically let's say I'm I'm getting more traffic for the search operation or the read request coming more to my application now since these are the two different microservices I can easily scale this specific microservice which is product query microservice I mean let's say I want to run it in run it with 20 nodes or 20 parts then I can easily scale it up now if I'll mix up these two like the old architecture then if you need to run with the multiple nodes or parts then again you need to deploy all your changes including the read and write operation right now since we segregate read and write now both the microservices in sync if you want to scale it independently either read or write that happily you can do it okay and if you want to implement the security or transactional specific to the read and write that also you can do so guys the above demo what we have done this is just one way of implementing the cqrs you can do that in many more ways based on your use case so in this case we have created two different microservices by pointing to two different database but if you don't want to do that still you can Club everything in a single micro Services by defining different controller and service class okay also you can use the event sourcing to keep the read and write data in a sync between the command and query in the same class and also you can use the nosql database the way I have used the MySQL because it is installed on my machine so you can play with the different kind of database okay so if you find out the solution over the Google you will find out multiple approach but this is the recommended approach since we have segregate the query and command it will be easy for you to scale it but it is really difficult to manage it okay because the complexity is more the approach we have done so again it depends on depends on your use case how you want to design or how you want to cater those kind of use cases okay so do let me know in a comment section if you guys have any doubts that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 25,451
Rating: undefined out of 5
Keywords: cqrs microservices spring boot, Cqrs microservices example, Cqrs microservices tutorial, cqrs pattern, cqrs spring boot, cqrs full form, cqrs microservices, javatechie cqrs, javatechie, cqrs
Id: fzGZPf0FMao
Channel Id: undefined
Length: 59min 3sec (3543 seconds)
Published: Sat Aug 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.