Reactive programming with scala and akka

download Reactive programming with scala and akka

If you can't read please download the document

Transcript of Reactive programming with scala and akka

Reactive programming with Scala and Akka

Piyush MishraSoftware ConsultantKnoldus Software LLP

Piyush Mishra

Agenda

Reactive programming and why we need it.

Brief introduction to Scala

Brief introduction to Akka

Reactive applications.

Four principles of reactive applications.

Piyush Mishra

Present Day

How do we write applications today?

AFoo

Piyush Mishra

Present Day

Till now we program like this

(new Foo).doSomething1(); (new Foo).doSomething2(); (new Foo).doSomething3(); (new Foo).printOutput

Synchronous ?Blocking ?Sequential ?Piyush Mishra

Reactive programming is completely different from the sequential work flow. They react when something is done

Worker ADone and react

Worker BDone and reactWorker CDone and react

OutputReactive programming

Piyush Mishra

Reactive programming Why?

Fast, faster, fastest .. fastetstestestest ...

Piyush Mishra

Reactive programming Why?

Piyush Mishra

What is reactive?

Piyush Mishra

What is reactive - Dissection

Piyush Mishra

What is reactive - Dissection

Piyush Mishra

What is reactive - Dissection

Piyush Mishra

What is reactive - Dissection

Piyush Mishra

What is reactive - Together

Piyush Mishra

Concurrent application uses shared memory to communicate.

Shared memory

Thread AThread BReactive Parts Event Driven

R/WR/WSynchronization Non-determinismHard to understand code

Event driven system avoid shared memory communicationAnd uses message passing model throughout theApplication

Continued..

Actor AActor B

MessageReactive Parts Event Driven

Reactive Parts Event Driven

SenderMessageReceiver

Loosely coupledEasier to extendEvolve

Receiver dormant till it receives message

Large number of receivers share same hardware thread

Low latencyHigh throughput

Lower operational costs

Piyush Mishra

Reactive Parts Event Driven

UIWeb LayerServicesDatabaseApplication must bereactive from top tobottom.

None of the layersshould stall.

Piyush Mishra

Reactive Parts Event Driven

We need to go in parallel. PERIOD!

Piyush Mishra

Reactive Parts Scalable

The word scalable is defined by Merriam-Webster as capable of being easily expanded or upgraded on demand

Piyush Mishra

Reactive Parts Scalable

Node 1Actor ANode 2Actor B

MessageAsynchronous message passing

Location transparency

The network is inherently unreliable

Piyush Mishra

Reactive Parts Resilient

Isolate failuresPrevent cascading failures

Piyush Mishra

Reactive Parts Resilient

Node 1Actor ANode 2Actor B

MessageLoose couplingIsolated components

Hierarchy of failure management

Piyush Mishra

Reactive Parts Responsive

Responsive is defined by Merriam-Webster as "quick to respond or react appropriately".

Respond immediately

Even in event of failure

Piyush Mishra

Reactive Parts Responsive

Response event is returned in O(1) or at least O(log n) time regardless of load

How ?

Batching when there is high traffic

Queues bound with back pressure

Failures handled with recovery and circuit breakers

Piyush Mishra

Reactive the whole

Piyush Mishra

Reactive How?

Piyush Mishra

Reactive How?

Piyush Mishra

Scala Basics

Piyush Mishra

Piyush Mishra

Scala Basics

Piyush Mishra

Scala Basics

1. Scala method definitions

def fun(x: Int) = {result}

2 . def fun = result

3 .Scala variable definitions

var x: Int = expressionval x: String = expression1. Java method definitions

Int fun(int x) {return result}

2. (no parameterless methods)

3. java variable definitions

Int x = expressionfinal String x = expression

Piyush Mishra

Scala Basics Concise code

Piyush Mishra

Scala Basics

pure object system

operator overloading

closures

mixin composition with traits specialtreatment of interfaces

existential types

abstract

pattern matching

static members

primitive types

break and continue

Interfaces

Wildcards

raw types

enums

Piyush Mishra

Akka is the framework developed by Typesafe Right abstraction with actors for concurrent, fault-tolerant and scalable applications

For Fault-Tolerance uses Let It Crash model Abstraction for transparent distribution of load

We can Scale In and Scale Out

What is Akka

Piyush Mishra

Truly highly concurrent systems

Truly scalable systems

Self-healing, fault-tolerant systems

Problem Which Akka Solve

Piyush Mishra

Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notification etc

Low level concurrency becomes Simple Workflow - we only thinkin terms of message flows in system

We get high CPU utilization, low latency, high throughput andscalability - for free as part of this model

Proven and superior model for detecting and recovering fromerrors

Akka

Piyush Mishra

Main part of Akka

Understanding the ACTOR model

Akka

Piyush Mishra

Actor Model

Messages are in mailbox but no thread is allocatedPiyush Mishra

Actor Model

Actor read the message , a thread is allocated to itAnd it apply behavior on it and state changePiyush Mishra

Actor Model

After processing thread is deallocatedPiyush Mishra

Define actor

Define message on which actor will react. case object Hello

Define actor Class HelloActor extends Actor { def receive = { case Hello => println(Hello) case _=> println(I did not understand) } }

Tying it back Event Driven

Message Passing

! ?

Piyush Mishra

Tying it back Event Driven

Code

Piyush Mishra

Tying it back Resilience

Piyush Mishra

Resilient

Applications which are fault tolerant, detect failure and recover from failure are resilient.

Such system are self healed and unstoppable

Actor System

Guardian actor Actor AActor BActor DActor CActor System can be think of as home for actors

Piyush Mishra

Actors form parent child relationship

Guardian actor Actor AActor BActor DActor C

/A/A/C/B/B/DPiyush Mishra

One for one Strategy

Guardian System actor

XY

z

One for one Strategy

Only x will be restated

Guardian System actor

XY

z

One for all Strategy

Guardian System actor

XY

z

One for all Strategy

All children will be restated

Guardian System actor

XY

z

Tying it back Resilience

Code

Piyush Mishra

Tying it back Scalability

Piyush Mishra

Scalable

Applications which are able to scale up/down as the load Increases or decreases are scalable.

Scaling is of two types. 1. Scaling the application on all cpu's core is called scale up. 2. Scaling the application to multiple machine is called scale out

Akka Scalability Code

Responsive

Application which are able to respond even if failure occurs or load increases are responsive.

Responsive

Bringing these three traits (event driven, resilient, and Scalable) together make to the applications responsive and all these four qualities together make the application reactive.

Reactive platform components

Play (Reactive we application framework built on top of akka) Akka Clustering Akka persistence

Adaptive load balancing

Software Transnational memory

Thanks