Developing distributed applications with Akka and Akka Cluster

40
Developing distributed applications with Akka and Akka Cluster Presented by K.Tsykulenko

Transcript of Developing distributed applications with Akka and Akka Cluster

Page 1: Developing distributed applications with Akka and Akka Cluster

Developing distributed applications

with Akka and Akka Cluster

Presented by K.Tsykulenko

Page 2: Developing distributed applications with Akka and Akka Cluster

Introduction

Page 3: Developing distributed applications with Akka and Akka Cluster

Agenda• What is Akka?• Concurrency paradigms overview.• Actors and actor model.• Live demo #1.• Akka remoting and clustering.• Live demo #2.• CRDTs and Akka Distributed Data.• Live demo #3.• Summary.• Q&A.

Page 4: Developing distributed applications with Akka and Akka Cluster

What is Akka?

http://akka.io

Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-

driven applications on the JVM.

Page 5: Developing distributed applications with Akka and Akka Cluster

What is Akka?

http://akka.io

Page 6: Developing distributed applications with Akka and Akka Cluster

Concurrency paradigms

Page 7: Developing distributed applications with Akka and Akka Cluster

Concurrency paradigms• Shared state and locks• Software Transactional Memory (STM)• Message-Passing Concurrency (Actors)• Dataflow Concurrency • and more…

Page 8: Developing distributed applications with Akka and Akka Cluster

Software transactional memory

V 1 V 2

V 1 V 12

V 11

V 3

V 2 V 22

successfulwrite

transaction

retriedwrite

transaction

V 1

Time

Page 9: Developing distributed applications with Akka and Akka Cluster

Dataflow Concurrency

X 1

X12=f(X1,X2)

X123=f(X12,X3)

X 2

X 3

Page 10: Developing distributed applications with Akka and Akka Cluster

Actors

Page 11: Developing distributed applications with Akka and Akka Cluster

Actors• Originate in a 1973 paper by Carl Hewitt• Implemented in Erlang• Encapsulate state and behavior• Closer to the definition of OO than classes

Page 12: Developing distributed applications with Akka and Akka Cluster

Actors

Sender ActorRef

Actor

Dispatcher

hasMessage

Message

Message

Mailbox

has

enqueue dequeue

schedule

send

Page 13: Developing distributed applications with Akka and Akka Cluster

Actorsuser ! User(“John Doe")

class UserActor extends Actor { def receive = { case User(name) => sender ! s"Hi $name" }}

Page 14: Developing distributed applications with Akka and Akka Cluster

Actorsval greeting = user ? User(“John Doe")

class UserActor extends Actor { def receive = { case User(name) => sender ! s"Hi $name" }}

Page 15: Developing distributed applications with Akka and Akka Cluster

Supervision and hierarchy

worker 1

worker 2

worker 3

worker 4

supervisor 1

supervisor 2

user

Page 16: Developing distributed applications with Akka and Akka Cluster

Building a web crawler1. Fetch a page2. Parse the page to get links3. Check if max crawl depth has been reached

and if yes, finish4. Go to 1 for all parsed links

Page 17: Developing distributed applications with Akka and Akka Cluster

Building a web crawler

Parser

CrawlMaster

user

Fetcher

pass urls

pass page content

pass parsed urls

Page 18: Developing distributed applications with Akka and Akka Cluster

Building a web crawler

Routerpass urls

CrawlMaster

initial url

UrlHandler

create UrlHandler

Fetcher Fetcher

Router

Parser Parser

Page 19: Developing distributed applications with Akka and Akka Cluster

Building a web crawlerclass FetcherActor(val parser: ActorRef) extends Actor with ActorLogging { import context.dispatcher val pipeline: HttpRequest => Future[HttpResponse] = sendReceive

override def receive: Receive = { case Url(link, depth) => pipeline(Get(link)).map(…).pipeTo(parser) }}

Page 20: Developing distributed applications with Akka and Akka Cluster

Building a web crawlerclass ParserActor extends Actor with ActorLogging with HtmlParser { override def receive: Receive = { case UrlContents(Url(link, depth), resp, requester) => val links = parseHtml(resp) .map(l => if (l.matches("^[\\/#].*")) link + l else l) .filter(l => Try(new Url(l)).isSuccess links.foreach(requester ! Url(_, depth + 1)) }}

Page 21: Developing distributed applications with Akka and Akka Cluster

Building a web crawlerakka { actor.deployment { /parsers { router = round-robin-pool nr-of-instances = 5 } /fetchers { router = round-robin-pool nr-of-instances = 5 } }}

Page 22: Developing distributed applications with Akka and Akka Cluster

Live demo #1

Page 23: Developing distributed applications with Akka and Akka Cluster

Going remote• Everything works using asynchronous

message passing which is good for remoting• Akka-remoting allows working with remote

actors just as if they were in the same JVM• Still need to handle additional issues like

serialization and handling potential networking problems

Page 24: Developing distributed applications with Akka and Akka Cluster

Akka cluster – pool routing

akka.tcp://localhost:2551/user/crawler

Routees

akka.tcp://localhost:2552/user/crawler

Cluster PoolRouter

localhost:2550

5. Gossip: localhost:2551

is Up

2. Gossip: localhost:2552

is Up

6. Deployroutee

3. Deployroutee

localhost:2551

localhost:2552

1. Joins cluster

4. Joins cluster

/remote/…/(routee)

/remote/…/(routee)

Page 25: Developing distributed applications with Akka and Akka Cluster

1. Joins cluster

Akka cluster – group routing

akka.tcp://localhost:2551/user/crawler

Routees

akka.tcp://localhost:2552/user/crawler

Cluster GroupRouter

localhost:2550

5. Gossip: localhost:2551

is Up

2. Gossip: localhost:2552

is Up

6. Routesmessages

3. Routesmessages

/user/crawler

localhost:2551

4. Joins cluster

/user/crawler

localhost:2552

Page 26: Developing distributed applications with Akka and Akka Cluster

Simple crawler clusterClient VM

CrawlClient

Router

Worker VM

CrawlMaster(s)

Worker VM

CrawlMaster(s)

Page 27: Developing distributed applications with Akka and Akka Cluster

Going remotecluster { seed-nodes = [ "akka.tcp://[email protected]:2551", "akka.tcp://[email protected]:2552"] auto-down-unreachable-after = 10s role { client.min-nr-of-members = 1 backend.min-nr-of-members = 2 }}

Page 28: Developing distributed applications with Akka and Akka Cluster

Going remoteactor { deployment { /workerRouter { router = consistent-hashing-group nr-of-instances = 100 routees.paths = ["/user/master"] cluster { enabled = on allow-local-routees = on use-role = backend } } } provider = "akka.cluster.ClusterActorRefProvider“}

Page 29: Developing distributed applications with Akka and Akka Cluster

Live demo #2

Page 30: Developing distributed applications with Akka and Akka Cluster

Distributed state

Page 31: Developing distributed applications with Akka and Akka Cluster

Distributed state

Page 32: Developing distributed applications with Akka and Akka Cluster

But what if we need to?

Page 33: Developing distributed applications with Akka and Akka Cluster

CRDTs• Good performance and scalability, the cost is

eventual consistency• Two main classes: operation based and state

based

Page 34: Developing distributed applications with Akka and Akka Cluster

CmRDTs

Page 35: Developing distributed applications with Akka and Akka Cluster

CvRDTs

Page 36: Developing distributed applications with Akka and Akka Cluster

Live demo #3

Page 37: Developing distributed applications with Akka and Akka Cluster

Summary• Actor model provides a concurrency

paradigm that is easier to reason about than traditional Java concurrency

• Designing actor systems is a lot like OO• You can easily make your actor systems

distributed and have referential transparency… to an extent

• Akka has many useful modules, like Akka distributed data, which allows to manage distributed state

• Try to build your own application and see how it works for you

Page 39: Developing distributed applications with Akka and Akka Cluster

Q&A

Page 40: Developing distributed applications with Akka and Akka Cluster

Thank you!Konstantin Tsykulenko

@Tsykulenko_K