Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits from using akka dispatcher vs scala global

Couldn't find any info on this. I have a Akka/Spray based service and was always using ExecutionContext.global if i wanted to write some async code with std scala Future. Are there any benefits from using a dispatcher in akka based app rather than importing a global one? And also are there are any disadvantages from mixing (some parts are using akka's dispatcher and other parts scale's global)?

like image 742
4lex1v Avatar asked Apr 24 '14 19:04

4lex1v


People also ask

What is an Akka dispatcher?

This is an event-based dispatcher that binds a set of Actors to a thread pool. Characteristics of default dispatcher are: Every actor has its own mailbox. The dispatcher can be shared among any number of actors. It is designed to be used when you have a non-blocking (async) code in your actor.

Which dispatcher is used for testing purpose in Akka?

The thread pool executor dispatcher is implemented using by a java. util.

What are futures in Scala?

Futures are the standard mechanism for writing multithreaded code in Scala. Whenever we create a new Future operation, Scala spawns a new thread to run that Future's code, and after completion it executes any provided callbacks.


1 Answers

Don't use Akka default Dispatcher for execution of blocking calls in Future-s - in this case you can get all your Actors sitting on the default Dispatcher fully hanged in case of all threads of the default Dispatcher are blocked by blocking calls in such Future-s.

Usage of different Dispatcher-s for different Actor types (and especially for Future-s that do blocking calls) is known as Bulkhead Pattern.

ExecutionContext.global is a ForkJoinPool (by default) that use scala.concurrent.context.minThreads/maxThreads/numThreads settings for configure number of threads. So, IMHO, you can use it for Future-s execution instead of any other Akka Dispatcher with fork-join-executor.

But I would suggest to use a separate Akka Dispatcher for bulkheading just to have all Dispatchers configured in a single place (in Akka config).

like image 111
Sergiy Prydatchenko Avatar answered Sep 21 '22 11:09

Sergiy Prydatchenko