Introduction to Akka

Preview:

Citation preview

Introduction to

AkkaActor Framework

Akka – an actor framework

A

B

C

D

X

Actor system:• An object model composed

of actors• Communication by

asynchronous message passing

• Message delivery (in general) not guaranteed

• Java & Scala API

Microservices in one app

E

An actor

• Reacts to messages• Is a ‘living’ object• Has a:

o Nameo Locationo Mutable stateo Lifecycleo A mailboxo A parento Optionally, childreno Changeable behavior

A

Props

ActorRef

Internally synchronous!

Actor hierarchy

A

B

C

D

X

• Actors form a supervision tree

• A child’s life is governed by its parent

• Failure handling isindependent from cause

• Failure is local

• Failure will happen…

E

What does it all give?

Actors are independent processing cells It’s fast, non blocking It gives a horizontally scalable application architecture Composes well with regular objects

Log sources

The problem to crunch – log processing

File

Single line

JsonLogAggregatorLogReceiver Json

LogReceiverService

Log sources

Possible Architecture

File

Single line

JsonLogAggregatorLogReceiver

Single LineProcessor

Single Line

Multiline Processor

Multiple lines

Single Line

Json

Json

Log sources

Another possibility

File

Single line

JsonLogAggregatorLogReceiver

Single LineProcessor

Single Line

Multiline Processor

Multiple lines

Single Line

Json

Json

Json

Log sources

Another one – concepts changed

File

Single line

JsonLogAggregatorLogReceiver

Line Parser

Single Line

LogProcessor

Multiple lines

Single Line

Json

Json

Json

Actor lifecycle

ARestart

Start

Stop

PoisonPill

Exception

Supervised by actor’s parent

• Stop the child?• Restart the child?• Restart all children?• Escalate (throw to parent)?

Old-school web application vs actor hierarchy

Controller

ServiceCServiceB

DAOServiceD

StatelessStatic

A

B

CD

X

E

StatefulDynamic

A

B

C

The internals

Thread

Thread

Thread

ExecutionContextDispatcher

Future Future

• Fork-Join Pool• Threadpool

Best practices

• Think of the hierarchy up-front• Avoid singleton root actors• Use ActorRefs, avoid the slow ActorSelection

• Always be prepared for failure => use timeouts• Use context.become to separate behavior by states• Monitor, log, measure everything• Don’t optimize the performance (yet)• If you can, delegate it => ensure single responsibility

Readings• http://doc.akka.io/docs/akka/2.3.7/scala.html• http://letitcrash.com/• Akka Concurency• Akka in Depth• Akka in Action - MEAP• Functional and Reactive Domain Modeling – MEAP• Code samples via Activator

Recommended