Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking IO in Akka

Tags:

scala

akka

I'm doing some Akka lately and wonder: Can I do blocking I/O in Akka without getting into big trouble? Let us say we have an Actor which does some blocking I/O because it uses a legacy library or for any other reason: Couldn't I just use a special dispatcher for those Actors which a reasonably sized ThreadPool and do blocking I/O without blocking all other actors because they run with a different dispatcher?

What are the downsides of this? And what would be the optimal way to call a 3rd party HTTP-API from an actor?

like image 338
Malax Avatar asked Feb 28 '12 15:02

Malax


People also ask

What is dispatcher in Akka?

Dispatchers are responsible for scheduling all code that run inside the ActorSystem . Dispatchers are one of the most important parts of Akka.NET, as they control the throughput and time share for each of the actors, giving each one a fair share of resources. By default, all actors share a single Global Dispatcher.

Is Akka single threaded?

Defining an Actor In Akka, actors are guaranteed to be run in a single-threaded illusion, which means that the Akka framework takes care of threading issues while allowing us to focus on the behavior that needs to be implemented. Actors may only communicate with each other and the outside world by through messages.

What is execution context in Akka?

An Akka MessageDispatcher is what makes Akka Actors “tick”, it is the engine of the machine so to speak. All MessageDispatcher implementations are also an ExecutionContext , which means that they can be used to execute arbitrary code, for instance Futures.

Is Akka asynchronous?

Akka actors are asynchronous from the beginning. Reactive programs can be implemented in either way, synchronous and asynchronous.


1 Answers

Doing blocking IO is a bad idea in general, and in a reactive multithreaded environment in particular, so your first step is to try to avoid it alltogether, that means looking into using AsyncHttpClient or HttpAsyncClient.

If that does not work, you can at least mitigate the risks by giving the blocking actors their own threads. This will of course be costly and you still risk filling up their mailboxes, but such is the choice of using blocking IO.

You also might want to look at the IO Actor module for a more raw interface to network IO.

Hope any of this helps,

Cheers, √

like image 150
Viktor Klang Avatar answered Oct 02 '22 02:10

Viktor Klang