[DOC] Add Akka.Analyzers AK1006 documentation #7205
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
uid: AK1006
title: Akka.Analyzers Rule AK1006 - "Should not call
Persist()
orPersistAsync()
inside a loop"AK1006 - Warning
Calling
Persist()
orPersistAsync()
inside a loop is an anti-pattern and is non-performant. Consider collecting all events in a collection and then callPersistAll()
orPersistAllAsync()
after the loop instead.Cause
Akka.NET persistence tries its best to batch consecutive calls to
Persist()
orPersistAsync()
methods, but there is no guarantee that all of them will be batched properly.For example, lets assume that for each command, you generate 10
Persist()
operations. This can potentially create 10 asynchronous database connection to complete:Resolution
If you know that a group of events need to be batched, it is a lot more performant to batch all
Persist()
operations into a singlePersistAll()
operation after the loopThe persist success callback will be called for each event that are successfully persisted, so you do not need to change your logic inside the callback.