Event Sourcing: Creating Conflicting Events Simultaneously

I am trying to implement an Event Sourcing system using Kafka and have encountered the following problem. During the registration of a new user, I want to check whether the user name already entered by the user is fulfilled. However, consider the case when 2 users try to register at the same time with the same username.

In my understanding of how the ES controller works, which processes the registration request, checks whether the request is valid, it will send a new event (for example NewUser) to Kafka, and finally, this event will be selected which will be saved in a materialized form (for example , Postgres DB). The problem is that the request is validated against the materialized view, but the actual persistence to it occurs later. Therefore, since two requests are processed in parallel (by different instances of the service), they can pass the test, as a result of which messages 2 appear NewUser. However, when the second controller tries to store these 2 NewUsermessages in the database, the storage of the second event will fail due to violation of the uniqueness restriction for the user name.

Any ideas on how to solve this problem?

Thank.

UPDATE:

In particular, I would like to check if the following approaches to the problem are accepted:

  • use username as userId (restrictive)
  • send the event to the section, divided by user name and during verification The event is sent to another topic
+4
source share
2 answers

Initial validation against a materialized view will be insufficient in most scenarios where you have limitations. There can always be some relevant events not yet implemented. There are two main approaches to concurrency control to ensure that the correct results are created:

1. : , (, ). , . , :

  • , , .
  • .

2. : . . , , . , . .

. : "" "". -, , , , . . , .

+1

, , .

.

, ES , , , , (, NewUser) Kafka, , , , (, Postgres DB).

. ( .)

, ; , , -.

- concurrency; , , , "replace (originalState, newState)".

, ,

replace(red,green)
replace(red,blue)

.

[...,replace(red,blue)...,replace(red,green)]

, replace(red,blue), , , , . , replace(red,green), , , .

, , ; , ..., . , , " ", , - .

, ?

, , .

, , , . , - " ".

, , , ( , )....

- ( , ), , , , , , , .

(: - , , , , ...)

+1

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


All Articles