1 of 15

Tutorial

Sebastian, Thorsten

Retreat July 2017

2 of 15

Akka brings actor programming to the JVM.

What’s actor programming, though?

3 of 15

Object-oriented programming

  • Objects encapsulate state and behavior
  • Objects communicate with each other

→ Due to separations of concerns applications become easier to build and maintain.

“Task-oriented” programming

  • Application split down into task graph
  • Tasks are scheduled and executed transparently

→ The decoupling of tasks from resources allows for asynchronous and parallel programming, thereby enhancing application efficiency and scalability.

Actor programming

  • Actors encapsulate state and behavior
  • Actors communicate with each other
  • Actor activities are scheduled and executed transparently

→ Actor programming combines the advantages of object- and task-oriented programming.

4 of 15

What’s Akka?

Actor: encapsulates state and behavior

Hierarchy: decompose tasks and delegate to child actors

Message: share tasks, results etc. among actors

5 of 15

What’s Akka?

Actor system: container/runtime for actors

Remoting: applications can span actor systems

Numerous extensions:

  • streaming (reactive streams with backpressure)
  • cluster (resilience, elasticity)
  • sharding (decouple actors from locations)
  • persistence (fault tolerance)
  • data (distributed key-value store)

6 of 15

A glance under the hood

Dispatcher

Threads

Remoting

Serializers

Event stream

Dispatcher

Remoting

Event stream

/

user

system

Controller

Worker

RemoteSystem

Worker

Unthreaded user-code

Managed mailbox

React to errors, new nodes, ...

Transparent multithreading and asynchronicity

Transparent communication with remote actor systems

System and remote actors reside here

Each actor has a path/URL

User actors reside here

Parents supervise children

7 of 15

Prime calculator: Basic structure

Master

PrimeWorker

PrimeWorker

PrimeWorker

Worker

Listener

Range

Primes

Primes

Range

Query

Reaper

8 of 15

Prime calculator: Distributed structure

Master

Worker

Listener

Range

Primes

Primes

Range

Query

Reaper

Reaper

Shepherd

Slave

Subscription

Subscription

Acknowledgement

Remote system

9 of 15

Let’s play! (Part I)

10 of 15

Prime calculator: Two-phase scaling

Worker

Master

Worker

Listener

Range

Primes

Primes

Range

Query

Reaper

Reaper

Shepherd

Slave

Subscription

Subscription

Acknowledgement

Remote system

Worker

Subworker

Subrange

Primes

11 of 15

Hands on: Create a local scheduler

  • Create a new class Subworker:
    • Extend AbstractLoggingActor
    • Create a static props() method
    • Create new messages for Workers and Subworkers
    • Implement createReceive()
  • Delegate work in the Worker actor
    • Create subworkers in preStart()
    • Split up incoming ranges into subranges and delegate to the Subworkers
    • Consider using an Router for scheduling
    • Handle replies from the Subworkers
  • See Runtime.getRuntime().availableProcessors()

12 of 15

Let’s play! (Part II)

13 of 15

Let’s play! (Part III)

14 of 15

Stumbling blocks

  • Blocking calls
    • Ask pattern
    • Long-running operations
  • System fails, but does not terminate
  • System deadlocks or livelocks
  • Error handling: Termination, dissociation, and friends
  • ActorSystem ambiguity
  • Messages too large

15 of 15

Further topics

  • Technical details on Akka
  • Experiences, best practices, patterns with actor programming
  • Relevant technologies employed by Akka or can be combined with it
    • Scala, Protocol Buffers, Kryo/Chill, Netty, SLF4j, Logback, Typesafe configuration