Building reactive applications with akka in java

Post on 07-Jan-2017

52 views 1 download

Transcript of Building reactive applications with akka in java

Building Reactive Systems

with AkkaTu Pham

CTO @ Dyno.meGoogle Developer Expert on Cloud Technology

The rules of the gamehave changed

Apps in the 60s-90s were written for

Apps today are written forSingle machines

Single core processors

Expensive RAM

Expensive disk

Slow networks

Few concurrent users

Small data sets

Latency in seconds

Clusters of machines

Multicore processors

Cheap RAM

Cheap disk

Fast networks

Lots of concurrent users

Large data sets

Latency in milliseconds

3

Reactive applications share four traits

Reactive Applications 5

Reactive applications enrich the user experience with low latency response.

Responsive• Real-time, engaging, rich and collaborative

• Create an open and ongoing dialog with users• More efficient workflow; inspires a feeling of connectedness• Fully Reactive enabling push instead of pull

“The move to these technologies is already paying off.Response times are down for processor intensive code–such as

image and PDF generation–by around 75%.”

Brian Pugh, VP of Engineering, Lucid Software

7

Reactive applications react to changes in the world around them.

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

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

“Clearly, the goal is to do these operations concurrently andnon-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

9

Introducing the Actor Model

A computational model that embodies:

11

✓ 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

The Actor Model

The essence of an actor from Akka’s perspective

12

1. DEFINE

2. CREATE

3. SEND

4. BECOME

5. SUPERVISE

public abstract class BaseEvent implements Serializable { private final String id; private final JsonElement data; // Can be Json Primitive, Object, Array private int created;

public BaseEvent(String id, JsonElement data) { this.id = id; this.data = data; this.created = DateTimeUtil.getCurrentUnixTime(""); } // getter & setter here }

X

0. DEFINE

public class Event extends BaseEvent { private EventType eventType;

public Event(String id, JsonElement data){ super(id, data); }

public Event(String id, EventType eventType, JsonElement data) { super(id, data); this.eventType = eventType; }}

X

0. DEFINE

public class SimpleActor extends UntypedActor { @Override public void onReceive(Object msg) throws Exception { if (msg instanceof Event) { // Say hello System.out.println(“Hello ” + msg.getId()) } }}

X

0. DEFINE

Give it a name

1. CREATE

You get an ActorRef back

Create the Actor

Create an Actor systemActor configuration

ActorSystem system = ActorSystem.create("MySystem");

ActorRef greeter = system.actorOf(Props.create(SimpleActor.class), “greeter");

Guardian System Actor

Actors can form hierarchies

Guardian System Actor

system.actorOf(Props.create(Foo.class), “Foo”);

Actors can form hierarchies

Foo

Guardian System Actor

system.actorOf(Props.create(Foo.class), “Foo”);

Actors can form hierarchies

A

Foo

Guardian System Actor

Actors can form hierarchies

A

Foo

Guardian System Actor

context().actorOf(Props.create(A.class), “A”);

Actors can form hierarchies

A

B

Bar

Foo

C

B E

A

D

C

Guardian System Actor

Actors can form hierarchies

A

B

Bar

Foo

C

B E

A

C

D

Guardian System Actor

Actors can form hierarchies

A

B

Bar

Foo

C

B E

A

C

D

Guardian System Actor

Name resolution—like a file-system

A

B

Bar

Foo

C

B E

A

C

/Foo

D

Guardian System Actor

Name resolution—like a file-system

A

B

Bar

Foo

C

B E

A

C

/Foo

/Foo/A

D

Guardian System Actor

Name resolution—like a file-system

A

B

Bar

Foo

C

B E

A

C

/Foo

/Foo/A

/Foo/A/B

D

Guardian System Actor

Name resolution—like a file-system

A

B

Bar

Foo

C

B E

A

C

/Foo

/Foo/A

/Foo/A/B

Guardian System Actor

/Foo/A/D

D

Name resolution—like a file-system

2. SEND

X

simpleRef.tell(new Event(new UUID().toString(), null, jsonObject), null);

Send the message asynchronously

Pass in the sender ActorRef

Reactive applications are architected to handle failure at all levels.

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

“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

21

Think Vending Machine

Coffee Machine

Programmer

Think Vending Machine

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Coffee Machine

Programmer

Inserts coins

Add more coins

Think Vending Machine

Coffee Machine

Programmer

Inserts coins

Gets coffee

Add more coins

Think Vending Machine

Coffee Machine

Programmer

Think Vending Machine

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Out of coffee beans error

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Out of coffee beans

errorWron

g

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Coffee Machine

Programmer

Inserts coins

Out of coffee beans

error

Think Vending Machine

Coffee Machine

Programmer

Service Guy

Inserts coins

Out of coffee beans

error

Think Vending Machine

Coffee Machine

Programmer

Service Guy

Inserts coins

Out of coffee beans

error

Adds more beans

Think Vending Machine

Coffee Machine

Programmer

Service Guy

Inserts coins

Gets coffee

Out of coffee beans

error

Adds more beans

Think Vending Machine

The Right Way

Service

Client

The Right Way

Service

Client

Request

The Right Way

Service

Client

Request

Response

The Right Way

Service

Client

Request

Validation Error

Response

The Right Way

Service

Client

Request

Validation Error

Application

Response

Error

The Right Way

Service

Client

Supervisor

Request

Validation Error

Application

Response

Error

The Right Way

Service

Client

Supervisor

Request

Validation Error

Application Erro

r

Manages Failure

Response

• Isolate the failure• Compartmentalize• Manage failure

locally• Avoid cascading

failures

Use Bulkheads

• Isolate the failure• Compartmentalize• Manage failure

locally• Avoid cascading

failures

Use Bulkheads

Enter Supervision

A

B

Bar

Foo

C

B E

A

D

C

Supervisor hierarchies

Automatic and mandatory supervision

Reactive applications scale up and down to meet demand.

Elastic• Elasticity and Scalability to embrace the

Cloud• Adaptive Scale on Demand• 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

33

Scale UP Scale OUT

34

Essentially the same thing

34

1.Minimize Contention2.Maximize Locality of

Reference

35

We need to

Share NOTHING

Design

36

Fully event-driven apps are a necessity

Amdahl’s Law will hunt you down

X

Typesafe Activatorhttp://typesafe.com/platform/getstarted

Typesafe Reactive Platform

Actors are asynchronous and communicate via message passingSupervision and clustering in support of fault tolerance

Purely asynchronous and non-blocking web frameworksNo container required, no inherent bottlenecks in session management

Asynchronous and immutable programming constructsComposable abstractions enabling simpler concurrency and parallelism

48

Reactive is being adopted across a wide range of industries.

Finance Internet/Social Media Mfg/Hardware Government Retail

DEMO TIMEA simple game of ping pong

3. BECOME

X

Questions?

Email: tu@dyno.me