I would consider Akka Persistence or using Akka Streams with the
Flow.mapAsync function. Either one provides a way to handle your situation
without unlimited threads. Using raw Actors only it will be just a lot of
work and cleverness and wheel re-invention.

Brian Maso

On Thu, Jan 4, 2018 at 2:30 AM, Martin Major <ngsoftw...@gmail.com> wrote:

> Hello,
>
> I have application in Akka Typed where I have instance of StateActor for
> each id (around 1000 instances). Each StateActor accepts 2 messages:
> GetState() and SetState(state).
>
> SetState saves its state to db and if that was successful, saves a copy to
> local actor cache.
> GetState responds with state from local cache if it is already loaded or
> loads the state from database, stores it locally and responds to caller.
>
> My DB api offers asynchronous access:
>
> val stateFuture: Future[State] = db.load(id)
> val storeFuture: Future[Boolean] = db.save(id, state) // boolean whether
> store was successful
>
> My problem is that I need to ensure that messages within one actor will be
> processed in serial. Thus GetState() can answer only after Future in
> previous SetState() is completed.
>
> Easy solution is to use Await.result() to each Future and change
> asynchronous code to blocking. This has big disadvantage that it would use
> many threads (up to number of actors).
>
> Another solution is to stash messages that comes while I'm doing
> asynchronous calls. When asynchronous call ends I'll first process messages
> from my stash. Disadvantage of this solution is that I'd lose mailbox on
> actor restart.
>
> Last solution I came up with is to create chained list of Futures that
> will do all the work. So every incoming message is simply append to chain
> and when everything is ready it is processed. Disadvantage of this solution
> is that I'm escaping from actor world to Futures world. And in case of
> restart or termination of actor chain of Futures can still run.
>
> It would be great if I have opportunity to signal from actor whether I
> want to process next message or not but IMHO it is not possible.
>
> What is the recommended way how to achieve serial processing of messages
> when the code contains Futures?
>
> Thank you very much,
> Martin
>
> --
> >>>>>>>>>> Read the docs: http://akka.io/docs/
> >>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/
> current/additional/faq.html
> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to akka-user+unsubscr...@googlegroups.com.
> To post to this group, send email to akka-user@googlegroups.com.
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to