Slide 1

Slide 1 text

Scale by the Bay 2018 Stefano Bonetti - @svez_faz

Slide 2

Slide 2 text

Monitoring Reactive Streams Stefano Bonetti - @svez_faz

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Agenda Throughput Backpressure

Slide 5

Slide 5 text

Agenda Throughput Backpressure

Slide 6

Slide 6 text

Throughput Web Service req res

Slide 7

Slide 7 text

Topology

Slide 8

Slide 8 text

Topology

Slide 9

Slide 9 text

I/O Correlation web service

Slide 10

Slide 10 text

I/O Correlation Processor[T, S]

Slide 11

Slide 11 text

Transparent Processors Processor[T, S] Processor[T, S] Subscriber[S] Publisher[T]

Slide 12

Slide 12 text

Transparent Processors Processor[T, S] Processor[T, S] Subscriber[S] Publisher[T] c1 c2 c3 c4 Processor[T, T] Processor[S, S]

Slide 13

Slide 13 text

val randomInt = Source.tick(10.millis, 10.millis, NotUsed) .map(_ ⇒ Random.nextInt()) val evenOnly = Flow[Int].filter(_ % 2 == 0) val print = Sink.foreach(println) randomInt .via(evenOnly) .runWith(print) AKKA STREAMS - THROUGHPUT

Slide 14

Slide 14 text

val randomInt = Source.tick(10.millis, 10.millis, NotUsed) .map(_ ⇒ Random.nextInt()) val evenOnly = Flow[Int].filter(_ % 2 == 0) val print = Sink.foreach(println) randomInt .via(evenOnly) .runWith(print) AKKA STREAMS - THROUGHPUT

Slide 15

Slide 15 text

val randomInt = Source.tick(10.millis, 10.millis, NotUsed) .map(_ ⇒ Random.nextInt()) val evenOnly = Flow[Int].filter(_ % 2 == 0) val print = Sink.foreach(println) def meter[T](name: String): Flow[T, T, NotUsed] = { val msgCounter = Kamon.metrics.counter(name) Flow[T].map { x ⇒ msgCounter.increment(); x } } randomInt .via(meter("produced")) .via(evenOnly) .via(meter("processed")) .runWith(print) AKKA STREAMS - THROUGHPUT

Slide 16

Slide 16 text

val randomInt = Source.tick(10.millis, 10.millis, NotUsed) .map(_ ⇒ Random.nextInt()) val evenOnly = Flow[Int].filter(_ % 2 == 0) val print = Sink.foreach(println) def meter[T](name: String): Flow[T, T, NotUsed] = { val msgCounter = Kamon.metrics.counter(name) Flow[T].map { x ⇒ msgCounter.increment(); x } } randomInt .via(meter("produced")) .via(evenOnly) .via(meter("processed")) .runWith(print) AKKA STREAMS - THROUGHPUT

Slide 17

Slide 17 text

Throughput dashboard (/s) randomInt .via(meter("produced")) .via(heavyEvenOnly) .via(meter("processed")) .runWith(print)

Slide 18

Slide 18 text

Agenda Throughput Backpressure

Slide 19

Slide 19 text

Agenda Throughput Backpressure

Slide 20

Slide 20 text

Example Validation/ Filtering Commit Kafka offset valid invalid Store via API

Slide 21

Slide 21 text

Example 100/s 25/s 75/s 75/s Validation/ Filtering Commit Kafka offset valid invalid Store via API

Slide 22

Slide 22 text

Example 100/s 25/s 75/s 75/s Validation/ Filtering Commit Kafka offset valid invalid Store via API 20000/s lag

Slide 23

Slide 23 text

Backpressure

Slide 24

Slide 24 text

Reactive Streams 101 Processor Publisher Subscriber request request emit emit

Slide 25

Slide 25 text

Reactive Streams 101 Processor Publisher Subscriber request request emit emit

Slide 26

Slide 26 text

Reactive Streams 101 Processor Publisher Subscriber request request emit emit

Slide 27

Slide 27 text

Reactive Streams 101 Processor Publisher Subscriber request request emit emit

Slide 28

Slide 28 text

Reactive Streams 101 Processor Publisher Subscriber request request emit emit

Slide 29

Slide 29 text

Backpressure Processor Publisher Subscriber request ? request ? emit emit

Slide 30

Slide 30 text

Backpressure Processor Publisher Subscriber request request emit ? emit ?

Slide 31

Slide 31 text

Backpressure Processor Publisher Subscriber request request emit emit request emit request request emit

Slide 32

Slide 32 text

Akka Streams - Graphstage API

Slide 33

Slide 33 text

final case class Map[In, Out](f: In ⇒ Out) extends GraphStage[FlowShape[In, Out]] { val in = Inlet[In]("Map.in") val out = Outlet[Out]("Map.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { override def onPush(): Unit = push(out, f(grab(in))) override def onPull(): Unit = pull(in) setHandlers(in, out, this) } } AKKA STREAMS - GRAPHSTAGE API

Slide 34

Slide 34 text

final case class Map[In, Out](f: In ⇒ Out) extends GraphStage[FlowShape[In, Out]] { val in = Inlet[In]("Map.in") val out = Outlet[Out]("Map.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { override def onPush(): Unit = push(out, f(grab(in))) override def onPull(): Unit = pull(in) setHandlers(in, out, this) } } AKKA STREAMS - GRAPHSTAGE API

Slide 35

Slide 35 text

final case class Map[In, Out](f: In ⇒ Out) extends GraphStage[FlowShape[In, Out]] { val in = Inlet[In]("Map.in") val out = Outlet[Out]("Map.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { override def onPush(): Unit = push(out, f(grab(in))) override def onPull(): Unit = pull(in) setHandlers(in, out, this) } } AKKA STREAMS - GRAPHSTAGE API

Slide 36

Slide 36 text

final case class Map[In, Out](f: In ⇒ Out) extends GraphStage[FlowShape[In, Out]] { val in = Inlet[In]("Map.in") val out = Outlet[Out]("Map.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { override def onPush(): Unit = push(out, f(grab(in))) override def onPull(): Unit = pull(in) setHandlers(in, out, this) } } AKKA STREAMS - GRAPHSTAGE API

Slide 37

Slide 37 text

final case class Map[In, Out](f: In ⇒ Out) extends GraphStage[FlowShape[In, Out]] { val in = Inlet[In]("Map.in") val out = Outlet[Out]("Map.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { override def onPush(): Unit = push(out, f(grab(in))) override def onPull(): Unit = pull(in) setHandlers(in, out, this) } } AKKA STREAMS - GRAPHSTAGE API

Slide 38

Slide 38 text

final case class Map[In, Out](f: In ⇒ Out) extends GraphStage[FlowShape[In, Out]] { val in = Inlet[In]("Map.in") val out = Outlet[Out]("Map.out") override val shape = FlowShape(in, out) override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with InHandler with OutHandler { override def onPush(): Unit = push(out, f(grab(in))) override def onPull(): Unit = pull(in) setHandlers(in, out, this) } } AKKA STREAMS - GRAPHSTAGE API

Slide 39

Slide 39 text

Akka Streams Graphstage onPull onPush onPull onPull onPush

Slide 40

Slide 40 text

var lastPulled: Long = System.nanoTime() var lastPushed: Long = lastPulled private val backpressure = Kamon.histogram(label + "_backpressure") override def onPush(): Unit = { push(out, grab(in)) val now = System.nanoTime() backpressure.record((lastPulled - lastPushed) * 100 / now - lastPushed) lastPushed = now } override def onPull(): Unit = { pull(in) lastPulled = System.nanoTime() } setHandlers(in, out, this) AKKA STREAMS - BACKPRESSURE

Slide 41

Slide 41 text

var lastPulled: Long = System.nanoTime() var lastPushed: Long = lastPulled private val backpressure = Kamon.histogram(label + "_backpressure") override def onPush(): Unit = { push(out, grab(in)) val now = System.nanoTime() backpressure.record((lastPulled - lastPushed) * 100 / now - lastPushed) lastPushed = now } override def onPull(): Unit = { pull(in) lastPulled = System.nanoTime() } setHandlers(in, out, this) AKKA STREAMS - BACKPRESSURE

Slide 42

Slide 42 text

var lastPulled: Long = System.nanoTime() var lastPushed: Long = lastPulled private val backpressure = Kamon.histogram(label + "_backpressure") override def onPush(): Unit = { push(out, grab(in)) val now = System.nanoTime() backpressure.record((lastPulled - lastPushed) * 100 / now - lastPushed) lastPushed = now } override def onPull(): Unit = { pull(in) lastPulled = System.nanoTime() } setHandlers(in, out, this) AKKA STREAMS - BACKPRESSURE

Slide 43

Slide 43 text

var lastPulled: Long = System.nanoTime() var lastPushed: Long = lastPulled private val backpressure = Kamon.histogram(label + "_backpressure") override def onPush(): Unit = { push(out, grab(in)) val now = System.nanoTime() backpressure.record((lastPulled - lastPushed) * 100 / now - lastPushed) lastPushed = now } override def onPull(): Unit = { pull(in) lastPulled = System.nanoTime() } setHandlers(in, out, this) AKKA STREAMS - BACKPRESSURE

Slide 44

Slide 44 text

val randomInt = Source.tick(10.millis, 10.millis, NotUsed) .map(_ ⇒ Random.nextInt()) val evenOnly = Flow[Int].filter(_ % 2 == 0) val print = Sink.foreach(println) randomInt .via(backpressureMeter("produced")) .via(evenOnly) .via(backpressureMeter("processed")) .runWith(print) AKKA STREAMS - BACKPRESSURE

Slide 45

Slide 45 text

Backpressure dashboard (%) randomInt .via(backpressureMeter("produced")) .via(heavyEvenOnly) .via(backpressureMeter("processed")) .runWith(print)

Slide 46

Slide 46 text

val randomInt = Source.tick(10.millis, 10.millis, NotUsed) .map(_ ⇒ Random.nextInt()) val evenOnly = Flow[Int].filter(_ % 2 == 0) val print = Sink.foreach(println) randomInt .via(backpressureMeter("produced")) .via(evenOnly) .via(backpressureMeter("processed")) .runWith(print) AKKA STREAMS - BACKPRESSURE

Slide 47

Slide 47 text

val randomInt = Source.tick(10.millis, 10.millis, NotUsed) .map(_ ⇒ Random.nextInt()) val heavyEvenOnly = Flow[Int].mapAsync(parallelism = 1) { n => after(250.millis, system.scheduler)(Future.successful(n)) }.filter(_ % 2 == 0) val print = Sink.foreach(println) randomInt .via(backpressureMeter("produced")) .via(heavyEvenOnly) .via(backpressureMeter("processed")) .runWith(print) AKKA STREAMS - BACKPRESSURE

Slide 48

Slide 48 text

Bottleneck - throughput dashboard (/s) randomInt .via(meter("produced")) .via(heavyEvenOnly) .via(meter("processed")) .runWith(print)

Slide 49

Slide 49 text

Bottleneck - backpressure dashboard (%) randomInt .via(backpressureMeter("produced")) .via(heavyEvenOnly) .via(backpressureMeter("processed")) .runWith(print)

Slide 50

Slide 50 text

Bottlenecks https://blog.colinbreck.com/maximizing-throughput-for-akka-streams/

Slide 51

Slide 51 text

Agenda Throughput Backpressure

Slide 52

Slide 52 text

akka-stream-checkpoint svezfaz/akka-stream-checkpoint throughput backpressure ratio push-pull latency liveness checks failures/completion

Slide 53

Slide 53 text

Stefano Bonetti Software Engineer @svezfaz @svez_faz