Introduction to Akka-Streams

58
INTRODUCTION TO AKKA-STREAMS Dmytro Mantula GlobalLogic, Kyiv

Transcript of Introduction to Akka-Streams

Page 1: Introduction to Akka-Streams

INTRODUCTION TO

AKKA-STREAMS

Dmytro Mantula GlobalLogic, Kyiv

Page 2: Introduction to Akka-Streams

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: Introduction to Akka-Streams

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: Introduction to Akka-Streams

MOORE’S LAW

Page 5: Introduction to Akka-Streams

EVIL OF CPU PERFORMANCE:

MOORE’S LAW DOESN’T WORK

ANYMORE FOR A SINGLE CORE

Page 6: Introduction to Akka-Streams

The more CPU cores a system has,

the faster it is.

True or false?

Page 7: Introduction to Akka-Streams

EVIL OF PARALLELISM:

AMDAHL’S LAW

Gene Amdahl 1967

Page 8: Introduction to Akka-Streams

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: Introduction to Akka-Streams

EVIL OF CONCURRENCY:

SHARED MUTABLE STATE

Page 10: Introduction to Akka-Streams

«Do not communicate by sharing memory.

Instead, share memory by communicating»

– Effective Go

Page 11: Introduction to Akka-Streams

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

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

Page 12: Introduction to Akka-Streams

• 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: Introduction to Akka-Streams

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 14: Introduction to Akka-Streams

• “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 15: Introduction to Akka-Streams

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 16: Introduction to Akka-Streams

Application should be reactive

from top to bottom

Page 17: Introduction to Akka-Streams

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

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

Page 18: Introduction to Akka-Streams

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 19: Introduction to Akka-Streams

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

by

ex

Page 20: Introduction to Akka-Streams

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 21: Introduction to Akka-Streams

IDLE

TWO STATES OF ACTOR

Page 22: Introduction to Akka-Streams

IDLE

TWO STATES OF ACTOR

MESSAGE PROCESSING

Page 23: Introduction to Akka-Streams

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 24: Introduction to Akka-Streams

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 25: Introduction to Akka-Streams

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 26: Introduction to Akka-Streams

ACTOR AND ACTOR MESSAGE

purely-immutable serializable

Page 27: Introduction to Akka-Streams

ACTORSYSTEM AND ENTRY POINT

Page 28: Introduction to Akka-Streams

OTHER FEATURES OF AKKA

• Supervision model

• Routing via configuration

• Remote actors via configuration

• Clustering via configuration

• etc.

Page 29: Introduction to Akka-Streams

WEAKNESS OF AKKA #1:

NO DSL FOR DATA FLOW AND

TYPE-UNSAFE MESSAGE PASSING

Page 30: Introduction to Akka-Streams

ACTORS BECAME JUST ANOTHER

CONCURRENCY PRIMITIVE

AND SHOULD BE HIDDEN BEHIND HIGHER-LEVEL ABSTRACTIONS

Page 31: Introduction to Akka-Streams

WEAKNESS OF AKKA #2:

AGGRESSIVE STRATEGIES OF DEALING WITH

BUFFER OVERFLOW

Page 32: Introduction to Akka-Streams

DANGEROUS SITUATION

PUBLISHER SUBSCRIBERData

Asyn

chro

nous

bo

unda

ry

Page 33: Introduction to Akka-Streams

Asyn

chro

nous

bo

unda

ry

DON’T PUSH BACK – JUST DON’T ASK

BACK-PRESSURE

PUBLISHER SUBSCRIBER

Demand

Data

Page 34: Introduction to Akka-Streams

HOW CAN PRODUCER DEAL WITH BACK-PRESSURE?

• Slow down producing data (if it can)

• Buffer (using bounded buffer, of course)

• Drop elements

• Fail

Page 35: Introduction to Akka-Streams

PARTIALLY BACK-PRESSURED STREAM

Page 36: Introduction to Akka-Streams

PARTIALLY BACK-PRESSURED STREAM

THE WHOLE STREAM SHOULD BE BACK-PRESSURED

Page 37: Introduction to Akka-Streams

REACTIVE STREAMS GOALS• minimalistic • asynchronous • never block (waste time/resources)

• safe (back-threads pressure)

• purely local abstraction • allow synchronous implementations

• compatible with TCP

Page 38: Introduction to Akka-Streams

REACTIVE STREAMS

• 2013 – “reactive non-blocking asynchronous back-pressure”

• 2015 – JEP-266: Introduced to OpenJDK 9 by Doug Lea

http://openjdk.java.net/jeps/266

Page 39: Introduction to Akka-Streams

REACTIVE STREAMS: INTERFACES

http://www.reactive-streams.org/

Page 40: Introduction to Akka-Streams

AKKA-STREAMS• Implements Reactive Streams, transparently for

the user

• Uses Akka under the hood:

• Derived actors can be configured using akka configuration

• Clustering, networking, HTTP, profiling tools

Page 41: Introduction to Akka-Streams

DEMO PRINT OUT LIST(“HELLO”, “WORLD”)

Page 42: Introduction to Akka-Streams

AKKA-STREAMS MAIN BUILDING BLOCKS

Source Flow Sink

Page 43: Introduction to Akka-Streams

AKKA-STREAMS ALGEBRA

• Source + Sink = RunnableGraph

• Source + Flow = (composite) Source

• Flow + Sink = (composite) Sink

• Flow + Flow = (composite) Flow

Page 44: Introduction to Akka-Streams

DEMO PRINT (1 TO 10)

MULTIPLICATION BY 2

Page 45: Introduction to Akka-Streams

MATERIALIZATION

Page 46: Introduction to Akka-Streams

MATERIALIZATION

Page 47: Introduction to Akka-Streams

MATERIALIZATION

Page 48: Introduction to Akka-Streams

MATERIALIZED VALUES

?

Page 49: Introduction to Akka-Streams

DEMO ACTORREF AS MATERIALIZED VALUE

SUM OF EVEN NUMBERS

Page 50: Introduction to Akka-Streams

GRAPH DSL

MORE COMPLEX SHAPES

Fan-In Fan-Out BidiFlow

UniformFanInShape UniformFanOutShapeFanInShape1FanInShape2FanInShapeN

FanOutShape1FanOutShape2FanOutShapeN

Page 51: Introduction to Akka-Streams

PLAIN FLOW DSL VS

GRAPH DSL

Page 52: Introduction to Akka-Streams

DEMO BROADCAST-MERGE GRAPH

Page 53: Introduction to Akka-Streams

DEMO BACK-PRESSURE

ON .THROTTLE AND ZIP

Page 54: Introduction to Akka-Streams

SOURCES OUT OF THE BOX

Page 55: Introduction to Akka-Streams

SINKS OUT OF THE BOX

Page 56: Introduction to Akka-Streams

OTHER FEATURES OF AKKA-STREAMS

• mapAsync for external calls • Custom shapes • Subgraph customisation with Attributes • Custom processing with GraphStage • Operator Fusing • Supervision Strategies via ActorMaterializerSettings • Integration with other RS implementations • Streams TestKit

Page 57: Introduction to Akka-Streams

RESOURCES

Akka: http://akka.io/ http://lightbend.com http://letitcrash.com

Principles of Reactive Programming @ Coursera

Akka Streams: http://doc.akka.io/docs/akka/current/scala/stream/

Page 58: Introduction to Akka-Streams

THANKS! Q & A