Event-sourcing: processing received data

How does a source event system use derived data? All the examples I read about source events demonstrate services that respond to fact events. For example, a popular example:

Bank Account System

Developments

  • Deposits
  • Withdrawn funds

Services

  • Balance service

They then show how the balance service can at any time receive the Ie balance from events. It makes sense; these events are facts. There is no doubt that they occurred - they are external to the system.

However, how do we deal with the data computed by the system?

eg.

Reinstalled Service:

Services that are responsible for monitoring the balance and performing certain actions when it drops below zero.

Is the event-based approach appropriate, how should we use (or not use) derived data? That is the balance. Perhaps one of the following?

1) Usage: [Withdrawals] + [Request balance service]

Listen to the Withdrawn event, and then ask for the Balance service for the current balance.

2) Usage: [Event with a changed balance]

Get the balance service to throw the “Changed balance” event containing the current balance. Presumably, this is not a “fact”, since it is not external to the system, therefore, it is prone to miscalculation.

3) Usage: [Event removed from the fund] + [Transactions with deposit of funds]

We could just skip the Balance service and each service maintain its own balance directly from the facts .... although this will lead to the fact that each service will have its own (potentially different) version of the balance.

+5
source share
2 answers

Acquisition of events is an evolving discipline with many diverse practices, practitioners, and charismatic people. You cannot expect them to provide you with a very consistent modeling method for all the scenarios you describe. Each of these scenarios has its pros and cons, and you indicated some of them. It can also be very different from one project to another, because the requirements for business (evolutionary pressure in the market) will be different.

If you are working on some critical system, and you want to constantly have a constant balance - it is better to use RDBMS and ACID transactions.

If you need maximum speed, and you are in good condition with stable conditions and don’t really worry about the accuracy of your balances (some events may be missing here and there for a number of reasons), you can derive your forecasts for balances from events asynchronously.

In both scenarios, you can use the event source, but you do not need to generate your forecasts asynchronously. It is normal to generate a projection in the same transaction area as the changes in your recording model, if you really need to do this.

Will it make Greg Young happy? I have no idea, but who cares about such things, if your balances one day can fail in a critical system ...

+2
source

Services that are responsible for monitoring the balance and performing certain actions when it drops below zero.

Summary: The way this is handled in source systems is not really all that different from the alternatives.

Speeding up the second - the advantage of having a domain model is to ensure that all proposed changes are consistent with business rules. CQRS borrowing: we send command messages to the command handler. The handler loads the state of the model and tries to apply the command. If the command is enabled, state changes to the domain model are updated and saved.

After saving the state of the model, the command handler can request this state to determine if their outstanding actions are. Udi Dahan details this in his conversation Reliable Messages .

Thus, the easiest way to describe your service is the one that updates the model each time the account balance changes and sets the flag “over the account” if the balance is negative. After saving the model, we plan any actions related to this state.

Part of the rationale for the source of events is that the state of the domain model is derived from history. That is, when we try to determine whether the model allows us to use the command, we load the history and calculate the current state from the history, and then use this state to determine whether the command is allowed.

In practice, this means that we can write the AccountOverdrawn event at the same time as we write the AccountDebited event.

This AccountDebited event can be subscribed to - Pub / Sub. Typical processing is that new events are published after they are successfully written to the record book. An event listener who subscribes to events leaving the domain model observes the event and schedules the execution of the command.

Digression: usually we need to perform at least a single execution of these actions. This means tracking confirmation.

Therefore, an event handler is also a stateful thing. There is no business condition in it, and, of course, there are no rules that would allow it to reject events. What he tracks, what events he saw, and what actions need to be planned. The rules for loading this event handler (often referred to as the process manager) are similar to the rules of the domain model - they load events from the record book to get the current state, and then see if any event is being processed.

Thus, it really subscribes to two events - the AccountDebited event and any event is returned from the operation to confirm that it has completed.

The same mechanic can be used to update the domain model in response to events from other sources.

Example: suppose we get the FundsWithdrawn event from an ATM, and we need to update the account history to match it. Thus, our event handler loads, updates itself, and assigns the RecordATMWithdrawal command to run. When the team loads, it loads the account, updates balances and records the events AccountCredited and AccountOverdrawn, as before. The event handler sees these events, loads the correct state of the process state based on metadata, and updates the state of the process.

In CQRS terms, all this happens in “recording models”; these processes are related to updating the record book.

The balance request itself is simple - we have already shown that the balance can be obtained from the history of the domain model, and exactly how your balance is expected to work.

Summarizing; at any time, you can download the history of the domain model, request its status, and you can download the history of the event processor to determine which work still needs to be confirmed.

0
source

Source: https://habr.com/ru/post/1240939/


All Articles