EdwinVW / pitstop

This repo contains a sample application based on a Garage Management System for Pitstop - a fictitious garage. The primary goal of this sample is to demonstrate several software-architecture concepts like: Microservices, CQRS, Event Sourcing, Domain Driven Design (DDD), Eventual Consistency.
Apache License 2.0
1.09k stars 477 forks source link

Consistency between Db Entity and Event #91

Closed AlexKulbako closed 3 years ago

AlexKulbako commented 3 years ago

I was wondering if there is a right approach to do this:

                  // insert customer
                    Customer customer = command.MapToCustomer();
                    _dbContext.Customers.Add(customer);
                    await _dbContext.SaveChangesAsync();

                    // send event
                    CustomerRegistered e = command.MapToCustomerRegistered();
                    await _messagePublisher.PublishMessageAsync(e.MessageType, e , "");

What happens if after you have called await _dbContext.SaveChangesAsync(); an error occurs when sending an event. Does it mean we will lose this event? Should we transactional outbox pattern here?

EdwinVW commented 3 years ago

Hi @AlexKulbako. Thanks for your interest in the Pitstop sample and your comment.

Yes, using the outbox pattern is a good way of making this more robust. I chose not to use this pattern here to keep it simple.

Additionally, take into consideration that when using an outbox pattern, the actual sending of the event that occurs in the background could still fail (even after some retries). But then this happens outside the context of the synchronous call (in this particular case). So the caller is unaware of this error. You need some other way of catching these errors (e.g. through monitoring).

CodeWithKashif commented 3 years ago

Thanks @EdwinVW for your answer, as earlier I was also thinking to make use if transactional outbox pattern here. Now your pragmatic approach really enlightened me!