Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Reactors/Reactions in Event-Sourcing?

I am new in CQRS and I read about projections and projectors also reactors but my resource does not cover it thoroughly. Can someone define reactors thoroughly in Event sourcing or give me a link or book to read? my main questions are:

  • what are reactors?
  • what is the structure of reactors?
  • how do we implement reactors?
like image 414
Alireza Rahmani khalili Avatar asked Oct 27 '17 07:10

Alireza Rahmani khalili


1 Answers

A projection is a function that receives a domain event and transforms it to an action on a list, tree, graph, whatever…

The idea here is that you want the semantic domain events to store them, and to be able to replay them, but that you need various interpretations of them to efficiently query them. Hence you need a projection that says that domain event X means INSERT or UPDATE or DELETE for a given view.

So, a projection is basically a mapping between domain events and CRUD operations. You might also say that a projection is one of many interpretations of a domain event.

Now, there may be actions requires in response to a domain event that are not related to updating a table. E.g., whenever you receive a userLoggedIn event, you may want to send an email to the user. It's like a simple if this then that rule. This is basically what you call a reaction. You just react to the domain event in some custom way.

The structure of a reactor (the component that reacts) is hence pretty similar to the one of a projector. The only difference is that a reactor does not update the read model, but does any arbitrary action you want to.

In other words: A projection is a special kind of a reaction, where it is always about updating the read model. Since this is so common, you have a dedicated pattern for this, but generally speaking: Whenever you do something in response to receiving an event, this is a reaction.

The question of how to implement reactors is hard to answer without having more details. In wolkenkit, a CQRS and event-sourcing framework for JavaScript and Node.js, reactors are nothing but flows. Flows can either be stateless flows or stateful flows, depending on whether they contain state themselves.

A simple example (which is of course specific to wolkenkit) may look like this:

'use strict';

const when = {
  'userManagement.user.loggedIn' (event, mark) {
    // ...
    mark.asDone();
  }
};

module.exports = { when };

(Disclaimer: I am one of the authors of wolkenkit, so please take the examples with a grain of salt.)

like image 105
Golo Roden Avatar answered Sep 19 '22 22:09

Golo Roden