5. Orchestration Use-Cases
• Blocking threads while waiting for
tasks
• Highly inefficient use of resources
• Prone to downstream failures and
connection stacking
Synchronous Systems
• Message-oriented
• Never blocking
• Optimal parallelism – with Akka
Asynchronous Systems
Common pattern coordinating workers & services
11. Akka Async, message passing middleware
Actors hold state but not threads
Low thread count, low context switch
Resilience & Supervision
12. Akka Actor Scheduling
12
Dispatcher
Low number of threads (1-2 per CPU)
Actor
Message Received
Run to completion
Multiple messages per dispatch
Actor Scheduled on Thread
Scheduling Actor ≠ Scheduling “task”
13. Asynchronous Orchestration Model
• Tasks represented by actors and/or services
• Tasks dependent on each others by results of previous task
• Each task can run independent of each other
• Tasks can run as early as the input to these tasks are available
• Tasks may or may not run parallel to other tasks, resource dependent
14. Before We Start
• Make sure you have the right dependency in your build.sbt
libraryDependencies += "org.squbs" %% "squbs-pattern" % "0.8.0"
15. Setting up the Orchestrator
class MyTaskOrchestrator(task1Actor: ActorRef, task2Actor: ActorRef,
task3Actor: ActorRef, task4Actor: ActorRef,
task5Actor: ActorRef)
extends Actor with Orchestrator {
expectOnce {
case input: Input => orchestrate(input, sender())
}
def orchestrate(input: Input, requester: ActorRef): Unit = {
// Goodies go here…
}
}
17. val task1F = doTask1(input)
val task2F = doTask2(input)
val task3F = (task1F, task2F) >> doTask3
val task4F = task2F >> doTask4
val task5F = (task3F, task4F) >> doTask5
for { result <- task5F } {
requester ! result
context.stop(self)
}
Describing the Orchestration in an Actor
task1
task2
task3
task4
task5
Input
Output
18. Orchestrator Characteristics
• Single-use actor, terminates after ONE orchestration
• Keeps orchestration state in Actor as OFuture
• OFuture: Same API as Future, no ExecutionContext
• Runs callbacks on actor message handling thread
• Similar to Java 8 CompletableFuture synchronous API
• Spares users and actors from potential concurrent close-overs
• Future implicitly converts to OFuture in Orchestrator Actor
• Resolves final result from one or more OFutures using for-
comprehensions and sends back to requester
19. Orchestration Functions
• (in1: T, in2: U, … inN: W) => OFuture[X]
• Mentioned as doTask1, doTask2, etc. in the example
• Usually calls other actors
• May use ask to ask another actor
• Or better performance – tell and receive value with expectOnce
26. Bootstrap & lifecycle control
for http, actors, and streams
Extension model for hooking into
operationalization
Data center aware clustering
Actor registry
Monitoring & console
Many more patterns
What’s more about it?