Building Reactive applications with Akka

50

description

Knoldus organized a Meetp up on Wednesday, 29 Oct 2014 at 5:00PM. Mr. Nilanjan Raychaudhuri from Typesafe presented this session on "Building Reactive applications with Akka". He did the session remotely from Germany and it was very well accepted by the audience at the meetup. Nilanjan is a consultant/trainer and member of Play framework team. He works for Typesafe. He has more than 14 years of experience managing and developing software solutions in Java, Ruby, Groovy and also in Scala. He is zealous about programming in Scala ever since he got introduced to this beautiful language. He enjoys sharing his experience via talks in various conferences and he is also the author of the “Scala in Action” book. We would like to thank Nilanjan for his time and his valuable insights. Checkout this presentation to find how Akka help you to build Reactive applications.

Transcript of Building Reactive applications with Akka

Page 1: Building Reactive applications with Akka
Page 2: Building Reactive applications with Akka

Building Reactive applicationsusing Akka

Nilanjan Raychaudhuri@nraychaudhuri

Page 3: Building Reactive applications with Akka

Reactive  Applications

3

@nraychaudhuri

Page 4: Building Reactive applications with Akka

Essence of Reactive applications

Page 5: Building Reactive applications with Akka

Reactive Traits

Page 6: Building Reactive applications with Akka
Page 7: Building Reactive applications with Akka

Akka is enabler of Reactive applications

Page 8: Building Reactive applications with Akka

Reactive  Applications

• Individual entities that can only communicate by passing messages

• Excellent for isolating mutable state and protecting it without locks

• Location transparency for remote and clustered topologies

• Supervision

• Well-suited for creating state machines

• Fault tolerance via Supervision

8

Page 9: Building Reactive applications with Akka

Reactive  Applications

Reference Architecture

9

Play  Server

Play  Server

Play  Server

Play  Server

Web  Tier  (Play) Work  Tier  (Akka)

Akka  Master  Router

Akka  Router  Standby

Akka  Worker

Akka  Worker

Akka  Worker

Akka  Worker

Akka  Worker

responsive event drivenscalableresilient

Page 10: Building Reactive applications with Akka

Message-driven

Reactive applications are architected based on loosely coupled design

Page 11: Building Reactive applications with Akka

Reactive  Applications

Message-Driven• Loosely coupled architecture, easier to extend, maintain, evolve

• Asynchronous and non-blocking• Concurrent by design, immutable state• Lower latency and higher throughput

11

“Clearly, the goal is to do these operations concurrently and non-blocking, so that entire blocks of seats or sections are not locked.

We’re able to find and allocate seats under load in less than 20ms without trying very hard to achieve it.”  

Andrew Headrick, Platform Architect, Ticketfly

Page 12: Building Reactive applications with Akka

Introducing Actor Model

Page 13: Building Reactive applications with Akka

13

A computational model that embodies:

✓ Processing

✓ Storage

✓ Communication

Supports 3 axioms—when an Actor receives a message it can:

1.Create new Actors

2.Send messages to Actors it knows

3.Designate how it should handle the next message it receives

Actor model

Page 14: Building Reactive applications with Akka

Thread pool

Actor modelActor model

Actor Actor

Page 15: Building Reactive applications with Akka

Akka  actor  -­‐  Definition

15

Page 16: Building Reactive applications with Akka

Akka  actor  -­‐  Create

16

Page 17: Building Reactive applications with Akka

Akka  actor  -­‐  Send

17

Page 18: Building Reactive applications with Akka

Elastic

Reactive applications scale up and down to meet demand

Page 19: Building Reactive applications with Akka

Elastic• Scalability and elasticity to embrace the Cloud

• Leverage all cores via asynchronous programming• Clustered servers support joining and leaving of nodes• More cost-efficient utilization of hardware

“Our traffic can increase by as much as 100x for 15 minutes each day. Until a couple of years ago, noon was a stressful time.

Nowadays, it’s usually a non-event.”  

Eric Bowman, VP Architecture, Gilt Groupe

19

Page 20: Building Reactive applications with Akka

20

Page 21: Building Reactive applications with Akka

FlightSearchActor

21

Page 22: Building Reactive applications with Akka

FlightSearch Actor - I

22

Page 23: Building Reactive applications with Akka

FlightSearch Actor - II

23

Page 24: Building Reactive applications with Akka

FlightSearchActor

24

Page 25: Building Reactive applications with Akka

SearchWorker - I

25

Page 26: Building Reactive applications with Akka

SearchWorker - II

26

Page 27: Building Reactive applications with Akka

ProviderProxyActor

27

Page 28: Building Reactive applications with Akka

ProviderProxy - I

28

Page 29: Building Reactive applications with Akka

ProviderProxy - II

29

Page 30: Building Reactive applications with Akka

Flight search flow

30

Page 31: Building Reactive applications with Akka

Let it crash

Page 32: Building Reactive applications with Akka

Reactive applications are architected to handle failure at all levels.

Page 33: Building Reactive applications with Akka

Resilient• Failure is embraced as a natural state in the app lifecycle

• Resilience is a first-class construct• Failure is detected, isolated, and managed• Applications self heal

33

“The Typesafe Reactive Platform helps us maintain a very aggressive development and deployment cycle, all in a fail-forward manner.

It’s now the default choice for developing all new services.”  

Peter Hausel, VP Engineering, Gawker Media

Page 34: Building Reactive applications with Akka

Failure?

34

Page 35: Building Reactive applications with Akka

Think Vending Machine

Page 36: Building Reactive applications with Akka

Coffee MachineProgrammer

Inserts coins

Gets coffee

Add more coins

Think Vending Machine

Page 37: Building Reactive applications with Akka

Coffee MachineProgrammer

Service Guy

Inserts coins

Gets coffee

Out of coffee beans

error

Adds more beans

Think Vending Machine

Out of coffee beans errorWrong

Page 38: Building Reactive applications with Akka

The Right Way

ServiceClient

Supervisor

Request

Response

Validation Error

Application Error

Manages Failure

Page 39: Building Reactive applications with Akka

Supervision

39

Page 40: Building Reactive applications with Akka

A

B

BarFoo

C

BE

A

C

system.actorOf(Props[Foo], “Foo”)

context.actorOf(Props[A], “A”)

Actor Topology

Page 41: Building Reactive applications with Akka

OneForOne

Page 42: Building Reactive applications with Akka

AllForOne

Page 43: Building Reactive applications with Akka

Reactive  Applications

Isolate failure at all levels• Within one node

• Across many nodes (JVMs)

• Across many physical servers

• Across data centers

43

Page 44: Building Reactive applications with Akka

Scale out

Page 45: Building Reactive applications with Akka

45

Akka clustering

Page 46: Building Reactive applications with Akka

Reactive  Applications

IoT

46

Page 47: Building Reactive applications with Akka

Reactive  Applications

Precision Farming

47

Akka ClusterJournal

Page 48: Building Reactive applications with Akka

Code

Page 49: Building Reactive applications with Akka

Q & Option[A]

@nraychaudhuri

Page 50: Building Reactive applications with Akka

©Typesafe 2014 – All Rights Reserved