Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain driven design and domain events

I'm new to DDD and I'm reading articles now to get more information. One of the articles focuses on domain events (DE). For example sending email is a domain event raised after some criteria is met while executing piece of code.

Code example shows one way of handling domain events and is followed by this paragraph

Please be aware that the above code will be run on the same thread within the same transaction as the regular domain work so you should avoid performing any blocking activities, like using SMTP or web services. Instead, prefer using one-way messaging to communicate to something else which does those blocking activities.

My questions are

  1. Is this a general problem in handling DE? Or it is just concern of the solution in mentioned article?
  2. If domain events are raised in transaction and the system will not handle them synchronously, how should they be handled?
  3. When I decide to serialize these events and let scheduler (or any other mechanism) execute them, what happens when transaction is rolled back? (in the article event is raised in code executed in transaction) who will cancel them (when they are not persisted to database)?

Thanks

like image 895
Karel Frajták Avatar asked Jun 08 '11 14:06

Karel Frajták


People also ask

What are domain events used for?

The essence of a Domain Event is that you use it to capture things that can trigger a change to the state of the application you are developing. These event objects are then processed to cause changes to the system, and stored to provide an Audit Log. Figure 1: Events funnel inputs into a single source.

What is meant by domain-driven design?

Domain-driven design (DDD) is a software development philosophy centered around the domain, or sphere of knowledge, of those that use it. The approach enables the development of software that is focused on the complex requirements of those that need it and doesn't waste effort on anything unneeded.

What is domain-driven design example?

An aggregate is a domain-driven design pattern. It's a cluster of domain objects (e.g. entity, value object), treated as one single unit. A car is a good example. It consists of wheels, lights and an engine.

Is CQRS domain-driven design?

In its core, DDD has nothing to do with neither CQRS nor ES, as it is just a design "framework", whereas CQRS and ES are implementation patterns. In other words, when you have DDD hat on, you shouldn't be thinking about CQRS nor ES at all, because you are designing software, not implementing it.


1 Answers

It's a general problem period never mind DDD

In general, in any system which is required to respond in a performant manner (e.g. a Web Server, any long running activities should be handled asynchronously to the triggering process.

This means queue.

Rolling back your transaction should remove item from the queue.

Of course, you now need additional mechanisms to handle the situation where the item on the queue fails to process - i.e the email isn't sent - you also need to allow for this in your triggering code - having a subsequent process RELY on the earlier process having already occurred is going to cause issues at some point.

In short, your queueing mechanism should itself be transactional and allow for retries and you need to think about the whole chain of events as a workflow.

like image 169
BonyT Avatar answered Sep 21 '22 15:09

BonyT