Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency issue when updating projection version #49

Closed
wants to merge 2 commits into from

Conversation

kagvfiau
Copy link

Concurrency issue when updating projection version.

The problem can occur because the existing projection version is fetched before the update transaction. In rare cases we have seen events being executed twice because of this.

The solution, as implemented in this PR, is to both fetch and update the projection version within the same transaction.

@jdav-dev
Copy link

jdav-dev commented Jun 2, 2023

I believe we hit this concurrency issue today. We had a duplicate list item projected to a JSONB field. Our only explanation is the same event being projected twice.

Comment on lines +69 to +92
|> Ecto.Multi.insert_all(
:insert_version,
ProjectionVersion,
[
%{
inserted_at: DateTime.to_naive(DateTime.utc_now()),
updated_at: DateTime.to_naive(DateTime.utc_now()),
projection_name: projection_name,
last_seen_event_number: 0
}
],
on_conflict: :nothing,
prefix: prefix
)
|> Ecto.Multi.update_all(
:update_version,
from(version in ProjectionVersion,
select: version,
where: version.projection_name == ^projection_name,
where: version.last_seen_event_number < ^event_number
),
[set: [last_seen_event_number: event_number]],
prefix: prefix
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two separate INSERT and UPDATE statements will need to be combined as a single UPSERT query otherwise there is still the possibilty of a concurrency issue due to executing two queries.

@slashdotdash
Copy link
Member

Implemented as an "UPSERT" SQL query in #52.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants