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

Поділитися
Вставка
  • Опубліковано 3 сер 2023
  • In this tutorial, we will Implement CQRS Design pattern using Spring Boot & Kafka
    CQRS stands for Command and Query Responsibility Segregation, a pattern that separates read and update operations for a data store. Implementing CQRS in your application can maximize its performance, scalability, and security.
    👉 What is CQRS
    👉 Why CQRS Problem & Solution
    👉 Use-case
    👉 Hands on coding implementation
    #JavaTechie #Microservice #SpringBoot #DesignPattern
    Spring boot microservice Premium course lunched with 70% off 🚀 🚀
    Hurry-up & Register today itself!
    COURSE LINK : javatechie5246.ongraphy.com/
    PROMO CODE : JAVATECHIE50
    GitHub:
    github.com/Java-Techie-jt/cqr...
    Blogs:
    / javatechie
    Facebook:
    / javatechie
    Join this channel to get access to perks:
    ua-cam.com/users/javatechiejoin
    guys if you like this video please do subscribe now and press the bell icon to not miss any update from Java Techie
    Disclaimer/Policy:
    --------------------------------
    Note : All uploaded content in this channel is mine and its not copied from any community ,
    you are free to use source code from above mentioned GitHub account
  • Наука та технологія

КОМЕНТАРІ • 142

  • @jhonatanmontess5788
    @jhonatanmontess5788 10 місяців тому +5

    You show something complex in an easy way and using simple but great diagram which is also key. Thanks a lot !

  • @smartumesh
    @smartumesh 6 місяців тому +3

    Clean and clear explanation. I have watched other youtube CQRS videos, none is upto this mark. I will recommend my team to watch this for CQRS design pattern. Thank you.

  • @biniluwyz4981
    @biniluwyz4981 10 місяців тому +1

    you are so great my friend, short and to the point! that is what makes me follow your channel and take you lessons.

    • @Javatechie
      @Javatechie  10 місяців тому

      Thanks buddy 😊. keep learning 👍

  • @sudharsansathiamoorthy1075
    @sudharsansathiamoorthy1075 10 місяців тому +1

    Bro, excellent explnation!! First time seeing a different version of CQRS!!

  • @BrijeshSingh-ex6ep
    @BrijeshSingh-ex6ep 10 місяців тому +1

    Such an awesome explanation!! Thank you so much 🙏

  • @Enchant3DAniTale
    @Enchant3DAniTale 10 місяців тому +3

    Thank you for giving me this great opportunity to learn new concepts from you, Sir...

  • @sameerkadgaye2048
    @sameerkadgaye2048 10 місяців тому

    Hi basant Thank u.
    One day just randomly searching on youtube about Generics In Java so expecting ur channel suggestions in search results. But I didn't find u were never covered that on ur channel.

  • @ChandlerBing11
    @ChandlerBing11 10 місяців тому +1

    Such an awesome tutorial!!!! Congrats

  • @deepand6558
    @deepand6558 8 місяців тому

    Excellent . Thank you for taking us with code . Appreciate it

  • @rishiraj2548
    @rishiraj2548 10 місяців тому +2

    Good day greetings!
    Thanks for the video on CQRS.

  • @gopisambasivarao5282
    @gopisambasivarao5282 10 місяців тому +1

    Appreciate your efforts Basant, God bless you….😊

  • @sarojgupta8181
    @sarojgupta8181 10 місяців тому +1

    wow!!this is what i really want.Thank You Basant :)

  • @abhishekgowlikar
    @abhishekgowlikar 5 місяців тому +1

    Hats off for your hardwork and explanation.

  • @viswanadh2450
    @viswanadh2450 5 місяців тому +1

    your explanation is entirely superb boss can you do more in microservices these videos are helping a lot actually great work

  • @eldiosdelcielo
    @eldiosdelcielo 8 місяців тому

    el mejor tutorial de kafka que jamas haya visto nunca.

  • @umamaheswarreddy9204
    @umamaheswarreddy9204 10 місяців тому +2

    Your teaching skills 👌

  • @Nur_0077
    @Nur_0077 10 місяців тому +1

    Excellent..just awesome

  • @MastanvaliShaik-yg1lh
    @MastanvaliShaik-yg1lh 7 місяців тому +2

    In one word Awesome 👍

  • @davidl6797
    @davidl6797 4 місяці тому +2

    Thanks so much :), clear explanation!

  • @vidhanchandra3997
    @vidhanchandra3997 10 місяців тому +1

    It was helpful.....plz upload more design patterns.

  • @milindkamle8287
    @milindkamle8287 10 місяців тому +1

    thank you very much , excellent explanation.

  • @vino7tech
    @vino7tech 10 місяців тому +1

    Tq Very much sir. Wonderful Videos are posting. Best Spring boot tutorial channel in youtube.

    • @Javatechie
      @Javatechie  10 місяців тому

      Thanks Vino 😃. Keep learning 👍

  • @anantsaxena2172
    @anantsaxena2172 Місяць тому +1

    Thanks a ton man, you have made my life easy. Hats off

  • @mahendranath2504
    @mahendranath2504 10 місяців тому +2

    Thank you so much 👋👍🙏🏻👌👏🤝🫡🫰🏻✌️🥳

  • @vijaynarayanan9353
    @vijaynarayanan9353 9 місяців тому +2

    Why service is crying? With that question, you made us laugh team. Interesting as always. Keep up the good work.

  • @fipabrate
    @fipabrate Місяць тому +2

    Awesome video! Just one thing. When creating a Product entity within the Product-Query-Microservice, there is no need to specify @GeneratedValue for the id field. This is because the id is always provided by the Product-Command-Microservice when consuming the product from the Kafka topic. This way you wouldn't have problem with id 53 in video.

    • @Javatechie
      @Javatechie  Місяць тому

      Yes agreed with you . Thank you for the correction

  • @user-pb4tb7hg2o
    @user-pb4tb7hg2o 10 місяців тому +4

    I love your tutorials!
    Can you consider making one tutorial on aggregating microservices' Swagger UI into the API Gateway Swagger UI? It would be awesome to manage all APIs from a centralized interface. Thanks!

  • @AleksandarT10
    @AleksandarT10 10 місяців тому +1

    Great video!

  • @sidhuipani700
    @sidhuipani700 2 місяці тому

    very clear and nice explanation, great job

  • @zaidsiddqui-pz7xl
    @zaidsiddqui-pz7xl 4 місяці тому +1

    Thankyou very much for explaining

  • @nirmesh44
    @nirmesh44 10 місяців тому +1

    Best explanation Ever

  • @petersteel7735
    @petersteel7735 Місяць тому

    Thank you so much! it helped a lot!!

  • @tariqueansari3043
    @tariqueansari3043 10 місяців тому +2

    Your videos are simply awesome.
    I am an experienced IT professional with more than 10yrs of experience. I always refer to your videos for quick references or brushing up a few concepts.
    Thanks a lot.

    • @Javatechie
      @Javatechie  10 місяців тому

      Thank you so much buddy 😊. Really so glad to have you as a Javatechie followers 🥰

  • @balag2368
    @balag2368 10 місяців тому +1

    Fantastic tutorial

  • @shubhampatidar6116
    @shubhampatidar6116 8 місяців тому +1

    Again great thanks 🎉

  • @ckudalkar
    @ckudalkar 2 місяці тому

    Fantastic!!! Thanks!!!

  • @ajadon62
    @ajadon62 Місяць тому

    Thanks for the detailed explanation of CQRS pattern. Excellent !!
    One Quick question that Query microservice is also writing the data eventually then what is advantages of aggregating Query and Command MS. I know that is asynchronous call but writing the data in the same DB from where it's being querying/fetched.

  • @sumitjain3526
    @sumitjain3526 Місяць тому

    You made it easy👍

  • @RishiRajxtrim
    @RishiRajxtrim 9 місяців тому +1

    Great thanks!

  • @rahulroyswe
    @rahulroyswe 5 місяців тому

    Great tutorial. Just a small doubt.Cant we use slave dbs for query service??

  • @sergiosevillavillegas6943
    @sergiosevillavillegas6943 10 місяців тому +4

    Thus it is very easy for the keys to be desynchronized in the consumption of the topic. Better would be to use UUIDs generated in the command and used in the consumer of the query. Thank you very much for your amazing videos.

    • @Javatechie
      @Javatechie  10 місяців тому +2

      Thanks buddy yes we should use random key 🗝️

    • @Nur_0077
      @Nur_0077 10 місяців тому +1

      @@Javatechie Can you please show how to do that in another small video?

    • @Javatechie
      @Javatechie  10 місяців тому +1

      Simple instead of only sending value in Kafka just send key and value both

    • @sergiosevillavillegas6943
      @sergiosevillavillegas6943 10 місяців тому +3

      To make it even better, I would write the UUID as the key of the kafka message to apply the correct order of changes for the same entity. It must be used as entity key in the DB query

    • @Nur_0077
      @Nur_0077 10 місяців тому +1

      @@Javatechie thanks

  • @huseyinaydin1002
    @huseyinaydin1002 10 місяців тому

    Bu aralar CQRS ve Mediatör'e ilgi büyük. Guut. 😀

  • @faizulislam5096
    @faizulislam5096 6 місяців тому +1

    Thank you for explanation! acn you please also explain how it will handle distributed transition?

    • @Javatechie
      @Javatechie  6 місяців тому

      Please checkout my Saga design pattern video

  • @sripriyanagaraju7019
    @sripriyanagaraju7019 7 місяців тому

    very good explanation

  • @amitpadgaonkar8830
    @amitpadgaonkar8830 3 місяці тому +2

    He is great with spring framework because his name is Basant

  • @AshishRohillax
    @AshishRohillax 10 місяців тому

    Hi Basant. Please tell how can we get yml auto complete in Intellij ? Which plugin is required.

  • @sumertheory
    @sumertheory 3 місяці тому +1

    good. clear.

  • @realworld_code
    @realworld_code 5 місяців тому +4

    This is sheer CQRS without Event sourcing - ES. in real world almost there is no CQRS pattern implemented without ES

  • @VikashKumar-zx5lg
    @VikashKumar-zx5lg 2 місяці тому +1

    nice tutorial

  • @user-kj6od4jh7c
    @user-kj6od4jh7c 3 місяці тому +1

    Even in CQRS also you are putting in the same amount of load on DB through kakfka events have to write data again to sync DBs then what is the use of it ? Correct if i am wring

  • @rickdevmajumder5729
    @rickdevmajumder5729 4 місяці тому +1

    I became a fan of you :)

  • @swarupdutta8683
    @swarupdutta8683 10 місяців тому +1

    Awesome explanation.
    Would you please help to understand handling failure scenario in this design pattern?

    • @Javatechie
      @Javatechie  10 місяців тому

      In failure we need to implement a retry mechanism i will cover that as part of Kafka series

  • @ramnatarajan8650
    @ramnatarajan8650 8 місяців тому +2

    Excellent tutorial, but the one thing I am greatly concerned is duplication of data. You need double the amount of db space which is a big concern for me. Is there a way to address this. Do you have any suggestion on this?

    • @Javatechie
      @Javatechie  8 місяців тому

      No such mechanism is available to avoid this usecase buddy.

  • @anandrai07
    @anandrai07 2 місяці тому +1

    When We should use CQRS or when Read replica? What are benefit of CQRS over read replica? Looking for answer as architecture point of view not coding.

  • @anands53
    @anands53 10 місяців тому +1

    This is really awesome. Thank you so much for putting all efforts in making these informative videos. I have one question. Deleting the tables might not be a great solution for different sequence ids issue. Is there any other way to fix that problem? Please let us know.

    • @Javatechie
      @Javatechie  10 місяців тому

      Yes we need to set strategy type as identity to avoid sequence issues

    • @anands53
      @anands53 10 місяців тому +1

      @@Javatechie thank you so much. we would be really grateful if you can provide that fix. I strongly believe that will help a lot of people out there. It is a real world practical problem.

    • @Javatechie
      @Javatechie  10 місяців тому

      @anands53 is very simple buddy just use below
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)

  • @jaya_surya
    @jaya_surya 9 місяців тому +1

    Thanks!

    • @Javatechie
      @Javatechie  9 місяців тому

      Thank you buddy 🙂

  • @rohitmanohar2733
    @rohitmanohar2733 9 місяців тому +1

    Sir it will be helpful if u can add one more step where we can use model classes at one module and use it in whole project , as it was one of the interview question asked

    • @Javatechie
      @Javatechie  9 місяців тому

      That is multi module project please check my saga pattern video you will get an idea 💡 how to do that

    • @rohitmanohar2733
      @rohitmanohar2733 9 місяців тому

      @@Javatechie Thank You loved ur video

  • @shashankdahake8985
    @shashankdahake8985 5 місяців тому +1

    Just another thanks

  • @smartumesh
    @smartumesh 6 місяців тому +1

    How to scale up read and make it more performant ? will the Kafka listeners can obstruct in the reading ?

    • @Javatechie
      @Javatechie  6 місяців тому

      Yes Kafka well capable to tackle this

  • @VLADICA94KG
    @VLADICA94KG 10 місяців тому +2

    Interestingly, you didn't implement mechanism for handling delete endpoint, since in that case you would need to have delete command in the product read service.
    How would you solve that?

    • @Javatechie
      @Javatechie  10 місяців тому +1

      That's not a problem at all in read also we do have save and update right? In a similar way you can add delete anyway it's just that we used to sync data and only we are allowing users to do fetch records not other http methods

  • @kainbizarro
    @kainbizarro 8 місяців тому

    Good video. I have a question: How I can avoid code dumplication using CQRS?

    • @kainbizarro
      @kainbizarro 8 місяців тому

      Can you do some video were you show a new video and teach how handle communication problems? Thanks in the advance

  • @itsnarahari
    @itsnarahari 10 місяців тому +1

    @Java Techie CQRS Pattern Is looks good but If we write Kafka In Each and every create/update methods It's looks like Coupled... Tomorrow If you want to update or Introduce any new Service It's hard to Update...So My suggestion Is to Sync the databases Independently I mean we can use any trigger points In database side Queues than the code side...and we can use any third party tool that will sync with external databases

    • @Javatechie
      @Javatechie  10 місяців тому

      Why will it impact buddy since Kafka will play the role asynchronously .

    • @itsnarahari
      @itsnarahari 10 місяців тому +1

      @@Javatechie Yeah async only but tomorrow If you want to use any streams we are tightly coupled with Kafka right we should use any generic one like spring cloud stream

    • @Javatechie
      @Javatechie  10 місяців тому +1

      Yes spring cloud stream we can use to make it loose coupling with application

  • @anandnerurkar8482
    @anandnerurkar8482 5 місяців тому +2

    whenwe implement rest endpoint , do we need to implement cqrs always since we are segragating read operation from write, or is it used in specific scenario only pls suggest

    • @Javatechie
      @Javatechie  4 місяці тому

      Not always but it's recommended design pattern for microservices

    • @anandnerurkar8482
      @anandnerurkar8482 13 днів тому

      @@Javatechie both contoller are maped to /products only, since we have 2 microservies, each should be pointing different request mapping like /productc /productq

  • @Gsuresh_3
    @Gsuresh_3 5 місяців тому

    How to open that kafka interface, partition, consumer related?

  • @rohitmanohar2733
    @rohitmanohar2733 9 місяців тому +1

    Hi sir Can u please create video on Distributed Transaction management pattern on spring microservice

    • @Javatechie
      @Javatechie  9 місяців тому +1

      Check this playlist it's already there
      Microservice design patterns: ua-cam.com/play/PLVz2XdJiJQxw1H3JVhclHc__WYDaiS1uL.html

    • @rohitmanohar2733
      @rohitmanohar2733 9 місяців тому

      @@Javatechie Thank you

  • @Bollywoodmaze
    @Bollywoodmaze 10 місяців тому +1

    Interview Question:
    Do we have a way to exclude parent pom inherited dependency ???

    • @Javatechie
      @Javatechie  10 місяців тому

      No not getting you exactly, what I understood in module project you want to execute parent pom ?

  • @anandnerurkar8482
    @anandnerurkar8482 Місяць тому

    can you pls cover event sourcing if possible

  • @Gsuresh_3
    @Gsuresh_3 5 місяців тому

    Here, you created two tables in same database right? Not each table in each database?

  • @samitamahajan5692
    @samitamahajan5692 3 місяці тому

    My question is here , why do we need CORS design pattern ? we can do same thing with replicas and use for reading purpose . Any specific problem where we can only use cors not replica ?

  • @fsdinterviewguide
    @fsdinterviewguide 10 місяців тому +1

    Any videos about session and cookie in your channel?

  • @bugra320
    @bugra320 3 місяці тому

    But, I believe there is something wrong with how is it done. Read DB is still getting write operations from a kafka topic, instead of REST calls.

  • @girishanker3796
    @girishanker3796 4 місяці тому

    when I create my own cqrs project with Soring boot 3.2.1 it is giving (Listener unable to read message error :- cannot convert from com.lang.String to com.myDto). Why is this happening? Is it any issue with the json de-serialiser? when i clonee your code it is working fine.(your code has version 3.1)

    • @Javatechie
      @Javatechie  4 місяці тому

      Let me upgrade and check

  • @amitpadgaonkar8830
    @amitpadgaonkar8830 3 місяці тому +1

    Why can't the problem of DB update be solved easily by having a view on the table?

    • @Javatechie
      @Javatechie  3 місяці тому

      Not getting you

    • @amitpadgaonkar8830
      @amitpadgaonkar8830 3 місяці тому

      @@Javatechie I think there was no necessity of creating another table for the query service. You could have just created a view on the 1st table. Please correct me if I am wrong

    • @Javatechie
      @Javatechie  3 місяці тому

      @@amitpadgaonkar8830 i am not aware about view will check and update you buddy

  • @shadliael-amri9470
    @shadliael-amri9470 3 місяці тому

    hello , i had this error as a loop
    The class 'com.example.productcommandservice.dto.ProductEvent' is not in the trusted packages
    any idea how to fix it please?

    • @Javatechie
      @Javatechie  3 місяці тому

      Check application.ptoperies file i have defined a key to avoid this error

    • @shadliael-amri9470
      @shadliael-amri9470 2 місяці тому

      @@Javatechie i did not find it ;-;

    • @Javatechie
      @Javatechie  2 місяці тому +1

      spring:
      kafka:
      consumer:
      properties:
      trusted.packages: com.example.myapp.domain
      producer:
      properties:
      trusted.packages: com.example.myapp.domain

  • @Vikramd555
    @Vikramd555 10 місяців тому

    Without this design pattern, for every product creation there is only one write operation.
    With this pattern there are two db writes to two different dbs with additional complexity of messaging.
    If the read service is making a save call then what is the benefit of additional complexity? 🤔

    • @Iliaspap80
      @Iliaspap80 10 місяців тому +5

      This is valid question as on this example the benefit of CQRS can't be shown. The core idea behind CQRS is that different models are optimized for reads and writes, which can lead to better performance and scalability. I can give you three main reasons:
      - Improved Read Performance: Since the read database is designed specifically for query optimization, it can provide faster responses to read requests, especially in scenarios where complex queries or aggregations are involved.
      - Flexibility: With separate read and write models, you can choose different technologies and data storage solutions for each. For example, you can use a NoSQL database for the read model and a traditional relational database for the write model, depending on the data access patterns and query requirements.
      - Event Sourcing: CQRS often goes hand in hand with event sourcing, where each change to the data is captured as an event. These events can be used for various purposes like auditing, maintaining a history of changes, or even for updating materialized views in the read database.
      It's important to note that while CQRS can provide significant benefits, it is not a one-size-fits-all solution. Implementing CQRS introduces additional complexity and requires careful design and consideration.

    • @rohitGupta-bv8qi
      @rohitGupta-bv8qi 10 місяців тому +1

      @@Iliaspap80 completely agree with you

  • @dfytq
    @dfytq 10 місяців тому

    @12:58 Isn't using ProductRepository directly instead of Interface tightly coupled the code?

    • @Javatechie
      @Javatechie  10 місяців тому +2

      Yes it’s an interface that we have created , where did you find tight coupling ?

    • @dfytq
      @dfytq 10 місяців тому +2

      @@Javatechie my bad. I have misunderstood. The tutorial is really very helpful. Thank u a lot for making such video.

  • @narendrareddyd2645
    @narendrareddyd2645 9 місяців тому +1

    But here how to make sure the DB data is in sync?

    • @Javatechie
      @Javatechie  9 місяців тому

      That's why we are publishing Kafka events right

    • @narendrareddyd2645
      @narendrareddyd2645 9 місяців тому +1

      @@Javatechie yes thanks bro really your videos are very much helpful

  • @beautifulso9216
    @beautifulso9216 10 місяців тому +1

    If id in PRODUCT_QUERY difference id PRODUCT_COMMAND

    • @Javatechie
      @Javatechie  10 місяців тому

      Then that's completely incorrect your both db is not sync correctly

  • @darogajee3286
    @darogajee3286 10 місяців тому +2

    CAn I use h2 databse

    • @Javatechie
      @Javatechie  10 місяців тому

      Ofcourse in your choice

    • @darogajee3286
      @darogajee3286 9 місяців тому +1

      @@Javatechie wont it be problematic while connecting because h2 cant be up until spring is running

    • @Javatechie
      @Javatechie  9 місяців тому

      Anyways you will test your api after starting the server right so h2 will automatically spin up that time no problem at all

    • @darogajee3286
      @darogajee3286 9 місяців тому

      @@Javatechie can I have 2 sepreate dB of h2 instance running. As h2 has default port number. Even if create different dB we will have same port number.

    • @Javatechie
      @Javatechie  9 місяців тому

      No since both applications running on different port 2 h2 instances will be created by spring boot. I would suggest you try once then ask

  • @fanclub69
    @fanclub69 6 місяців тому

    kafka data publish failed with ClassCastExeption. class com.product.command.dto.ProductEvent cannot be cast to class java.lang.String
    Did anyone face ?

  • @RealSlimShady-um6gf
    @RealSlimShady-um6gf 8 місяців тому

    Visited

  • @souravmukherjee1436
    @souravmukherjee1436 10 місяців тому +1

    Tried with hands-on, able to successfully produce the message to the topic but getting exception
    "Caused by: java.lang.IllegalArgumentException: The class 'com.sourav.command.dto.ProductEvent' is not in the trusted packages:"
    though I mentioned the right package in the yml file. Is there any other changes needs to do for this?
    application.yml
    spring:
    kafka:
    consumer:
    bootstrap-servers: localhost:9092
    groupId: product-event-group
    key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
    value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
    properties:
    spring:
    json:
    trusted:
    packages: com.sourav.query.dto

    • @Javatechie
      @Javatechie  10 місяців тому +1

      You might not follow the proper package structure please check and fix that

    • @souravmukherjee1436
      @souravmukherjee1436 10 місяців тому +1

      @@Javatechie Nice explanation buddy. Its now working fine.

    • @maisrasulov2643
      @maisrasulov2643 8 місяців тому

      Hi
      how you resolve it ? I have same problem

  • @sumitjain3526
    @sumitjain3526 Місяць тому

    You made it easy👍