I have an actor in Akka that will process messages to create certain entities. Some fields on these entities are computed based on the state of other entities in the database at the moment of creation.
I would like to avoid creating a race condition where the actor processing goes faster than the database is able to persist the entities. This may lead to inconsistent data, going like:
- Actor creates a
Foo
and sends it to other actors for further processing and saving - The actor is asked to create another
Foo
. Since the first one is not yet saved, the new one is created based on the old content of the DB, thereby creating a wrongFoo
.
Now, this possibility is quite remote, since the creation of the Foo
s will be triggered manually. But it is still conceivable that a double click may cause problems under high load. And who knows if tomorrow Foo
will be created automatically.
Hence, what I need is some way to tell the actor to wait, and resume its operations only after confirmation that the Foo
s have been saved.
Is there a way to put an actor in idle state, and tell it to resume its operations after a while?
Basically, I would like to use the mailbox as a message queue, and have control over the processing speed of the queue.
Foo
has been saved, but rather waiting to process other messages until then. I would to like to block my actor for a while, so that its mailbox acts as a queue while I am still processing the Foo. But it is possible that I have misunderstood your comment, and maybe this is exactly what I need. In case, could you please expand the comment into an answer? – Runin