Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Akka-chan's Survival Guide for the Streaming World

3,927 views

Published on

In this talk we dive into the various kinds of "Streaming", what it actually means, where to use which technology and specifically take a look at Akka Streams and their specific use case and strengths.

Published in: Technology
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Akka-chan's Survival Guide for the Streaming World

  1. 1. Konrad `ktoso` Malawski @ Scala Matsuri 2017 @ Tokyo Survival guide for the Streaming World Akka-chan’s
  2. 2. Me, Tokyo & Akka Streams… The road to 2017… 2014年: リアクティブストリーム始動
  3. 3. 0.7 Early preview RS started (late 2013) 2014 2016年: JDK 9 への導入決定 Reactive Streams started in late 2013, initiated by Roland Kuhn,Viktor Klang, Erik Meijer, Ben Christiansen
  4. 4. 0.7 Early preview RS begins! 2.0 Experimental RS on way to JDK9 2014 2016 2014年: リアクティブストリーム始動2017年: 安定版。リアクティブストリーム付きの JDK9。
  5. 5. 0.7 Early preview RS begins! 2.0 Experimental RS on way to JDK9 2.4.17 + 10.0.3 Fully stable RS in JDK9 (!) 2014 2016 2017 2014年: リアクティブストリーム始動2016年: JDK 9 への導入決定
  6. 6. Konrad `ktoso` Malawski Akka Team, Reactive Streams TCK, Scala SLIP Committee member
  7. 7. Konrad `@ktosopl` Malawski work: akka.io lightbend.com personal blog: http://kto.so communities: geecon.org Java.pl / KrakowScala.pl sckrk.com GDGKrakow.pl lambdakrk.pl
  8. 8. Konrad `@ktosopl` Malawski work: akka.io lightbend.com personal blog: http://kto.so communities: geecon.org Java.pl / KrakowScala.pl sckrk.com GDGKrakow.pl lambdakrk.pl
  9. 9. 2017年: 安定版。リアクティブストリーム付きの JDK9。
  10. 10. The concurrent & distributed applications toolkit Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM 2017年: 安定版。リアクティブストリーム付きの JDK9。
  11. 11. Actors – Concurrency / high perf. messaging Cluster – Location transparent, resilient clusters Persistence – EventSourcing support (many DBs) Distributed Data – Availability-first gossiped CRDTs HTTP – Fully Async & Reactive Http Server (Websockets, Http/2) soon:
 Typed – really well-typed Actors, coming this year …and much more (kafka, cassandra, testing, …)
  12. 12. “Stream”
  13. 13. “Stream” なんですか? 2017年: 安定版。リアクティブストリーム付きの JDK9。
  14. 14. Suddenly everyone jumped on the word “Stream”. Akka Streams / Reactive Streams started end-of-2013. “Streams” * when put in “” the word does not appear in project name, but is present in examples / style of APIs / wording.
  15. 15. Suddenly everyone jumped on the word “Stream”. Akka Streams / Reactive Streams started end-of-2013. The word “Stream” is used in many contexts/meanings Akka Streams Reactive Streams RxJava “streams”* Spark Streaming Apache Storm “streams”* Java Steams (JDK8) Reactor “streams”* Kafka Streams ztellman / Manifold (Clojure) * when put in “” the word does not appear in project name, but is present in examples / style of APIs / wording. Apache GearPump “streams” Apache [I] Streams (!) Apache [I] Beam “streams” Apache [I] Quarks “streams” Apache [I] Airflow “streams” (dead?) Apache [I] Samza Scala Stream Scalaz Streams, now known as FS2 Swave.io Java InputStream / OutputStream / … :-) 2017年: 安定版。リアクティブストリーム付きの JDK9。
  16. 16. “Stream” What does it mean?! • Possibly infinite datasets (“streams”) • “Streams are NOT collections.” • Processed element-by-element • Element could mean “byte” • More usefully though it means a specific type “T” • Asynchronous processing • Asynchronous boundaries (between threads) • Network boundaries (between machines) 2017年: 安定版。リアクティブストリーム付きの JDK9。
  17. 17. The new “big data” is “fast data”. Enabled by reactive building blocks. “Fast Data” 2017年: 安定版。リアクティブストリーム付きの JDK9。
  18. 18. The typical “big data” architecture ever since Hadoop entered the market Legacy Hadoop architectures would mostly look like this: (legacy, batch oriented architectures) 典型的な「ビッグデータ」アーキテクチャ
  19. 19. The typical “big data” architecture ever since Hadoop entered the market Legacy Hadoop architectures would mostly look like this: (legacy, batch oriented architectures) 典型的な「ビッグデータ」アーキテクチャ
  20. 20. Fast Data – architecture example Many of these technologies compose together into what we call Fast Data architectures. 典型的な「ビッグデータ」アーキテクチャ
  21. 21. Fast Data – architecture example It’s like big-data, but FAST - which often implies various kinds of streaming. 典型的な「ビッグデータ」アーキテクチャ
  22. 22. Where does Akka Stream fit? Akka Streams specifically fits, if you answer yes to any of these: • Should it take on public traffic? • Processing in hot path for requests? • Integrate various technologies? • Protect services from over-load? • Introspection, debugging, excellent Akka integration? • (vs. other reactive-stream impls.) 以上の質問に「はい」と答えた場合は Akka Stream に向いている
  23. 23. How do I pick which “streaming” I need? Kafka serves best as a transport for pub-sub across services. • Note that Kafka Streams (db ops are on the node) 
 is rather, different than the Reactive Kafka client • Great for cross-service communication 
 instead of HTTP Request / Reply Kafka はサービス間の pub-sub 通信に向いている HTTP の代わりにサービス間の通信に使う
  24. 24. How do I pick which “streaming” I need? Spark has vast libraries for ML or join etc ops. • It’s the “hadoop replacement”. • Spark Streaming is windowed-batches • Latency anywhere up from 0.5~1second • Great for cross-service communication 
 instead of HTTP Req/Reply Spark は機械学習系が充実している
  25. 25. Oh yeah, there’s JDK8 “Stream” too! Terrible naming decision IMHO, since Java’s .stream() • Geared for collections • Best for finite and known-up-front data • Lazy, sync/async (async rarely used) • Very (!) hard to extend It’s the opposite what we talk about in Streaming systems! It’s more:“bulk collection operations” Also known as… Scala collections API (i.e. Iterator JDK8 の Stream はイテレータ的なもの
  26. 26. What about JDK9 “Flow”? JDK9 introduces java.util.concurrent.Flow • Is a 1:1 copy of the Reactive Streams interfaces • On purpose, for people to be able to impl. it • Does not provide useful implementations • Is only the inter-op interfaces • Libraries like Akka Streams implement RS,
 and expose useful APIs for you to use. JDK9 の Flow はリアクティブ・ストリーム
  27. 27. And the many meanings it carries. Reactive
  28. 28. And the many meanings it carries. Reactive
  29. 29. The many meanings of Reactive reactivemanifesto.org
  30. 30. The many meanings of Reactive
  31. 31. So what are Reactive Streams actually?
  32. 32. So what are Reactive Streams actually?
  33. 33. So what are Reactive Streams actually?
  34. 34. Getting the complete picture
  35. 35. Getting the complete picture (yet… not the topic of today’s talk)
  36. 36. How to think about these techniques? bit.ly/why-reactive
  37. 37. “Not-quite-Reactive-System” The reason we started researching into transparent to users flow control.
  38. 38. “Best practices are solutions to yesterdays problems.” https://twitter.com/FrankBuytendijk/status/795555578592555008 Circuit breaking as substitute of flow-control
  39. 39. See also, Nitesh Kant, Netflix @ Reactive Summit https://www.youtube.com/watch?v=5FE6xnH5Lak
  40. 40. See also, Nitesh Kant, Netflix @ Reactive Summit https://www.youtube.com/watch?v=5FE6xnH5Lak
  41. 41. HTTP/1.1 503 Service Unavailable HTTP/1.1 503 Service Unavailable Throttling as represented by 503 responses. Client will back-off… but how? What if most of the fleet is throttling?
  42. 42. http://doc.akka.io/docs/akka/2.4/common/circuitbreaker.html HTTP/1.1 503 Service Unavailable HTTP/1.1 503 Service Unavailable
  43. 43. http://doc.akka.io/docs/akka/2.4/common/circuitbreaker.html
  44. 44. See also, Nitesh Kant, Netflix @ Reactive Summit https://www.youtube.com/watch?v=5FE6xnH5Lak “slamming the breaks”
  45. 45. See also, Nitesh Kant, Netflix @ Reactive Summit https://www.youtube.com/watch?v=5FE6xnH5Lak “slamming the breaks”
  46. 46. See also, Nitesh Kant, Netflix @ Reactive Summit https://www.youtube.com/watch?v=5FE6xnH5Lak “slamming the breaks”
  47. 47. See also, Nitesh Kant, Netflix @ Reactive Summit https://www.youtube.com/watch?v=5FE6xnH5Lak “slamming the breaks”
  48. 48. See also, Nitesh Kant, Netflix @ Reactive Summit https://www.youtube.com/watch?v=5FE6xnH5Lak “slamming the breaks”
  49. 49. See also, Nitesh Kant, Netflix @ Reactive Summit https://www.youtube.com/watch?v=5FE6xnH5Lak We’ll re-visit this specific case in a bit :-) “slamming the breaks”
  50. 50. Are absolutely useful! Still… “We can do better than that.” Circuit Breakers
  51. 51. But we’ll need everyone to understand some shared semantics… We can do better.
  52. 52. A fundamental building block. Not end-user API by itself. reactive-streams.org Reactive Streams
  53. 53. Reactive Streams More of an SPI (Service Provider Interface), than API. reactive-streams.org
  54. 54. The specification. Reactive Streams Origins of
  55. 55. What is back-pressure? ?
  56. 56. What is back-pressure?
  57. 57. No no no…! Not THAT Back-pressure! Also known as: flow control. What is back-pressure?
  58. 58. No no no…! Not THAT Back-pressure! Also known as: application level flow control. What is back-pressure?
  59. 59. Reactive Streams - story: 2013’s impls ~2013: Reactive Programming becoming widely adopted on JVM. - Play introduced “Iteratees” - Akka (2009) had Akka-IO (TCP etc.) - Ben starts work on RxJava http://blogs.msdn.com/b/rxteam/archive/2009/11/17/announcing-reactive-extensions-rx-for-net-silverlight.aspx http://infoscience.epfl.ch/record/176887/files/DeprecatingObservers2012.pdf - Ingo Maier, Martin Odersky https://github.com/ReactiveX/RxJava/graphs/contributors https://github.com/reactor/reactor/graphs/contributors https://medium.com/@viktorklang/reactive-streams-1-0-0-interview-faaca2c00bec#.69st3rndy Teams discuss need for back-pressure in simple user API. Play’s Iteratee / Akka’s NACK in IO. }
  60. 60. Reactive Streams - story: 2013’s impls 2014–2015: Reactive Streams Spec & TCK development, and implementations. 1.0 released on April 28th 2015, with 5+ accompanying implementations. 2015 Proposed to be included with JDK9 by Doug Lea via JEP-266 “More Concurrency Updates” http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/6e50b992bef4/src/java.base/share/classes/java/util/concurrent/Flow.java
  61. 61. But what does it do!? Reactive Streams
  62. 62. Publisher[T] Subscriber[T] Back-pressure explained
  63. 63. Fast Publisher Slow Subscriber Push model
  64. 64. Subscriber usually has some kind of buffer. Push model
  65. 65. Push model
  66. 66. Push model
  67. 67. What if the buffer overflows? Push model
  68. 68. Use bounded buffer, drop messages + require re-sending Push model
  69. 69. Kernel does this! Routers do this! (TCP) Use bounded buffer, drop messages + require re-sending Push model
  70. 70. Increase buffer size… Well, while you have memory available! Push model
  71. 71. Push model
  72. 72. Reactive Streams explained Reactive Streams explained in 1 slide
  73. 73. Fast Publisher will send at-most 3 elements. This is pull-based-backpressure. Reactive Streams: “dynamic push/pull”
  74. 74. 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> { } }
  75. 75. 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> { } } Single basic (helper) implementation available in JDK: SubmissionPublisher
  76. 76. Reactive Streams: goals 1) Avoiding unbounded buffering across async boundaries 2)Inter-op interfaces between various libraries
  77. 77. Reactive Streams: goals 1) Avoiding unbounded buffering across async boundaries 2)Inter-op interfaces between various libraries Argh, implementing a correct RS Publisher or Subscriber is so hard!
  78. 78. 1) Avoiding unbounded buffering across async boundaries 2)Inter-op interfaces between various libraries Reactive Streams: goals Argh, implementing a correct RS Publisher or Subscriber is so hard!
  79. 79. Reactive Streams: goals Argh, implementing a correct RS Publisher or Subscriber is so hard! You should be using Akka Streams instead! 1) Avoiding unbounded buffering across async boundaries 2)Inter-op interfaces between various libraries
  80. 80. Already made a huge industry impact Reactive Streams
  81. 81. Back-pressure as a feature
  82. 82. Inspiring other technologies
  83. 83. Inspiring other technologies It’s been a while since Java inspired other modern technologies, hasn’t it?
  84. 84. The implementation. Complete and awesome Java and Scala APIs. As everything since day 1 in Akka. Akka Streams
  85. 85. Akka Streams in 20 seconds: // types: Source[Out, Mat] Flow[In, Out, Mat] Sink[In, Mat] // generally speaking, it's always: val ready = Source.from…(???).via(flow).map(i => i * 2).to(sink) val mat: Mat = ready.run() // the usual example: val f: Future[String] = Source.single(1).map(i => i.toString).runWith(Sink.head) Proper static typing!
  86. 86. Akka Streams in 20 seconds: // types: _ Source[Int, NotUsed] Flow[Int, String, NotUsed] Sink[String, Future[String]] Source.single(1).map(i => i / 0).runWith(Sink.head)
  87. 87. Source.single(1).map(i => i.toString).runWith(Sink.head) // types: _ Source[Int, NotUsed] Flow[Int, String, NotUsed] Sink[String, Future[String]] Akka Streams in 20 seconds:
  88. 88. Your app has specific needs
  89. 89. Your app has specific needs
  90. 90. Your app has specific needs
  91. 91. Your app has specific needs
  92. 92. Your app has specific needs
  93. 93. Ecosystem that solves problems > (is greater than) solving all the problems ourselves
  94. 94. Codename: Alpakka // these are “Alpacasso”
  95. 95. A community for Streams connectors Alpakka – a community for Stream connectors Alp
  96. 96. Alpakka – a community for Stream connectors http://developer.lightbend.com/docs/alpakka/current/
  97. 97. Alpakka – a community for Stream connectors http://developer.lightbend.com/docs/alpakka/current/ Krzysztof Ciesielski Reactive Kafka (Alpakka) maintainer Room B @ 15:30,1日
  98. 98. Alpakka – a community for Stream connectors http://developer.lightbend.com/docs/alpakka/current/
  99. 99. Alpakka – a community for Stream connectors http://developer.lightbend.com/docs/alpakka/current/
  100. 100. Backpressure in action, visualized streams & HTTP
  101. 101. A core feature not obvious to the untrained eye…! Quiz time! TCP is a ______ protocol? Akka Streams & HTTP
  102. 102. A core feature not obvious to the untrained eye…! Quiz time! TCP is a STREAMING protocol! Akka Streams & HTTP
  103. 103. Streaming in Akka HTTP HttpServer as a: Flow[HttpRequest, HttpResponse] http://doc.akka.io/docs/akka/2.4/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778
  104. 104. Streaming in Akka HTTP HttpServer as a: Flow[HttpRequest, HttpResponse] HTTP Entity as a: Source[ByteString, _] http://doc.akka.io/docs/akka/2.4/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778
  105. 105. Streaming in Akka HTTP http://doc.akka.io/docs/akka/2.4/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778 HttpServer as a: Flow[HttpRequest, HttpResponse] TCP as a: Flow[ByteString, ByteString] Websocket connection as a: Flow[ws.Message, ws.Message]
  106. 106. Streaming Deep Learning Predictions Akka [Streams/HTTP/Alpakka] + h2o’s Sparkling Water
  107. 107. Streaming Deep Learning Predictions Akka [Streams/HTTP/Alpakka] + h2o’s Sparkling Water
  108. 108. Streaming Deep Learning Predictions Akka [Streams/HTTP/Alpakka] + h2o’s Sparkling Water DEMO TIME
  109. 109. Streaming from Akka HTTP (Java) wrap(mySource): Source === apply(Source): Source def bytesToMeasurements(bytes: Source[ByteString, Any]): Source[Double, NotUsed] = {
 val measurementLines =
 bytes
 .via(Framing.delimiter(ByteString("n"), maximumFrameLength = 1000))
 .map(_.utf8String)
 
 measurementLines
 .via(CsvSupport.takeColumns(Set("LinAccX (g)", "LinAccY (g)", "LinAccZ (g)")))
 .mapConcat(_.flatMap(col => Try(col.toDouble).toOption))
 .mapMaterializedValue(_ => NotUsed)
 }
  110. 110. Streaming from Akka HTTP (Java) wrap(mySource): Source === apply(Source): Source def bytesToMeasurements(bytes: Source[ByteString, Any]): Source[Double, NotUsed] = {
 val measurementLines =
 bytes
 .via(Framing.delimiter(ByteString("n"), maximumFrameLength = 1000))
 .map(_.utf8String)
 
 measurementLines
 .via(CsvSupport.takeColumns(Set("LinAccX (g)", "LinAccY (g)", "LinAccZ (g)")))
 .mapConcat(_.flatMap(col => Try(col.toDouble).toOption))
 .mapMaterializedValue(_ => NotUsed)
 } via(Flow) === Source.via(Flow) Flow.via(Flow) val bytesToMeasurements: Flow[ByteString, Double, NotUsed] = {
 val measurementLines =
 Flow[ByteString]
 .via(Framing.delimiter(ByteString("n"), maximumFrameLength = 1000))
 .map(_.utf8String)
 
 measurementLines
 .via(CsvSupport.takeColumns(Set("LinAccX (g)", "LinAccY (g)", "LinAccZ (g)")))
 .mapConcat(_.flatMap(col => Try(col.toDouble).toOption))
 .mapMaterializedValue(_ => NotUsed)
 }
  111. 111. Streaming from Akka HTTP (Java) wrap(mySource): Source === apply(Source): Source def bytesToMeasurements(bytes: Source[ByteString, Any]): Source[Double, NotUsed] = {
 val measurementLines =
 bytes
 .via(Framing.delimiter(ByteString("n"), maximumFrameLength = 1000))
 .map(_.utf8String)
 
 measurementLines
 .via(CsvSupport.takeColumns(Set("LinAccX (g)", "LinAccY (g)", "LinAccZ (g)")))
 .mapConcat(_.flatMap(col => Try(col.toDouble).toOption))
 .mapMaterializedValue(_ => NotUsed)
 } via(Flow) === Source.via(Flow) Flow.via(Flow) val bytesToMeasurements: Flow[ByteString, Double, NotUsed] = {
 val measurementLines =
 Flow[ByteString]
 .via(Framing.delimiter(ByteString("n"), maximumFrameLength = 1000))
 .map(_.utf8String)
 
 measurementLines
 .via(CsvSupport.takeColumns(Set("LinAccX (g)", "LinAccY (g)", "LinAccZ (g)")))
 .mapConcat(_.flatMap(col => Try(col.toDouble).toOption))
 .mapMaterializedValue(_ => NotUsed)
 }
  112. 112. Streaming Deep Learning Predictions We have demonstrated: • Superior pluggability and reusability • Fully type-safe APIs • Simple custom Stages • Advanced custom GraphStages
  113. 113. Back-pressure in real systems
  114. 114. Understanding Reactive Systems
  115. 115. Understanding Systems At Lightbend we build for usability, understanding and performance.
  116. 116. Understanding Systems At Lightbend we build for usability, understanding and performance. Remember the car? That’s only: “highest possible performance”.
  117. 117. Understanding Systems We need controllable, introspectable, operable systems. Monitoring, tracing. Right at the core of the tech.
  118. 118. Visualising Architectures
  119. 119. Visualising Architectures https://app.prod.opsclarity.com
  120. 120. Reactive Streams / Akka Streams Is now the time to adopt?
  121. 121. Totally, go for it.
  122. 122. Happy hAkking!
  123. 123. We <3 contributions • Easy to contribute: • https://github.com/akka/akka/issues?q=is%3Aissue+is%3Aopen+label%3Aeasy-to-contribute • https://github.com/akka/akka/issues?q=is%3Aissue+is%3Aopen+label%3A%22nice-to-have+%28low-prio%29%22 • Akka: akka.io && github.com/akka • Reactive Streams: reactive-streams.org • Reactive Socket: reactivesocket.io • Mailing list: • https://groups.google.com/group/akka-user • Public chat rooms: • http://gitter.im/akka/dev developing Akka • http://gitter.im/akka/akka using Akka
  124. 124. What’s next for Akka? • Akka 2.5.x • Distributed Data becomes stable in 2.5 • Persistence Query becomes stable in 2.5 • Alpakka connectors • Akka HTTP/2 • Akka HTTP default backend for Play (2.6.0-M1 sic!) • Akka Typed (sic!) • Better AbstractActor for Java8 users • Possible multi-data centre extensions …? • …?
  125. 125. Find the Akka Ecosystem <3 Scaladex https://index.scala-lang.org
  126. 126. Free e-book and printed report. bit.ly/why-reactive Covers what reactive actually is. Implementing in existing architectures. Thoughts from the team that’s building reactive apps since more than 6 years. Obligatory “read my book!” slide :-)
  127. 127. ktoso @ lightbend.com twitter: ktosopl github: ktoso team blog: blog.akka.io home: akka.io myself: kto.so Q/A

×