Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors. Jonas Bonér Viktor Klang

Similar documents
Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors. Jonas Bonér Scalable Solutions

Above the Clouds: Introducing Akka. Jonas Bonér Scalable Solutions

[ «*< > t] open source t. Akka Essentials. Akka's actor-based, distributed, concurrent, and scalable Java applications

Akka. Developing SEDA Based Applications

The Actor Model. Towards Better Concurrency. By: Dror Bereznitsky

Concurrency. Unleash your processor(s) Václav Pech

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Building loosely coupled and scalable systems using Event-Driven Architecture. Jonas Bonér Patrik Nordwall Andreas Källberg

Message Passing. Advanced Operating Systems Tutorial 7

Reactive App using Actor model & Apache Spark. Rahul Kumar Software

Tackling Concurrency With STM. Mark Volkmann 10/22/09

Tackling Concurrency With STM

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

Favoring Isolated Mutability The Actor Model of Concurrency. CSCI 5828: Foundations of Software Engineering Lecture 24 04/11/2012

Evolution of an Apache Spark Architecture for Processing Game Data

Building Reactive Applications with Akka

Inside Broker How Broker Leverages the C++ Actor Framework (CAF)

Identity, State and Values

Scala Actors. Scalable Multithreading on the JVM. Philipp Haller. Ph.D. candidate Programming Methods Lab EPFL, Lausanne, Switzerland

Persistent Data Structures and Managed References

Erlang. Joe Armstrong

CMPT 435/835 Tutorial 1 Actors Model & Akka. Ahmed Abdel Moamen PhD Candidate Agents Lab

Microservices. Webservices with Scala (II) Microservices

Actors in the Small. Making Actors more Useful. Bill La

Reference types in Clojure. April 2, 2014

Using the SDACK Architecture to Build a Big Data Product. Yu-hsin Yeh (Evans Ye) Apache Big Data NA 2016 Vancouver

Akka Java Documentation

Covers Scala 2.10 IN ACTION. Nilanjan Raychaudhuri. FOREWORD BY Chad Fowler MANNING

Clojure Lisp for the Real #clojure

Akka Scala Documentation

An overview of (a)sync & (non-) blocking

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Reminder from last time

Combining Concurrency Abstractions

IGL S AND MOUNT NS. Taking AKKA to Production

Clojure Lisp for the Real clojure.com

Clojure Concurrency Constructs. CSCI 5828: Foundations of Software Engineering Lecture 12 10/02/2014

FRANCESCO CESARINI. presents ERLANG/OTP. Francesco Cesarini Erlang

9.1 Know when to use Actors

Advances in Programming Languages

SDC 2015 Santa Clara

Reminder from last time

CS Programming Languages: Scala

A Survey of Concurrency Constructs. Ted Leung Sun

Introduction Basics Concurrency Conclusion. Clojure. Marcel Klinzing. December 13, M. Klinzing Clojure 1/18

Transitioning from C# to Scala Using Apache Thrift. Twitter Finagle

Philipp Wille. Beyond Scala s Standard Library

Problems with Concurrency. February 19, 2014

Cassandra, MongoDB, and HBase. Cassandra, MongoDB, and HBase. I have chosen these three due to their recent

Exceptional Actors. Implementing Exception Handling for Encore. Sahand Shamal Taher

Effective v2.0. Jamie Allen Sr. Director of Global Services

Akka Java Documentation

IV. Process Synchronisation

NoSQL Databases Analysis

AUTOBEST: A United AUTOSAR-OS And ARINC 653 Kernel. Alexander Züpke, Marc Bommert, Daniel Lohmann

Distributed System. Gang Wu. Spring,2018

Lecture 10: Avoiding Locks

Scheduling Transactions in Replicated Distributed Transactional Memory

Scala Java Sparkle Monday, April 16, 2012

ATOMIC COMMITMENT Or: How to Implement Distributed Transactions in Sharded Databases

TWO-PHASE COMMIT ATTRIBUTION 5/11/2018. George Porter May 9 and 11, 2018

Erlang. Joe Armstrong.

02 - Distributed Systems

Causal Consistency and Two-Phase Commit

Overview. Distributed Computing with Oz/Mozart (VRH 11) Mozart research at a glance. Basic principles. Language design.

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Experience with Processes and Monitors in Mesa p. 1/1

Time and Space. Indirect communication. Time and space uncoupling. indirect communication

CONCURRENCY AND THE ACTOR MODEL

Distributed Data Analytics Akka Actor Programming

Mohamed M. Saad & Binoy Ravindran

DISTRIBUTED SYSTEMS [COMP9243] Lecture 5: Synchronisation and Coordination (Part 2) TRANSACTION EXAMPLES TRANSACTIONS.

DISTRIBUTED SYSTEMS [COMP9243] Lecture 5: Synchronisation and Coordination (Part 2) TRANSACTION EXAMPLES TRANSACTIONS.

A transaction is a sequence of one or more processing steps. It refers to database objects such as tables, views, joins and so forth.

Scaling Up & Out. Haidar Osman

Event-sourced architectures with Luminis Technologies

Fault Tolerance. o Basic Concepts o Process Resilience o Reliable Client-Server Communication o Reliable Group Communication. o Distributed Commit

Locking: A necessary evil? Lecture 10: Avoiding Locks. Priority Inversion. Deadlock

Chapter 2 Distributed Information Systems Architecture

Process Description and Control. Chapter 3

Simon Peyton Jones (Microsoft Research) Tokyo Haskell Users Group April 2010

Clojure. A (not-so-pure) functional approach to concurrency. Paolo Baldan Linguaggi per il Global Computing AA 2016/2017

Concurrent & Distributed Systems Supervision Exercises

Chapter Outline. Chapter 2 Distributed Information Systems Architecture. Distributed transactions (quick refresh) Layers of an information system

Shared state model. April 3, / 29

THEATR An actor based language

Goals Pragmukko features:

02 - Distributed Systems

Akka. Reactive Applications made easy

Multithreading and Interactive Programs

Stuart

COMMUNICATION IN DISTRIBUTED SYSTEMS

DISTRIBUTED COMPUTER SYSTEMS

Sistemi in Tempo Reale

TomcatCon London 2017 Clustering Mark Thomas

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

Commit-On-Sharing High Level Design

Scalable Performance for Scala Message-Passing Concurrency

sinfonia: a new paradigm for building scalable distributed systems

Transcription:

Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors Jonas Bonér Viktor Klang

We believe that... Writing correct concurrent applications is too hard Scaling out applications is too hard Writing highly fault-tolerant applications is too hard

It doesn t have to be like this We need to raise the abstraction level

Locks & Threads are...

...sometimes plain evil

...but always the wrong default

Introducing STM Actors Agents Dataflow Distributed Open Source RESTful Secure Persistent

Actors one tool in the toolbox

Actor Model of Concurrency Implements Message-Passing Concurrency Share NOTHING Isolated lightweight processes Communicates through messages Asynchronous and non-blocking Each actor has a mailbox (message queue)

Actor Model of Concurrency Easier to reason about Raised abstraction level Easier to avoid Race conditions Deadlocks Starvation Live locks

Akka Actors

Two different models Thread-based Event-based Very lightweight (600 bytes per actor) Can easily create millions on a single workstation (6.5 million on 4 G RAM) Does not consume a thread

Microbenchmark Chameneos benchmark http://shootout.alioth.debian.org/ Akka 0.8.1 3815 Scala Actors 2.8.0.Beta1 (react) 16086 +4 times faster Run it yourself (3 different benchmarks): http://github.com/jboner/akka-bench

Actors case object Tick class Counter extends Actor { private var counter = 0 def receive = { case Tick => counter += 1 println(counter) } }

Actors anonymous val worker = actor { case Work(fn) => fn() }

Send:! // fire forget counter! Tick

Send:!! // uses Future with default timeout val result: Option[..] = actor!! Message

Send:!!! // returns a future val future = actor!!! Message future.await val result = future.get... Futures.awaitOne(List(fut1, fut2,...)) Futures.awaitAll(List(fut1, fut2,...))

Reply class SomeActor extends Actor { def receive = { case User(name) => // use reply reply( Hi + name) } }

Akka Dispatchers

Dispatchers class Dispatchers { def newthreadbaseddispatcher(actor: Actor) def newexecutorbasedeventdrivendispatcher def newexecutorbasedeventdrivenworkstealingdispatcher... // etc }

Set dispatcher class MyActor extends Actor { dispatcher = Dispatchers.newThreadBasedDispatcher(this)... } actor.dispatcher = dispatcher // before started

Let it crash fault-tolerance

Stolen from Erlang

9 nines

OneForOne fault handling strategy

OneForOne fault handling strategy

OneForOne fault handling strategy

OneForOne fault handling strategy

OneForOne fault handling strategy

OneForOne fault handling strategy

OneForOne fault handling strategy

AllForOne fault handling strategy

AllForOne fault handling strategy

AllForOne fault handling strategy

AllForOne fault handling strategy

Supervisor hierarchies

Supervisor hierarchies

Supervisor hierarchies

Supervisor hierarchies

Fault handlers AllForOneStrategy( maxnrofretries, withintimerange) OneForOneStrategy( maxnrofretries, withintimerange)

Linking link(actor) unlink(actor) startlink(actor) spawnlink[myactor]

trapexit trapexit = List( classof[serviceexception], classof[persistenceexception])

Supervision class Supervisor extends Actor { trapexit = List(classOf[Throwable]) faulthandler = Some(OneForOneStrategy(5, 5000)) def receive = { case Register(actor) => link(actor) } }

Manage failure class FaultTolerantService extends Actor {... override def prerestart(reason: Throwable) = {... // clean up before restart } override def postrestart(reason: Throwable) = {... // init after restart } }

Remote Actors

Remote Server // use host & port in config RemoteNode.start RemoteNode.start("localhost", 9999) Scalable implementation based on NIO (Netty) & Protobuf

Two types of remote actors Client-initated and managed Server-initiated and managed

Client-managed supervision works across nodes // methods in Actor class spawnremote[myactor](host, port) spawnlinkremote[myactor](host, port) startlinkremote(actor, host, port)

Client-managed moves actor to server client manages through proxy val actorproxy = spawnlinkremote[myactor]( darkstar, 9999) actorproxy! message

Server-managed register and manage actor on server client gets dumb proxy handle RemoteNode.register( service:id, new MyService) server part

Server-managed val handle = RemoteClient.actorFor( service:id, darkstar, 9999) handle! message client part

Cluster Membership Cluster.relayMessage( classof[typeofactor], message) for (endpoint < Cluster) spawnremote[typeofactor]( endpoint.host, endpoint.port)

STM another tool in the toolbox

What is STM? 43

STM: overview See the memory (heap and stack) as a transactional dataset Similar to a database begin commit abort/rollback Transactions are retried automatically upon collision Rolls back the memory on abort

Managed References Separates Identity from Value - Values are immutable - Identity (Ref) holds Values Change is a function Compare-and-swap (CAS) Abstraction of time Must be used within a transaction

Managed References val ref = Ref(Map[String, User]()) val users = ref.get val newusers = users + ( bill > User( bill )) ref.swap(newusers)

Transactional datastructures val users = TransactionalMap[String, User]() val users = TransactionalVector[User]()

STM: API import Transaction.Local._ atomic {... atomic { // nested transactions compose... // do something within a transaction } }

Actors + STM = Transactors

Transactors class UserRegistry extends Transactor { private lazy val storage = TransactionalMap[String, User]() def receive = { case NewUser(user) => storage + (user.name > user)... } }

Transactors

Transactors Start transaction

Transactors Start transaction Send message

Transactors Start transaction Send message

Transactors Start transaction Send message Update state within transaction

Transactors Start transaction Send message Update state within transaction

Transactors Start transaction Send message Update state within transaction

Transactors Start transaction Send message Update state within transaction

Transactors Start transaction Send message Update state within transaction

Transactors Start transaction Send message Update state within transaction

Transactors Start transaction Send message Update state within transaction Transaction fails

Transactors Start transaction Send message Update state within transaction Transaction fails

Transactors

Transactors

Transactors

Transactors

Transactors

Transactors

Transactors Transaction automatically retried

Agents yet another tool in the toolbox

Agents val agent = Agent(5) // send function asynchronously agent send (_ + 1) val result = agent() // deref... // use result agent.close Cooperates with STM

How to run it? Deploy as dependency JAR in WEB-INF/lib etc. Run as stand-alone microkernel Soon OSGi-enabled, then drop in any OSGi container (Spring DM server, Karaf etc.)

Akka Persistence

Persistence // transactional Cassandra backed Map val map = CassandraStorage.newMap // transactional Redis backed Vector val vector = RedisStorage.newVector // transactional Mongo backed Ref val ref = MongoStorage.newRef

...and much much more REST Camel Spring Security Comet AMQP Web Guice

Learn more http://akkasource.org

The commercial entity behind Akka http://akkasource.com

EOF

?