take a look at akka+java (english version)

48
TAKE A LOOK AT AKKA + JAVA Dmytro Mantula GlobalLogic, Kyiv

Upload: globallogic-ukraine

Post on 16-Apr-2017

800 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Take a Look at Akka+Java (English version)

TAKE A LOOK AT AKKA + JAVA

Dmytro Mantula GlobalLogic, Kyiv

Page 2: Take a Look at Akka+Java (English version)

PREVIOUS AGE OF COMPUTING SYSTEMS

• Monolith software architecture

• Managed servers and containers

• RDBMS, transactions isolation

• Scalability: scale-up by more powerful hardware

• Proprietary enterprise solutions

Page 3: Take a Look at Akka+Java (English version)

NEW CHALLENGES

• response time: s -> ms

• high availability: 3 nines (8h/y) -> 5+ nines (5– m/y)

• storage: GBs (109) -> PBs (1015)+

• hardware: spread from mobile phone to 1000+ nodes cluster

Page 4: Take a Look at Akka+Java (English version)

MOORE’S LAW

Page 5: Take a Look at Akka+Java (English version)

EVIL OF CPU PERFORMANCE:

MOORE’S LAW DOESN’T WORK

ANYMORE FOR A SINGLE CORE

Page 6: Take a Look at Akka+Java (English version)

The more CPU cores a system has,

the faster it is.

True or false?

Page 7: Take a Look at Akka+Java (English version)

EVIL OF PARALLELISM:

AMDAHL’S LAW

Gene Amdahl 1967

Page 8: Take a Look at Akka+Java (English version)

STUBBORN AMDAHL’S LAW

The speedup of a program using multiple processors in parallel computing

is limited by the sequential fraction of the program.

Page 9: Take a Look at Akka+Java (English version)

EVIL OF CONCURRENCY:

SHARED MUTABLE STATE

Page 10: Take a Look at Akka+Java (English version)

«Do not communicate by sharing memory.

Instead, share memory by communicating»

– Effective Go

Page 11: Take a Look at Akka+Java (English version)

v1.0: July 2013 v2.0: Sept 2014

REACTIVE SOFTWARE DESIGNhttp://www.reactivemanifesto.org/

Page 12: Take a Look at Akka+Java (English version)

• rapid and consistent response times • response in SLA time may be more

important than late correct response • problems may be detected quickly

and dealt with effectively

react to usersResponsive

Page 13: Take a Look at Akka+Java (English version)

v1.0: July 2013 v2.0: Sept 2014

REACTIVE SOFTWARE DESIGNhttp://www.reactivemanifesto.org/

Page 14: Take a Look at Akka+Java (English version)

react to loadElastic

• No contention points or central bottlenecks • ability to shard or replicate components

and distribute inputs among them

• Automatic resource management • scale-up • scale-out • scale-down • scale-in

Page 15: Take a Look at Akka+Java (English version)

v1.0: July 2013 v2.0: Sept 2014

REACTIVE SOFTWARE DESIGNhttp://www.reactivemanifesto.org/

Page 16: Take a Look at Akka+Java (English version)

• “Let it crash!”: there is no way to think about every single failure point

• failure is not dealt with as an error • means that some capacity of the

system will be reduced • dealt with as a message

react to failuresResilient

Page 17: Take a Look at Akka+Java (English version)

v1.0: July 2013 v2.0: Sept 2014

REACTIVE SOFTWARE DESIGNhttp://www.reactivemanifesto.org/

Page 18: Take a Look at Akka+Java (English version)

Asynchronous message-passing • loose coupling • isolation of components • location transparency • failures as messages

Benefits: • load management • elasticity • flow control (monitoring , back-pressure)

react to eventsMessage Driven

Page 19: Take a Look at Akka+Java (English version)

Application should be reactive

from top to bottom

Page 20: Take a Look at Akka+Java (English version)

v1.0: July 2013 v2.0: Sept 2014

REACTIVE SOFTWARE DESIGNhttp://www.reactivemanifesto.org/

Page 21: Take a Look at Akka+Java (English version)

CARL HEWITT, 1973

ACTOR MODEL

• Describes:

• message processing algorithm

• data storage principles

• interaction between modules

• Erlang

• Used in telecommunication systems

• High Availability of 9 “nines”

Page 22: Take a Look at Akka+Java (English version)

• Written in Scala • Stable since 2009 • Part of Scala Standard Library since 2013 • Supports Java 8 since 2014

by

ex

Page 23: Take a Look at Akka+Java (English version)

WHAT IS AN ACTOR?

Actor is a unit of code with a mailbox

and an internal state that just responds to messages

in a single thread

(br ief ly)

Page 24: Take a Look at Akka+Java (English version)

IDLE

TWO STATES OF ACTOR

Page 25: Take a Look at Akka+Java (English version)

IDLE

TWO STATES OF ACTOR

MESSAGE PROCESSING

Page 26: Take a Look at Akka+Java (English version)

WHAT IS AN ACTOR?

• Similar to object in OOP, but message-driven • Even more isolated than object: no explicit access

exposed (hidden behind ActorRef) • no shared state • location transparent (can live in different JVMs)

• Light-weight: 300B memory footprint (millions per GB) • No threads allocated in idle state • Single-threaded invocation inside, sequential message

processing • Supervises children actors

Page 27: Take a Look at Akka+Java (English version)

WHAT CAN ACTOR DO?

• If no messages being processed:

• Nothing

• When message being processed (one at a time):

• make local decisions

• send messages to other actors (incl. sender() and self())

• do other actions with side-effects (IO, logs, DB access)

• change own behavior for next messages

• create more actors (and promise to supervise them)

Page 28: Take a Look at Akka+Java (English version)

BENEFITS

• You’re not going to have multiple threads modifying a variable at the same time.

• Forget about:

• Shared state • Threads • Locks, “synchronized” • Concurrent collections • wait/notify/notifyAll

• Describe only business behavior in the code. • Akka and app configuration care about everything else.

Page 29: Take a Look at Akka+Java (English version)

MESSAGE CLASS

• Obligatory: purely-immutable

• Desirably: serializable

• Good practice: declared with recipient Actor class

Page 30: Take a Look at Akka+Java (English version)

MESSAGE CLASS

• Obligatory: purely-immutable

• Desirably: serializable

• Good practice: declared with recipient Actor class

SCALA:

Page 31: Take a Look at Akka+Java (English version)

ACTOR IN JAVA 7

Page 32: Take a Look at Akka+Java (English version)

ACTOR IN JAVA 7

Strict @Override

Page 33: Take a Look at Akka+Java (English version)

ACTOR IN JAVA 7

Casting boilerplate

Strict @Override

Page 34: Take a Look at Akka+Java (English version)

ACTOR IN JAVA 7

Casting boilerplate

Strict @Override

“if” hell

Page 35: Take a Look at Akka+Java (English version)

ACTOR IN JAVA 7

Casting boilerplate

Strict @Override

“if” hell

Do not forget!!!

Page 36: Take a Look at Akka+Java (English version)

ACTOR IN JAVA 8

Page 37: Take a Look at Akka+Java (English version)

Partial function

ACTOR IN JAVA 8

Page 38: Take a Look at Akka+Java (English version)

Partial function

Behavior as a constant

ACTOR IN JAVA 8

Page 39: Take a Look at Akka+Java (English version)

Partial function

Behavior as a constant

Type inference

within lambda

ACTOR IN JAVA 8

Page 40: Take a Look at Akka+Java (English version)

Partial function

Behavior as a constant

Type inference

within lambda

ACTOR IN JAVA 8

Predicate

Page 41: Take a Look at Akka+Java (English version)

ACTORSYSTEM AND ENTRY POINT

Page 42: Take a Look at Akka+Java (English version)

DONEC QUIS NUNC

SUPERVISION MODEL

Page 43: Take a Look at Akka+Java (English version)

ROUTING

• RoundRobin • Random • SmallestMailbox • Broadcast • ScatterGatherFirstCompleted • (Custom)

Page 44: Take a Look at Akka+Java (English version)

REMOTE ACTORS

Page 45: Take a Look at Akka+Java (English version)

WHERE TO USE AKKA?

• Multi-user concurrency (e.g. gambling systems)

• Rule based systems (like trading systems)

• Data storages with lots of write operations

• Systems with high-uptime requirements (like telecom)

• Processing pipelines

• Streaming data

• Reactive frameworks

Page 46: Take a Look at Akka+Java (English version)

DERIVATIVE FRAMEWORKS/TOOLS• For testing actors: Akka TestKit

• Software Transactional Memory: Akka STM

• Finite State Machines: Akka FSM

• Durable mailboxes & other persistence models

• Akka IO, Akka Streams, Akka Cluster

• Akka HTTP (ex. Spray)

• Play Framework

• Akka Camel, Akka Spring

• Activator

Page 47: Take a Look at Akka+Java (English version)

RESOURCES

http://akka.io/

http://lightbend.com

http://letitcrash.com

Coursera: Principles of Reactive Programming

Page 48: Take a Look at Akka+Java (English version)

THANKS!

Q & A