Akka Fundamentals

Preview:

Citation preview

Akka Fundamentals

Agenda• Why Akka?

• Actor Model

• Actor API (Scala/Java)

• Fault Tolerance - Supervision

• Routing

Shared-State Concurrency

Users:- John Snow- Bran Stark

- Tyrion Lannister

Locks

• Error Prone• Hard to reason about• Hard to implement• Hard to debug• Ugly• Low level• …

"All problems in computer science can be solved by another level of indirection"

David Wheeler

The Actor Model

A selection of events in the history of Actors:

• Carl Hewitt et al, 1973: Actors invented for research on AI

• Gul Agha, 1986: Actor languages and communication patterns

• Ericsson, 1995: first commercial use in Erlang/OTP for telecom platform

• Phillip Haller, 2006: implemented in the Scala stdlib

• Jonas Boner, 2009: creation of Akka

Principles of Reactive Programming, Week 5 Part 1

An Actor

What’s the time?

13:30What’s the time?

What’s the time?13:30

Message Passing

An Actor:

• Encapsulates state

• Has a behaviour (logic)

• Communicates via async messages

The Actor API

State

Behaviour

Messages

State

Behaviour

Messages

Fault Tolerance

We’ve crossed thread boundaries

This async boundary is explicit and a first class citizen in Akka

• All Actor communication is message based

• Errors are messages

Akka guides you in building fault tolerant applications byembracing failure.

“Let it crash”

Akka’s fault tolerance policy:

• supervised Actors form a tree structure

• the supervisor needs to create its child

• failed Actor is terminated or restarted

• decision must be taken by one other Actor

Supervision

Boss

Worker A Worker B Worker C

Actors form hierarchies

Worker A Worker B Worker C

Boss

SupervisorStrategy

• OneForOneStrategy

• AllForOneStrategy

Decider

Boss

Worker A Worker B Worker C

Boss

Worker A Worker B Worker C

Boss

Worker A Worker B Worker C

Boss

Worker A Worker B Worker C

Boss

Worker A Worker B Worker C

Boss + Workers

System

Boss + Workers

System

Boss + Workers

System

Error Kernel Pattern

Routing

An abstraction over the delivery of messages from a Sender to Receiver(s)

Router

• Akka provides several builtin Routers

• You may of course define your own

Round Robin Router

D. Wyatt - Akka Concurrency

Broadcast Router

D. Wyatt - Akka Concurrency

Smallest Mailbox Router

D. Wyatt - Akka Concurrency

Two types of routers:

1. Pool

• Router creates routees

2. Group

• You create routees and pass them in

Pool

application.conf

By Configuration

MyActor.scala

Programatically

Group

By Configurationapplication.conf

MyActor.scala

Programatically