3

Suppose I have 3 domain events, Event1 Event2 and Event3. When command arrives at my Service Layer, I can rise 2 commands at the same time - e.g. Event1 and Event2. One Class has event listener for both events, but it should basically wait until both events are received to start processing it. Otherwise I need to execute expensive calculation 2 times (1st when event1 is received and 2nd time when event2 is received). Is there some design pattern that can help me in this?

The only thing that comes to my mind is that I suppose I could have some buffer and then wait for TRANSACTION_COMPLETE event in domain to start processing all received events at once.

  • 1
    Could ypu not cache the result of the expensive calc? – Ewan Aug 23 '16 at 11:56
  • Can you be more specific about Event1 and Event2 ? What are they really? How come they are published at the same time? Oftentimes, this kind of problem can be solved by designing your events slightly differently. – guillaume31 Aug 23 '16 at 12:52
  • so I have function(event1, event2). In order to process it, i need both events. If event1 is received - i can run function with old event2 as input. But as soon as i receive event2, I need to start recalculation this time using new event1 and event2 as input. Basically I need some time to wait until I receive all the inputs needed in order to start the work. – Marko Kraljevic Aug 23 '16 at 14:22
  • Dear Marko, it is difficult to help you, because we know nothing about your functional and non-functional requirements. How fast do you have to respond? Would it be okay not to answer on Ev1 and only on Ev2? Etc. Could you please give more context? Else we'll be stabbing at the dark. – Grimaldi Sep 18 '17 at 5:36
0

Suppose I have 3 domain events, Event1 Event2 and Event3. When command arrives at my Service Layer, I can rise 2 commands at the same time - e.g. Event1 and Event2. One Class has event listener for both events, but it should basically wait until both events are received to start processing it. Otherwise I need to execute expensive calculation 2 times (1st when event1 is received and 2nd time when event2 is received).

Is there some design pattern that can help me in this?

You might be looking for a process manager. A process manager is essentially a state machine that transitions from one state to the next when it receives an event, where each state describes the outstanding work that we're waiting on.

Udi Dahan wrote up a simplified demonstration in Saga Persistence and Event-Driven Architectures. (Udi uses "saga" where most of the community now uses process manager.)

Depending on the data structures you are using to communicate events to your event handlers, you might do well to separate the reading of events from the actions that you take. For instance, the Disruptor library is specifically designed for the idea that you would process a batch of events in memory, and then act on the changes when you reach the end of the batch.

In your case, that would mean that you usually process Event1 and Event2 together (saving the extra work), but in those cases where Event2 is delayed, the event handler can still provide a provisional answer within your SLA.

  • 1
    I suppose problem with using process machine is that event2 can be received but it is not mandatory. If it is received and I already started calculation - that means that I have to abort it and re-run it with adding event2 as an input. Disruptor pattern seems to has also some kind of wait strategy - so potentially using this pattern I can also have a situation with running the algorithm without necessity. I was thinking about some in-process queue pattern where consumers are doing ACK/NACK and one listener can be set to wait until event bus is empty - then start processing? – Marko Kraljevic Aug 24 '16 at 7:54
  • Listening for event bus depletion can be a bit dangerous - listeners could repopulate event bus, blurring definition of EMPTY. – Basilevs May 21 '17 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.