71
Building Reactive Systems with Akka Tu Pham CTO @ Dyno.me Google Developer Expert on Cloud Technology

Building reactive applications with akka in java

  • Upload
    tu-pham

  • View
    52

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Building reactive applications with akka in java

Building Reactive Systems

with AkkaTu Pham

CTO @ Dyno.meGoogle Developer Expert on Cloud Technology

Page 2: Building reactive applications with akka in java

The rules of the gamehave changed

Page 3: Building reactive applications with akka in java

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

Page 4: Building reactive applications with akka in java
Page 5: Building reactive applications with akka in java

Reactive applications share four traits

Reactive Applications 5

Page 6: Building reactive applications with akka in java

Reactive applications enrich the user experience with low latency response.

Page 7: Building reactive applications with akka in java

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

Page 8: Building reactive applications with akka in java

Reactive applications react to changes in the world around them.

Page 9: Building reactive applications with akka in java

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

Page 10: Building reactive applications with akka in java

Introducing the Actor Model

Page 11: Building reactive applications with akka in java

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

Page 12: Building reactive applications with akka in java

The essence of an actor from Akka’s perspective

12

1. DEFINE

2. CREATE

3. SEND

4. BECOME

5. SUPERVISE

Page 13: Building reactive applications with akka in java

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

Page 14: Building reactive applications with akka in java

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

Page 15: Building reactive applications with akka in java

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

Page 16: Building reactive applications with akka in java

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");

Page 17: Building reactive applications with akka in java

Guardian System Actor

Actors can form hierarchies

Page 18: Building reactive applications with akka in java

Guardian System Actor

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

Actors can form hierarchies

Page 19: Building reactive applications with akka in java

Foo

Guardian System Actor

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

Actors can form hierarchies

Page 20: Building reactive applications with akka in java

A

Foo

Guardian System Actor

Actors can form hierarchies

Page 21: Building reactive applications with akka in java

A

Foo

Guardian System Actor

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

Actors can form hierarchies

Page 22: Building reactive applications with akka in java

A

B

Bar

Foo

C

B E

A

D

C

Guardian System Actor

Actors can form hierarchies

Page 23: Building reactive applications with akka in java

A

B

Bar

Foo

C

B E

A

C

D

Guardian System Actor

Actors can form hierarchies

Page 24: Building reactive applications with akka in java

A

B

Bar

Foo

C

B E

A

C

D

Guardian System Actor

Name resolution—like a file-system

Page 25: Building reactive applications with akka in java

A

B

Bar

Foo

C

B E

A

C

/Foo

D

Guardian System Actor

Name resolution—like a file-system

Page 26: Building reactive applications with akka in java

A

B

Bar

Foo

C

B E

A

C

/Foo

/Foo/A

D

Guardian System Actor

Name resolution—like a file-system

Page 27: Building reactive applications with akka in java

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

Page 28: Building reactive applications with akka in java

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

Page 29: Building reactive applications with akka in java

2. SEND

X

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

Send the message asynchronously

Pass in the sender ActorRef

Page 30: Building reactive applications with akka in java

Reactive applications are architected to handle failure at all levels.

Page 31: Building reactive applications with akka in java

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

Page 32: Building reactive applications with akka in java

Think Vending Machine

Page 33: Building reactive applications with akka in java

Coffee Machine

Programmer

Think Vending Machine

Page 34: Building reactive applications with akka in java

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Page 35: Building reactive applications with akka in java

Coffee Machine

Programmer

Inserts coins

Add more coins

Think Vending Machine

Page 36: Building reactive applications with akka in java

Coffee Machine

Programmer

Inserts coins

Gets coffee

Add more coins

Think Vending Machine

Page 37: Building reactive applications with akka in java

Coffee Machine

Programmer

Think Vending Machine

Page 38: Building reactive applications with akka in java

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Page 39: Building reactive applications with akka in java

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Out of coffee beans error

Page 40: Building reactive applications with akka in java

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Out of coffee beans

errorWron

g

Page 41: Building reactive applications with akka in java

Coffee Machine

Programmer

Inserts coins

Think Vending Machine

Page 42: Building reactive applications with akka in java

Coffee Machine

Programmer

Inserts coins

Out of coffee beans

error

Think Vending Machine

Page 43: Building reactive applications with akka in java

Coffee Machine

Programmer

Service Guy

Inserts coins

Out of coffee beans

error

Think Vending Machine

Page 44: Building reactive applications with akka in java

Coffee Machine

Programmer

Service Guy

Inserts coins

Out of coffee beans

error

Adds more beans

Think Vending Machine

Page 45: Building reactive applications with akka in java

Coffee Machine

Programmer

Service Guy

Inserts coins

Gets coffee

Out of coffee beans

error

Adds more beans

Think Vending Machine

Page 46: Building reactive applications with akka in java

The Right Way

Service

Client

Page 47: Building reactive applications with akka in java

The Right Way

Service

Client

Request

Page 48: Building reactive applications with akka in java

The Right Way

Service

Client

Request

Response

Page 49: Building reactive applications with akka in java

The Right Way

Service

Client

Request

Validation Error

Response

Page 50: Building reactive applications with akka in java

The Right Way

Service

Client

Request

Validation Error

Application

Response

Error

Page 51: Building reactive applications with akka in java

The Right Way

Service

Client

Supervisor

Request

Validation Error

Application

Response

Error

Page 52: Building reactive applications with akka in java

The Right Way

Service

Client

Supervisor

Request

Validation Error

Application Erro

r

Manages Failure

Response

Page 53: Building reactive applications with akka in java
Page 54: Building reactive applications with akka in java

• Isolate the failure• Compartmentalize• Manage failure

locally• Avoid cascading

failures

Use Bulkheads

Page 55: Building reactive applications with akka in java

• Isolate the failure• Compartmentalize• Manage failure

locally• Avoid cascading

failures

Use Bulkheads

Page 56: Building reactive applications with akka in java

Enter Supervision

Page 57: Building reactive applications with akka in java

A

B

Bar

Foo

C

B E

A

D

C

Supervisor hierarchies

Automatic and mandatory supervision

Page 58: Building reactive applications with akka in java

Reactive applications scale up and down to meet demand.

Page 59: Building reactive applications with akka in java

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

Page 60: Building reactive applications with akka in java

Scale UP Scale OUT

34

Page 61: Building reactive applications with akka in java

Essentially the same thing

34

Page 62: Building reactive applications with akka in java

1.Minimize Contention2.Maximize Locality of

Reference

35

We need to

Page 63: Building reactive applications with akka in java

Share NOTHING

Design

36

Page 64: Building reactive applications with akka in java

Fully event-driven apps are a necessity

Amdahl’s Law will hunt you down

X

Page 65: Building reactive applications with akka in java

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

Page 66: Building reactive applications with akka in java

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

Page 67: Building reactive applications with akka in java

Reactive is being adopted across a wide range of industries.

Page 68: Building reactive applications with akka in java

Finance Internet/Social Media Mfg/Hardware Government Retail

Page 69: Building reactive applications with akka in java

DEMO TIMEA simple game of ping pong

Page 70: Building reactive applications with akka in java

3. BECOME

X

Page 71: Building reactive applications with akka in java

Questions?

Email: [email protected]