Saga Pattern For Microservice Architecture With Rebus And RabbitMQ

  Рет қаралды 27,320

Milan Jovanović

Milan Jovanović

Күн бұрын

☄️ Master the Modular Monolith Architecture: bit.ly/3SXlzSt
📌 Accelerate your Clean Architecture skills: bit.ly/3PupkOJ
🚀 Support me on Patreon to access the source code: / milanjovanovic
A well-known pattern for solving distributed transactions is the Saga pattern. The Saga is a sequence of local transactions, where each local transaction updates the Saga state and publishes a message triggering the next step in the Saga. In this video, I'll show you how to create an orchestrated Saga using the Rebus library with RabbitMQ for message transport.
Implementing The Saga Pattern With Rebus And RabbitMQ
www.milanjovan...
Join my weekly .NET newsletter:
www.milanjovan...
Read my Blog here:
www.milanjovan...
Subscribe for more:
/ @milanjovanovictech
Chapters
0:00 What is the Saga Pattern?
0:41 Installing Rebus and the required libraries
1:22 Adding the RabbitMQ Docker image
2:05 Configuring Rebus and RabbitMQ
4:18 Creating the Saga and ISagaData classes
6:10 Defining the Event messages
7:27 Configuring the Saga - IAmInitiatedBy, IHandleMessages
8:03 Implementing the CorrelateMessages method
8:39 Handling the first step in the Saga
9:46 Adding the handler for the SendOrderConfirmationEmail command
11:14 Handling the next step in the Saga and updating the Data
12:14 Adding the handler for the SendOrderPaymentRequest command
13:19 Completing the Saga with MarkAsComplete
14:19 Subscribing to messages from RabbitMQ
15:22 Publishing the first message to start the Saga
15:59 Examining the complete Saga flow

Пікірлер: 100
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@qasimislamch
@qasimislamch Жыл бұрын
Thanks for the reply ❤️
@josedesousa5789
@josedesousa5789 Жыл бұрын
Good video ! I can't wait to see how you will implement the compensating transactions ! Thanks Milan for your job !
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Working on that one 😁
@user-pc7oz4ww1k
@user-pc7oz4ww1k Жыл бұрын
simplest explanation of the saga pattern I ever heard and saw with very practical example, thank you
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks a lot, Iosif :)
@jameshancock
@jameshancock Жыл бұрын
This is great! Are you planning on doing an error handling video on this?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
That's the plan!
@mylesdavies9476
@mylesdavies9476 Жыл бұрын
I was curious about this also, I'm assuming the persisted saga can resume from a given point if a step fails and is then resolved?
@jameshancock
@jameshancock Жыл бұрын
@@mylesdavies9476 yes in the case where it was transient. The issue for me is how do you unwind a saga if a required step fails?
@GeoffSeeley
@GeoffSeeley Жыл бұрын
@@jameshancock MassTransit has a Routing Slip pattern with compensation for activities to allow a rollback if needed.
@radeksendecki9922
@radeksendecki9922 Жыл бұрын
You do wonderful work in practical way. Thank you.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you very much, Radek! Happy to contribute :)
@erikwiktorsson6841
@erikwiktorsson6841 Жыл бұрын
Great video Milan!, agree with other some error/rollback logic would be very nice to see. Keep up the good work!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
On it!
@nove1398
@nove1398 Жыл бұрын
Great video, very informative. Will be testing it out soon
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Have fun! 😁
@neopro688
@neopro688 Жыл бұрын
Very good content! will be nice if you make a video about massTransit
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
There already is one, although it's basic and doesn't cover Sagas
@mike19821028
@mike19821028 Жыл бұрын
Great video, thank you!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad it helped!
@hlazunov
@hlazunov Жыл бұрын
A nice and short tutorial. But how are you going to implement rollback flow in case of failure on one of the steps? It would be great to demonstrate this as well.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I'll tackle it in a future video
@MarioUlivelli
@MarioUlivelli Жыл бұрын
Great content, but you have shown happy case scenario. How to complete the implementation (with Rebus for example) if last part of saga failed? How to do "rollback" and configure it? How it works?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I'll tackle that in a future video, this one was already pretty long
@allen9504
@allen9504 6 ай бұрын
Masstransit also supports Sagas? What are pros and cons vs rebus?
@MilanJovanovicTech
@MilanJovanovicTech 6 ай бұрын
Will do a deep-dive into that
@adriancooke7310
@adriancooke7310 Жыл бұрын
Hi Milan, when you first save your order into the database and then publish your order created event. I have seen a library such as CAP nuget package provide you with an atomic transaction between the database and the bus to ensure that you get both operations succeeded rather than in some cases when the message bus is offline only getting the database activity occur. It is also known as the outbox pattern.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I'm aware 😁 I've talked about the Outbox pattern before, but didn't want to add too much complexity in a single video.
@milicabozic3827
@milicabozic3827 Жыл бұрын
Good video Milan!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks Milica :)
@alaaalnajjar1824
@alaaalnajjar1824 Жыл бұрын
Nice video, Milan! I wish the video was focused on Orchestrated SAGA in a comprehensive way, like executing different transactions in different services (maybe 3 different services), compensating steps if something failed in between, and maybe more realistic example; maybe creating order, then deduct qty in "Stock service", then bill it in an invoice (invoice service)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I've a video coming out about a failure case and compensations
@alaaalnajjar1824
@alaaalnajjar1824 Жыл бұрын
@@MilanJovanovicTech I will be waiting for it 👍 Long Question please bear with me: If user clicks on "Submit order" from UI and an http request sent to a Web API that starts a saga, it seems we will return a success to the user while the SAGA is still going in the backend; sending integration events for emails and other things, and other services still handling those events, maybe end up compensating for the whole Saga, what if the user doesn't want to see a success message unless we're 100% sure the saga completed successfully or error message if SAGA failed & fully compensated?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
@@alaaalnajjar1824 You show a simple message like "We're processing your order" and just orchestrate the saga accordingly
@alaaalnajjar1824
@alaaalnajjar1824 Жыл бұрын
@@MilanJovanovicTech This will work but for some functions I don't think it will accept "we're processing, you will know once it's done", maybe the user has to stay on UI screen and never leave it unless a definitive result is received from SAGA about the order he's adding, in such case...I think we will need some kind of notification pushed from the server? maybe last step in SAGA (either completing/compensating) pushes SAGA result to a notification/SignalR, consumed by frontend and act upon it by showing success message and close the screen, or stay on screen and show the error/returned message, Am I right ? Is there a better way ?
@cyasotha
@cyasotha 3 ай бұрын
Hi Milan, nice explanation. Just a question, based on clean architecture we should try to avoid dependencies in external libraries in the application layers - so how we can solve that?
@MilanJovanovicTech
@MilanJovanovicTech 3 ай бұрын
With an abstraction
@GeoffSeeley
@GeoffSeeley Жыл бұрын
Now let's see the same pattern using a MassTransit Saga.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Perhaps, but I'll need a different example
@dasdotnet1
@dasdotnet1 Жыл бұрын
This is Great
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks a lot!
@AlexanderSalguero
@AlexanderSalguero 10 ай бұрын
Hi Milan, excelent explanation. Just a question, according to the examples it's possible return a value (id) after SAGA process finished? For example, to newsletters endpoint?
@MilanJovanovicTech
@MilanJovanovicTech 10 ай бұрын
The Saga is asynchronous, so your endpoint can't wait for it to complete
@juri1music
@juri1music Жыл бұрын
Could you briefly describe what are the benefits of this approach compaired to direct calling of smth like IEmailSender in the mediator? As I get, it allows us to store staging state and somehow guarantee that if smth is wrong on the SMPT server side (for example), we're going to deliver the user an email anyway. Am I right?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Decoupling + it scales better in a microservice environment
@alaaalnajjar1824
@alaaalnajjar1824 Жыл бұрын
What is assigned to "ApplicationAssemblyReference" ?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
typeof(ApplicationAssembly).Assembly
@sunnypatel1045
@sunnypatel1045 Жыл бұрын
Can you do a video using the Saga pattern with MassTransit with mediator please.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Sure
@unhandledexception1948
@unhandledexception1948 Жыл бұрын
Hi Milan, any chance you can make a slower more background video is more beginner friendly on this important topic ? I guess giving background why use Rebus, what are the main abstractions in a Saga etc.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Yeah, good idea
@hellokaamds
@hellokaamds Жыл бұрын
Looks great, does it make sence to use this with CAP? Or the Rebus implementation already contains a CAP like functionality?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I didn't work with CAP, gotta check it out first
@JuanCamba81
@JuanCamba81 4 ай бұрын
Hi Milan !What have you got on the ApplicationAssemblyReference class ?
@MilanJovanovicTech
@MilanJovanovicTech 4 ай бұрын
Static class, with a static field
@yugo6243
@yugo6243 Жыл бұрын
It would be nice to see another important part of the Saga pattern: error handling by configuring retries and issuing a compensation transaction in a case of a fail
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
As luck would have it, I just released such a video 😁😁
@mahmoudalaskalany
@mahmoudalaskalany Жыл бұрын
This is a great topic , can we merge this with retry policy and dead queue in case one of consumers are down
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Of course, you can add those to make it more robust
@Ajmal_Yazdani
@Ajmal_Yazdani Жыл бұрын
Hello. Thanks for sharing it @Milan. Could you please tell me what theme you're using for VS?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
VS dark theme + ReSharper color highlighting
@tyomidi
@tyomidi Жыл бұрын
I may have missed a video, but do you prefer or recommend Rebus over Masstransit?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I don't prefer either one. I worked with both. From me experience, Rebus is a lot simpler to configure.
@user-xm7sh3vw8o
@user-xm7sh3vw8o 11 ай бұрын
Could not resolve Rebus.RabbitMq.RabbitMqTransport with decorator depth 0 - registrations: Rebus.Injection.Injectionist+Handler
@MilanJovanovicTech
@MilanJovanovicTech 11 ай бұрын
What did you do different?
@qasimislamch
@qasimislamch Жыл бұрын
Are you using CQRS pattern? I’m new to this pattern and rebus and i am curious how can we implement CQRS with rebus
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
CQRS is just about splitting your reads and writes logically in your code
@FarukLuki111
@FarukLuki111 Жыл бұрын
This is too much "orchestration of business logic" inside the code". I would use CAMUNDA to do all this stuff, and implement "classic handler" only. It is than up the modelled workflow in camunda to decide what to do, what to call, how to handle errors etc.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
It was supposed to be an orchestrated saga, duh
@eugene5096
@eugene5096 Жыл бұрын
Wow what we have here ..... interesting ))))))))
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Did it meet expectiations?
@eugene5096
@eugene5096 Жыл бұрын
@@MilanJovanovicTech yes man very nice, would be great to see some compensation action if something is failed but i saw same question on other comment so will wait for that. Also what disadvantages you feel sagas have, except of implementation complexity ?
@ryoman76
@ryoman76 Жыл бұрын
Great video! Why do you use Rebus to implement saga pattern and not use Masstransit?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I wanted to try out Rebus and show it to you guys :)
@ryoman76
@ryoman76 Жыл бұрын
@@MilanJovanovicTech Will you make a video for implementing saga with masstransit?
@omerbilalcan
@omerbilalcan Жыл бұрын
Brate, can you say me what is your “vs template”? Font-colors are very good. Thank you for this video. 😊
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
VS dark theme + ReSharper color highlighting
@omerbilalcan
@omerbilalcan Жыл бұрын
@@MilanJovanovicTech thank you so much brate. Pozzzic from Istanbul. 🍻😊
@mihaimyh
@mihaimyh Жыл бұрын
What will happen if the app goes down and the saga was not completed?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Since it's persisted, it can continue from it left off, assuming the messages are retried
@ramytawfik9168
@ramytawfik9168 Жыл бұрын
can you recommend a good source of functional programming Milan ?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Zoran Horvat for KZfaq, also any talk from Simon Painter, Scott Wlaschin
@ramytawfik9168
@ramytawfik9168 Жыл бұрын
@@MilanJovanovicTech thanks alot my friend ❤️
@AndreiDzimchuk
@AndreiDzimchuk Жыл бұрын
Should a request handler be aware of being a part of a saga in the first place?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
In which way?
@juancarlosbaezpozos9353
@juancarlosbaezpozos9353 10 күн бұрын
your work is valuable, and I think you have the right to ask for remuneration, but if you already monetize your videos, you still want more? what about the students who don't have the money to contribute on patreon and can't see the whole picture? In the end it turns out to be a video that they end up selling to you.
@MilanJovanovicTech
@MilanJovanovicTech 9 күн бұрын
This video made a whopping $59 in 486 days, quite a lot no doubt. 😅 Try here: github.com/m-jovanovic/newsletter-orchestrated-saga
@juancarlosbaezpozos9353
@juancarlosbaezpozos9353 9 күн бұрын
@@MilanJovanovicTech Thank you very much Milan and I am sorry for being so rude. I appreciate what you do
@skyhappy
@skyhappy Жыл бұрын
Your Patreon is expensive, $9 per month just for the source code! Love your content but damn, that's a lot. I am surprised you have 597 patrons, this youtube channel is not that big. I assume the supporters are mainly from your newsletter. Love your vids but just to get the source code that price is a steep monthly cost. I would like to support you though, I might pay for just a month and get the source code then unsubscribe.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Is that with taxes? Cause it should be $5,99. The channel may not be "big", yet. But I interact with many people, try to be helpful. By the way, how much is a cup of coffee where you're at?
@ToryBerra
@ToryBerra 25 күн бұрын
So this is why Elsa exists lol this is crazy verbose.
@MilanJovanovicTech
@MilanJovanovicTech 25 күн бұрын
Yes
@qingweige7127
@qingweige7127 7 ай бұрын
Great video!! Thank you for your video, it has benefited me a lot. But I have a question, how do I guarantee that the _bus.Send method will send 100% of the messages to the RabbitMq Server after Context.SaveChanges is executed? that code is at #15:57 of this video.
@MilanJovanovicTech
@MilanJovanovicTech 7 ай бұрын
Outbox pattern is as close as you can get
Cache Invalidation Doesn't Have To Be Hard
12:47
Milan Jovanović
Рет қаралды 13 М.
Microservices with Databases can be challenging...
20:52
Software Developer Diaries
Рет қаралды 23 М.
Look at two different videos 😁 @karina-kola
00:11
Andrey Grechka
Рет қаралды 11 МЛН
Turns out REST APIs weren't the answer (and that's OK!)
10:38
Dylan Beattie
Рет қаралды 138 М.
NestJs Microservices with RabbitMQ
23:21
Computerix
Рет қаралды 10 М.
Что сказать на собеседовании про паттерн Saga?
7:35
Андрей Суховицкий
Рет қаралды 7 М.
AMD slayed the dragon - AMD Ryzen 9 9900X & 9950X
8:39
ShortCircuit
Рет қаралды 307 М.
Saga Pattern | Distributed Transactions | Microservices
17:18
ByteMonk
Рет қаралды 2,4 М.
Build Highly Scalable Applications With RabbitMQ in  .NET
14:02
Ускоряем ваш TV🚀
0:44
ARTEM_CHIBA
Рет қаралды 784 М.
Что за "голый" Андройд? #pixel #android
0:40
Не шарю!
Рет қаралды 709 М.
Это iPhone 16
0:52
Wylsacom
Рет қаралды 3 МЛН