Reactive applications and Akka intro used in the Madrid Scala Meetup

108
[email protected] @miguelinlas3 Reactive applications Building concurrent and distributed apps using Akka Miguel Ángel Pastor Olivar

Transcript of Reactive applications and Akka intro used in the Madrid Scala Meetup

Page 1: Reactive applications and Akka intro used in the Madrid Scala Meetup

[email protected] !@miguelinlas3

Reactive applications Building concurrent and distributed apps using Akka

Miguel Ángel Pastor Olivar

Page 2: Reactive applications and Akka intro used in the Madrid Scala Meetup

Writing software for the Liferay infrastructure teamPhd student? on distributed and cloud systems

Scala enthusiast

Twitter: @miguelinlas3

About me

A little, very little, bit of experience with Erlang and OTP

Page 3: Reactive applications and Akka intro used in the Madrid Scala Meetup

Reactive apps: do we really need them? !

Crash course on basic Akka concepts !

Old known problems !

Bonus: A quick look into newer (and more fun tools)

Agenda

Page 4: Reactive applications and Akka intro used in the Madrid Scala Meetup

Applications has become extremely demanding !

New kind of systems !

Event driven, scalable , resilient, responsive !

This is what Reactive Applications are all about

Reactive applications

Page 5: Reactive applications and Akka intro used in the Madrid Scala Meetup

Blocking Caller blocked. Wait for the results !

Non blocking Caller not blocked. Need to pool

!

Asynchronous Caller not blocked. Select, epoll, signals

!

Event driven Message flowing through the system

Reactive applications: definitions

Page 6: Reactive applications and Akka intro used in the Madrid Scala Meetup

Basic pillars

Page 7: Reactive applications and Akka intro used in the Madrid Scala Meetup

Basic pillars

Event Driven

Page 8: Reactive applications and Akka intro used in the Madrid Scala Meetup

Basic pillars

Event Driven

Resilient

Page 9: Reactive applications and Akka intro used in the Madrid Scala Meetup

Basic pillars

Event Driven

Resilient Scalable

Page 10: Reactive applications and Akka intro used in the Madrid Scala Meetup

Basic pillars

Event Driven

Responsive

Resilient Scalable

Page 11: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event drivenOld known problems, The actor model

Page 12: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Page 13: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Page 14: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Page 15: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Thread 1

Page 16: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Thread 1 Thread 2

Page 17: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Thread 1 Thread 2

Page 18: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Thread 1 Thread 2

Page 19: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Thread 1 Thread 2

R/W

Page 20: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Thread 1 Thread 2

R/WR/W

Page 21: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going Event Driven

Shared Memory

Thread 1 Thread 2

R/WR/W AVOID IT!!

Page 22: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

Page 23: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

Composition

Page 24: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionLocks don´t compose

Page 25: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionGranularity

Locks don´t compose

Page 26: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionGranularity

Locks don´t compose

Have I taken too few locks? Too many?

Page 27: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionGranularity

Encapsulation

Locks don´t compose

Have I taken too few locks? Too many?

Page 28: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionGranularity

Encapsulation

Locks don´t compose

Have I taken too few locks? Too many?

Breaking abstractions

Page 29: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionGranularity

EncapsulationCorrectness

Locks don´t compose

Have I taken too few locks? Too many?

Breaking abstractions

Page 30: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionGranularity

EncapsulationCorrectness

Locks don´t compose

Have I taken too few locks? Too many?

Have I taken the correct lock?

Breaking abstractions

Page 31: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionGranularity

EncapsulationCorrectness

Ordering

Locks don´t compose

Have I taken too few locks? Too many?

Have I taken the correct lock?

Breaking abstractions

Page 32: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: problems with locks

CompositionGranularity

EncapsulationCorrectness

Ordering

Locks don´t compose

Have I taken too few locks? Too many?

Have I taken the correct lock?

Have I used the correct order?

Breaking abstractions

Page 33: Reactive applications and Akka intro used in the Madrid Scala Meetup

Asynchronous message/event passing !

Workflow of the events through your system !

Benefits: Better throughput Lower latencies Loosely coupled solutions

Going event driven: designing your system

Page 34: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: Amdahl’s law

Page 35: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: the actor model

Fundamental unit of computation that embodies: • Processing • Storage • Communication !

Three axioms. When an Actor receives a message it can: • Create new Actors • Send messages to Actors it knows • Designate how it should handle the next message it receives !Carl Hewitt

Page 36: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: the actor model

Page 37: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: the actor model

Page 38: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: the actor model

Page 39: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going event driven: the actor model

Zero sharing !

Isolated, lightweight event based processes !

Communication through asynchronous message passing !

Location transparent !

Built-in supervision

Page 40: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Define a new actor

class Frontend extends Actor { … ! def receive = { case work => implicit val timeout = Timeout(5.seconds) ! (mediator ? Send("/user/master/active", work, localAffinity = false)) map { case Master.Ack(_) => Ok } recover { case _ => NotOk } pipeTo sender ! } !}

Page 41: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Creating an actor

val system = ActorSystem(systemName) !Cluster(system).join(joinAddress) !val frontend = system.actorOf(Props[Frontend], “frontend") !system.actorOf(Props(classOf[WorkProducer], frontend), “producer") !system.actorOf(Props[WorkResultConsumer], "consumer")

Page 42: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Getting a reference

system = ActorSystem(systemName) !val initialContacts = Set(system.actorSelection(RootActorPath(contactAddress) / "user" / "receptionist"))

Page 43: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Sending a message

case Work(workId, job) => … workExecutor ! job …

Page 44: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Replying to a message

case RegisterWorker(workerId) => if (workers.contains(workerId)) { workers += (workerId -> workers(workerId).copy(ref = sender)) } else { workers += (workerId -> WorkerState(sender, status = Idle)) if (pendingWork.nonEmpty) sender ! WorkIsReady }

Page 45: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Switch implementation

def waitForWorkIsDoneAck(result: Any): Receive = { case Ack(id) if id == workId => sendToMaster(WorkerRequestsWork(workerId)) ! context.setReceiveTimeout(Duration.Undefined) context.become(idle) }

Page 46: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Routing

val writer = system.actorOf( Props(new TweetWriter(cluster)).withRouter(RoundRobinRouter(nrOfInstances = 100)))

Page 47: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Configuring a router

akka { actor { deployment { /TweetWriter { router = round-robin nr-of-instances = 100 } } } }

Page 48: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Dispatchers

Default dispatcher !

PinnedDispatcher !

BalancingDispatcher !

CallingThreadDispatcher

Page 49: Reactive applications and Akka intro used in the Madrid Scala Meetup

Actor model: Dispatchers

my-pinned-dispatcher { executor = "thread-pool-executor" type = PinnedDispatcher } !val myActor = context.actorOf(Props[MyActor].withDispatcher("my-pinned-dispatcher"), "myactor2")

Page 50: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalableDistributed computing, The actor model

Page 51: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: scalability

I really do have an scalability problem ... !

if my system is fast for a single user but slow when the system is under heavy load

Page 52: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: transparent distributed computing

Distributed shared mutable state !

Distributed objects !

Distributed transactions

Page 53: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: transparent distributed computing

Synchronous method dispatching across the network is a bad idea !

Ignores partial failures !

Raises Latency !

General scalability and DC concerns

Page 54: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: transparent distributed computing

EMBRACE THE NETWORK!!

Page 55: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: remote actors

akka { actor { deployment { /metricsActor { provider = akka.remote.RemoteActorProvider

remote = "akka://MonitoringServiceSystem@hostname:5557"

} } } }

val monitoringServiceSystem = ActorSystem(“MonitoringServiceSystem");

!def metricsActor = monitoringServiceSystem.actorOf( new Props(MetricsProcessorActor.class), "metricsActor");

No changes are required in our existing code!!

Page 56: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: clustering

Gossip cluster membership !

Failure detector !

Cluster death watch !

Cluster aware routers

Page 57: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: cluster membership

Dynamo based (heavily influenced by Riak) !

Gossip protocol for state dissemination !

Cluster membership Configuration data Leader determination !

Vector clocks to detect convergence !

!

Page 58: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: leaders

P2P approach !

Any node can be the leader !

Deterministically recognised by all nodes

Page 59: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going scalable: cluster usageakka { actor { provider = "akka.cluster.ClusterActorRefProvider" } remote { log-remote-lifecycle-events = off netty.tcp { hostname = "127.0.0.1" port = 0 } } cluster { seed-nodes = [ "akka.tcp://[email protected]:2551", "akka.tcp://[email protected]:2552"] auto-down-unreachable-after = 10s } }

Page 60: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going resilientError handling

Page 61: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going resilient: common problems

Single thread of control !

Thread blows up --> you are screwed !

Error handling within single thread !

Errors don´t get propagated !

Defensive programming Tangled within business logic Scattered along all our codebase

Page 62: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going resilient: actor topology and supervision

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B5

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7

Page 63: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for one

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B5

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7

Page 64: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for one

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7

Page 65: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for one

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7

Page 66: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for one

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7

Page 67: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for one

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7

Page 68: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for one

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

Page 69: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B5

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B6

B7

Page 70: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B6

B7

Page 71: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B6

B7

Page 72: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B6

B7

Page 73: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B6

B7

Page 74: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B6

B7

Page 75: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B6

B7

Page 76: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B6

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B6

B7

Page 77: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B6

B7

Page 78: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B7

Page 79: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B6

B7

Page 80: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

B7

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B6

B7

Page 81: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B6

B7

Page 82: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B6

Page 83: Reactive applications and Akka intro used in the Madrid Scala Meetup

Supervision strategies: One for all

Syste

A1

A2

A4

B1

B2

B4

B3

A3

/A1

/A1/A2

/A1/A2/A4

/A1/A3

/B1

/B1/B2

/B1/B2/B4

/B1/B3

/B1/B3/B5

/B1/B2/B6

/B1/B2/B7B5

B6

B7

Page 84: Reactive applications and Akka intro used in the Madrid Scala Meetup

Going resilient: configure supervision

override def supervisorStrategy = OneForOneStrategy() { case _: ActorInitializationException => Stop case _: DeathPactException => Stop case _: Exception => currentWorkId foreach { workId => sendToMaster(WorkFailed(workerId, workId)) } context.become(idle) Restart }

Supervision strategies are pluggable and you can develop and configure your own

Page 85: Reactive applications and Akka intro used in the Madrid Scala Meetup

Something more?Of course!

Page 86: Reactive applications and Akka intro used in the Madrid Scala Meetup

Something more

Akka Http (previously know as Spray.io) !

Akka.io !

Akka Reactive Streams !

Akka Event Sourced !

Dataflow

Page 87: Reactive applications and Akka intro used in the Madrid Scala Meetup

TakeawaysQuick summary of main ideas

Page 88: Reactive applications and Akka intro used in the Madrid Scala Meetup

Takeaways

Go reactive !

Avoid locking (share nothing, lock free algs) !

Already in the JVM? Akka could be good choice !

Distributed systems are hard !

Use the correct level of abstraction !

And remember ...

Page 89: Reactive applications and Akka intro used in the Madrid Scala Meetup

Takeaways

When all you have is a hammer everything looks like

a nail. !

Choose the

right tool for your job!

Page 90: Reactive applications and Akka intro used in the Madrid Scala Meetup

Questions?And let’s hope answers

Page 91: Reactive applications and Akka intro used in the Madrid Scala Meetup

BONUS!We deserve better and more fun tools

Page 92: Reactive applications and Akka intro used in the Madrid Scala Meetup

More tools: futures

•Spawn concurrent computations !

•Write once - Read many !

•Freely sharable !

•Non-blocking composition !

•Composable (monadic operations) !

•Managing failure

Page 93: Reactive applications and Akka intro used in the Madrid Scala Meetup

More tools: futures

val f: Future[List[String]] = future { session.getRecentPosts } !// handle both cases: Success and Failure f onComplete { case Success(posts) => for (post <- posts) println(post) case Failure(t) => println("An error has occured: " +

t.getMessage) } !// only handle Success f onSuccess { case posts => for (post <- posts) println(post) } !// only handle Failure f onFailure { case t => println("An error has occured: " + t.getMessage) }

Page 94: Reactive applications and Akka intro used in the Madrid Scala Meetup

More tools: agents

•Reactive memory cells !

•Send update function to an agent !

•Reads are “free” !

•Composable !

•Originally in Clojure, borrowed by Akka

Page 95: Reactive applications and Akka intro used in the Madrid Scala Meetup

More tools: agents

// declare the agent val counter = Agent(0) !// send the function: enqueued the change counter send { _ + 1} !// long running or blocking operations !implicit val ec = defineSomeExecutionContext() agent sendOff blockingOperation !!// read the agent’ s value val agentValue = counter.value

Page 96: Reactive applications and Akka intro used in the Madrid Scala Meetup

More tools: software transactional memory

Sometimes we will deal with shared state Heap + Stack: transactional dataset: !

Begin, commit, rollback !

Retry on collision !

Rollback on abort !

Transaction composability

Page 97: Reactive applications and Akka intro used in the Madrid Scala Meetup

More tools: reactive extensions

•Futures + Stream concept !

•Composables !

•Async and event based !

•Push collections !

•JVM available through the RxJava project

Page 98: Reactive applications and Akka intro used in the Madrid Scala Meetup

More tools: reactive extensions

!def simpleComposition() { customObservableNonBlocking() .skip(10) .take(5) .map({ stringValue -> return stringValue + "_transformed"}) .subscribe({ println "nextElement => " + it}) } // we should an output like this !nextElement => foo_10_transformed nextElement => foo_11_transformed nextElement => foo_12_transformed nextElement => foo_13_transformed nextElement => foo_14_transformed

Page 99: Reactive applications and Akka intro used in the Madrid Scala Meetup

More tools: reactive extensions!def Observable<T> getData(int id) { if(availableInMemory) { return Observable.create({ observer -> observer.onNext(valueFromMemory); observer.onCompleted(); }) } else { return Observable.create({ observer -> executor.submit({ try { T value = getValueFromRemoteService(id); observer.onNext(value); observer.onCompleted(); }catch(Exception e) { observer.onError(e); } }) }); } }

Page 100: Reactive applications and Akka intro used in the Madrid Scala Meetup

Useful approaches

Where should I use each tool?

Page 101: Reactive applications and Akka intro used in the Madrid Scala Meetup

Layers of complexity

Declarative & immutable core Logic or functional programming Futures and/or dataflow

!

Non-determinism when needed Actors, Agents or Rx

!

Shared mutability Protected by Software Transactional Memory

!

Finally, only if it is really needed Locks and explicit threads

Page 102: Reactive applications and Akka intro used in the Madrid Scala Meetup

Takeways

A quick summary

Page 103: Reactive applications and Akka intro used in the Madrid Scala Meetup

References

Interesting resources

Page 104: Reactive applications and Akka intro used in the Madrid Scala Meetup

References: slides, talks and books

High Performance Networking in the JVM by Eric Onnen !

Going reactive talk by Jonas Bonér !

Introducing Akka by Jonas Bonér !

The future I was promised by Viktor Klang !

Real world Akka recipes by Björn Antonsson et all

Page 105: Reactive applications and Akka intro used in the Madrid Scala Meetup

References: slides, talks and books

Programming Concurrency in the JVM: Mastering synchronization, STM and Actors by Venkat Subramaniam !

Systems than run forever Self-heal by Joe Amstrong !

The art of multiprocessor programming, Revised Reprint by Maurice Herlihy !

Reactive Manifesto

Page 106: Reactive applications and Akka intro used in the Madrid Scala Meetup

References: slides, talks and books

Akka Concurrency: Building reliable software in a multi-core world !

Reative Design Patterns (available as Manning MEAP at the time of this writing)

Page 107: Reactive applications and Akka intro used in the Madrid Scala Meetup

References: frameworks and toolkits

Erlang !

Akka Toolkit !

Scala language !

Vert.x !

Clojure language !

GPars

Page 108: Reactive applications and Akka intro used in the Madrid Scala Meetup

References: frameworks and toolkits

Cloud Haskell !

NodeJS !

Netty IO !

Finagle