Introduction to Akka

14
Introduction to Akka Actor Framework

Transcript of Introduction to Akka

Page 1: Introduction to Akka

Introduction to

AkkaActor Framework

Page 2: Introduction to Akka

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

Page 3: Introduction to Akka

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!

Page 4: Introduction to Akka

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

Page 5: Introduction to Akka

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

Page 6: Introduction to Akka

Log sources

The problem to crunch – log processing

File

Single line

JsonLogAggregatorLogReceiver Json

LogReceiverService

Page 7: Introduction to Akka

Log sources

Possible Architecture

File

Single line

JsonLogAggregatorLogReceiver

Single LineProcessor

Single Line

Multiline Processor

Multiple lines

Single Line

Json

Json

Page 8: Introduction to Akka

Log sources

Another possibility

File

Single line

JsonLogAggregatorLogReceiver

Single LineProcessor

Single Line

Multiline Processor

Multiple lines

Single Line

Json

Json

Json

Page 9: Introduction to Akka

Log sources

Another one – concepts changed

File

Single line

JsonLogAggregatorLogReceiver

Line Parser

Single Line

LogProcessor

Multiple lines

Single Line

Json

Json

Json

Page 10: Introduction to Akka

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)?

Page 11: Introduction to Akka

Old-school web application vs actor hierarchy

Controller

ServiceCServiceB

DAOServiceD

StatelessStatic

A

B

CD

X

E

StatefulDynamic

Page 12: Introduction to Akka

A

B

C

The internals

Thread

Thread

Thread

ExecutionContextDispatcher

Future Future

• Fork-Join Pool• Threadpool

Page 13: Introduction to Akka

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

Page 14: Introduction to Akka

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