Developing distributed applications with Akka and Akka Cluster

32
Developing distributed applications with Akka and Akka Cluster

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

Page 2: Developing distributed applications with Akka and Akka Cluster

What is Akka?

Page 3: Developing distributed applications with Akka and Akka Cluster

• A Scala and Java framework for scalability, fault-tolerance, concurrency and remoting through actors.

• Inspired by Erlang OTP.• Developed by Typesafe: https://www.typesafe.com/.

Page 4: Developing distributed applications with Akka and Akka Cluster

Concurrency paradigms

Page 5: Developing distributed applications with Akka and Akka Cluster

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

Page 6: Developing distributed applications with Akka and Akka Cluster

STM

Page 7: Developing distributed applications with Akka and Akka Cluster

Dataflow Concurrency

Page 8: Developing distributed applications with Akka and Akka Cluster

Actors

Page 9: Developing distributed applications with Akka and Akka Cluster

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

Page 10: Developing distributed applications with Akka and Akka Cluster

• Implements Message-Passing Concurrency• Share nothing• Isolated lightweight processes• Communicates through messages• Asynchronous and non-blocking

Page 11: Developing distributed applications with Akka and Akka Cluster

Concurrency model

• No shared state – no synchronization• Each actor has a mailbox (message queue)• Non-blocking send• Blocking receive• Messages are immutable

Page 12: Developing distributed applications with Akka and Akka Cluster

userActor ! User(“John Doe”)

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

Page 13: Developing distributed applications with Akka and Akka Cluster

Dispatchers

sample-dispatcher { type = Dispatcher executor = "fork-join-executor" fork-join-executor { parallelism-min = 2 parallelism-factor = 2.0 parallelism-max = 10 } throughput = 100}

Page 14: Developing distributed applications with Akka and Akka Cluster

Supervision and hierarchy

Page 15: Developing distributed applications with Akka and Akka Cluster

Routers

• Local• Remote• Various routing algorithms (round robin, random, consistent hashing

etc)

Page 16: Developing distributed applications with Akka and Akka Cluster

Let’s build a web crawler!

Page 17: Developing distributed applications with Akka and Akka Cluster

1.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 18: Developing distributed applications with Akka and Akka Cluster
Page 19: Developing distributed applications with Akka and Akka Cluster
Page 20: Developing distributed applications with Akka and Akka Cluster

Demo time

Page 21: 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 22: Developing distributed applications with Akka and Akka Cluster

Akka Cluster

• Based on DynamoDB clustering• Completely decentralized, uses gossip protocol for membership and

failure detection• Cluster aware routers can be used for balancing tasks across the

cluster

Page 23: Developing distributed applications with Akka and Akka Cluster

Cluster aware routersactor { deployment { /crawlerService/crawlWorkers { router = consistent-hashing-group nr-of-instances = 100 routees.paths = ["/user/crawlWorker"] cluster { enabled = on allow-local-routees = on use-role = backend } } } provider = "akka.cluster.ClusterActorRefProvider" }

Page 24: Developing distributed applications with Akka and Akka Cluster
Page 25: Developing distributed applications with Akka and Akka Cluster

Demo time

Page 26: Developing distributed applications with Akka and Akka Cluster

Detecting cycles

• Need to detect link cycles to avoid needless downloads• Distributed shared state?• Akka CRDTs can help!

Page 27: 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 28: Developing distributed applications with Akka and Akka Cluster

CmRDTs

• Based on messages• Requires messages to be delivered and processed exactly once

(complex!)• No need to transfer the whole state

Page 29: Developing distributed applications with Akka and Akka Cluster

CvRDTs

• Based on the object’s state• Need merge function that must be commutative, associative, and

idempotent

Page 30: Developing distributed applications with Akka and Akka Cluster

Demo time

Page 31: Developing distributed applications with Akka and Akka Cluster

References

• Akka documentation: http://akka.io/ • Good presentation on CRDTs: https://vimeo.com/43903960• On DynamoDB clustering: http://

www.allthingsdistributed.com/files/amazon-dynamo-sosp2007.pdf

Page 32: Developing distributed applications with Akka and Akka Cluster

Questions?