Top Banner
WEBINAR Understanding Akka Streams, Back Pressure and Asynchronous Architectures by Konrad Malawski (@ktosopl)
65

Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Jan 07, 2017

Download

Software

Lightbend
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

WEBINAR

Understanding Akka Streams, Back Pressure and Asynchronous Architectures

by Konrad Malawski (@ktosopl)

Page 2: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Konrad `ktoso` Malawski

Akka Team, Reactive Streams TCK,

Persistence, HTTP

Page 3: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Konrad `@ktosopl` Malawski

akka.iotypesafe.comgeecon.org

Java.pl / KrakowScala.plsckrk.com

GDGKrakow.pl lambdakrk.pl

Page 4: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures
Page 5: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

“Stream”

Page 6: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

“Stream” What does it mean?!

Page 7: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka Streams

Akka Streams && Reactive Streams

Page 8: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Why back-pressure?

?

Page 9: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Why back-pressure?

So you’ve built your app and it’s awesome.

Page 10: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Why back-pressure?

Let’s not smash it horribly under load.

Page 11: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

What is back-pressure?

?

Page 12: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

What is back-pressure?

Page 13: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

No no no…! Not THAT Back-pressure!

No no no…! Not THAT Back-pressure!

What is back-pressure?

Page 14: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Publisher[T] Subscriber[T]

Back-pressure explained

Page 15: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Fast Publisher Slow Subscriber

Push model

Page 16: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Subscriber usually has some kind of buffer.

Push model

Page 17: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Push model

Page 18: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Push model

Page 19: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

What if the buffer overflows?

Push model

Page 20: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Use bounded buffer, drop messages + require re-sending

Push model

Page 21: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Kernel does this!Routers do this!

(TCP)

Use bounded buffer, drop messages + require re-sending

Push model

Page 22: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Increase buffer size… Well, while you have memory available!

Push model

Page 23: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Push model

DEMO

Page 24: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Reactive Streams explained

Reactive Streams explained in 1 slide

Page 25: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Fast Publisher will send at-most 3 elements. This is pull-based-backpressure.

Reactive Streams: “dynamic push/pull”

Page 26: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

JEP-266 – soon…!public final class Flow { private Flow() {} // uninstantiable

@FunctionalInterface public static interface Publisher<T> { public void subscribe(Subscriber<? super T> subscriber); }

public static interface Subscriber<T> { public void onSubscribe(Subscription subscription); public void onNext(T item); public void onError(Throwable throwable); public void onComplete(); }

public static interface Subscription { public void request(long n); public void cancel(); }

public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> { }}

Page 27: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Reactive Streams: goals

1) Avoiding unbounded buffering across async boundaries

2)Inter-op interfaces between various libraries

Page 28: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries

2) Inter-op interfaces between various libraries

Argh, implementing a correct RS Publisher or Subscriber is so hard!

Page 29: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries

2) Inter-op interfaces between various libraries

Argh, implementing a correct RS Publisher or Subscriber is so hard!

Page 30: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries

2) Inter-op interfaces between various libraries

Argh, implementing a correct RS Publisher or Subscriber is so hard!

You should be using Akka Streams abstractions instead!

Page 31: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka Streams

Streams complement Actors, they do not replace them.

Actors – distribution (location transparency) Streams – back-pressured + more rigid-blueprint

Page 32: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka is a Toolkit, pick the right tools for the job.

Runar’s excellent talk @ Scala.World 2015

Asynchronous processing toolbox:

Power

Constraints

Page 33: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka is a Toolkit, pick the right tools for the job.

Asynchronous processing toolbox:

Constraints

Power

Page 34: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka is a Toolkit, pick the right tools for the job.

Single value, no streaming by definition.Local abstraction. Execution contexts.

Power

Constraints

Page 35: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka is a Toolkit, pick the right tools for the job.

Mostly static processing layouts.Well typed and Back-pressured!

Constraints

Power

Page 36: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka is a Toolkit, pick the right tools for the job.

Plain Actor’s younger brother, experimental.Location transparent, well typed.Technically unconstrained in actions performed

Constraints

Power

Page 37: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka is a Toolkit, pick the right tools for the job.

Runar’s excellent talk @ Scala.World 2015

Location transparent. Various resilience mechanisms.

(watching, persistent recovering, migration, pools)

Untyped and unconstrained in actions performed.

Constraints

Power

Page 38: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka Streams

streams

Page 39: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka Streams in 20 seconds: // types: Source[Out, Mat] Flow[In, Out, Mat] Sink[In, Mat]

// generally speaking, it's always: val ready = Source(???).via(flow).map(_ * 2).to(sink)

val mat: Mat = ready.run() // the usual example: val f: Future[String] = Source.single(1).map(_.toString).runWith(Sink.head)

Proper static typing!

Page 40: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka Streams in 20 seconds:

// types: _Source[Int, Unit] Flow[Int, String, Unit] Sink[String, Future[String]]

Source.single(1).map(_.toString).runWith(Sink.head)

Page 41: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka Streams in 20 seconds:

// types: _Source[Int, Unit] Flow[Int, String, Unit] Sink[String, Future[String]]

Source.single(1).map(_.toString).runWith(Sink.head)

Page 42: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Materialization

Gears from GeeCON.org, did I mention it’s an awesome conf?

Page 43: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

What is “materialization” really?

Page 44: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

What is “materialization” really?

Page 45: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

What is “materialization” really?

Page 46: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

What is “materialization” really?

Page 47: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka Streams & HTTP

streams& HTTP

Page 48: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka HTTP

Joint effort of Spray and Akka teams. Complete HTTP Server/Client implementation.

Learns from Spray’s 3-4 years history.

Since the beginning with streaming as first class citizen.

Side note: Lagom also utilises Akka Streams for streaming.

Page 49: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Streaming in Akka HTTPDEMO

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

Page 50: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Streaming in Akka HTTPDEMO

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

HTTP Entity as a: Source[ByteString, _]

Page 51: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Streaming in Akka HTTPDEMO

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

HTTP Entity as a: Source[ByteString, _]

Websocket connection as a: Flow[ws.Message, ws.Message]

Page 52: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Persistence Query (experimental)

“The Query Side” of Akka Persistence

Page 53: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Persistence Query (experimental)

Persistence Query Journals

akka/akka-persistence-cassandra 0.16 akka/akka @ leveldb-journal 2.4.8

dnvriend/akka-persistence-jdbc 2.3.3 scullxbones/akka-persistence-mongo 1.2.5

…and more, that I likely forgot about.

Implementation of “data-pump” pattern.

Page 54: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Akka + Kafka = BFF

Reactive Kafka

+

Started by Krzysiek Ciesielski & Adam Warski @ SofwareMill.com

Page 55: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

“ACKnowladged streams”

happy ACKing!

Page 56: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Kafka + Akka = BFF

Akka is Arbitrary processing.

Kafka is somewhat more than a message queue, but very focused on “the log”.

Spark shines with it’s data-science focus.

Page 57: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Kafka + Akka = BFF

Page 58: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Kafka + Akka = BFF

Page 59: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Streams talking to Actors &&

Actors talking to Streams

Page 60: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Streams <=> Actors inter-op

Source.actorRef (no back-pressure) Source.queue (safe)

Sink.actorRef (no back-pressure) Sink.actorRefWithAck (safe)

Page 61: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Exciting times ahead!

Page 62: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Next steps for Akka

Completely new Akka Remoting (goal: 1M+ msg/s (!)),(it is built using Akka Streams).

More integrations for Akka Streams stages, also dynamic fan-in/out A.K.A. “the Hub”.

Reactive Kafka polishing and stable release with SoftwareMill.

“Confirmed Streams” work from Reactive Kafka generalised.

Akka Typed likely to progress again.

Of course, continued maintenance of Cluster and others.

Page 64: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

lightbend.com/pov

Reactive RoundtableWorld Tour by Lightbend

lightbend.com/reactive-roundtable

Proof of Value ServiceAccelerate Project Success

Page 65: Understanding Akka Streams, Back Pressure, and Asynchronous Architectures

Q/A

ktoso @ lightbend.com twitter: ktosopl

github: ktosoteam blog: letitcrash.com

home: akka.io