Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Streams 入門 #jjug

Reactive Streams 入門 #jjug

2015/06/24 JJUG ナイトセミナー 「Reactive Streams特集」 by @okapies
https://jjug.doorkeeper.jp/events/26547

補足記事: http://okapies.hateblo.jp/entry/2015/06/26/024505

Ef45a84b10aa3cb5acd2ea0c9846bd3b?s=128

Yuta Okamoto

June 24, 2015
Tweet

Transcript

  1. JJUG φΠτηϛφʔ: Reactive Streams ೖ໳ Yuta Okamoto (@okapies) 2015/06/24

  2. Who are you? • Ԭຊ ༤ଠ (@okapies) • ւ֎ͷϚΠφʔͳ Scala

    OSS ͷൃ۷͕झຯ • ʢγΣϧεΫϦϓτ৬ਓʣ • Scala Matsuri 2016 ४උҕһʢ຋༁νʔϜʣ
  3. Scala Matsuri 2016 ! ! • དྷ೥1݄30ʙ31೔ʹ։࠵ܾఆʂ http://scalamatsuri.org/ • ४උҕһձͷελοϑืूதʂ

    • ࣍ճఆྫ͸ 7/11ʢ౔ʣϦϞʔτࢀՃ࿮༗
  4. ࡞ͬͨ΋ͷ • finagle-kafka (https://github.com/okapies/ finagle-kafka) • sircuit (https://github.com/okapies/sircuit) • rx-process

    (https://github.com/okapies/rx- process)
  5. ॻ͍ͨ΋ͷ • ຋༁: • Reactive Manifesto v2.0 (http://www.reactivemanifesto.org/ja) • Effective

    Scala (http://twitter.github.io/effectivescala/index-ja.html) • ໋ྩܕͷίʔϧόοΫɺؔ਺ܕͷϓϩϛε (https://gist.github.com/okapies/5354929) • ϒϩάهࣄ (http://okapies.hateblo.jp/): • ඇಉظετϦʔϜॲཧͷඪ४ԽΛ໨ࢦ͢ “Reactive Streams” ͱ͸ • ʮؔ਺ܕϓϩάϥϚͷͨΊͷ Rx ೖ໳ʯγϦʔζ • ϚΠΫϩαʔϏε͕ Scala ΛબͿ̏ͭͷཧ༝ ࠓ೔ͷ͓୊ʂ
  6. Reactive
 Streams https://www.flickr.com/photos/vinothchandar/8160520014

  7. http://www.reactive-streams.org/announce-1.0.0 (2015/05/08)

  8. Reactive Streams ͱ͸ʁ • ”Back-Pressure ෇͖ͷඇಉظετϦʔϜॲཧ“ͷඪ ४ΛఆΊΔ࢓༷ • ඇಉظετϦʔϜॲཧʹ͖ͭ·ͱ͏ϑϩʔ੍ޚ ͷ՝୊ʢόοϑΝᷓΕ໰୊ʣͷղܾࡦΛఏࣔ

    • ࢓༷ʹ४ڌͨ͠ϥϯλΠϜؒͷ૬ޓ઀ଓੑΛอ ূ͢Δ • ࠷ऴ໨ඪ͸ Java 9 Ͱͷඪ४Խ ΤϯυϢʔβ޲͚ͷ
 API ͸είʔϓ֎
 ʢ࣮૷ຖʹ༻ҙ͢Δʣ
  9. ओͳࢀՃϕϯμʔ • (Akka) • (RxJava) • (Reactor) • (Vert.x) (Scala

    ݴޠͷ։ൃݩ) (ถࠃ࠷େखͷ
 ಈը഑৴αʔϏε)
  10. ओͳࢀՃऀ • Doug Lea • JSR 166 (java.util.concurrent)
 ͷ Specification

    Lead • “Java Concurrency in Practice”
 ͷஶऀͷҰਓ • ඪ४Խ͸ࢯͷओಋͰਐΊΒΕ͍ͯΔ http://www.amazon.co.jp/dp/4797337206/
  11. ܦҢ • Reactive Manifesto ͷൃද(2013೥9݄) • Typesafe ͷओಋͰɺඇಉظετϦʔϜٕज़Λ ։ൃ͢Δϕϯμʔ֤ࣾͷؒͰɺϑϩʔ੍ޚͷٕ ज़՝୊ͷղܾࡦΛ࿩͠߹͏ίϛϡχςΟ͕ൃ଍

    • GitHub ʹ reactive-streams άϧʔϓΛ࡞੒ɻ ਖ਼ࣜʹڞಉͰ࢓༷ࡦఆΛ࢝ΊΔ(2014೥ॳ಄)
  12. Reactive? https://www.flickr.com/photos/livenature/204420128

  13. Reactive ̋̋ 
 ϓϩάϥϛϯάϞσϧ ! ϥϯλΠϜ ! ΞʔΩςΫνϟ ʹ ×

    ×
  14. ࠓ೔ͷΩʔϫʔυ Reactive Manifesto Reactive Programming Reactive Streams ϓϩάϥϛϯάϞσϧ: ϥϯλΠϜ: ΞʔΩςΫνϟ:

  15. ࠓ೔ͷΩʔϫʔυ Reactive Manifesto Reactive Programming Reactive Streams ϓϩάϥϛϯάϞσϧ: ϥϯλΠϜ ΞʔΩςΫνϟ

  16. Reactive Programming https://www.flickr.com/photos/kovah/15331505770

  17. Ͳ͜Ͱ࢖ΘΕ͍ͯΔ͔ • GUIɾϑϩϯτΤϯυͷ࣮૷ • (ϚΠΫϩ)αʔϏεͷ࣮૷ • Ϗοάσʔλॲཧ • etc… •

    Πϕϯτۦಈ • ඇಉظϓϩάϥϛϯά • ฒߦॲཧ ͕ඞཁͳͱ͜Ζશൠ
  18. ʊਓਓਓਓਓਓਓਓਓਓਓਓਓਓʊ ʼ σʔλϑϩʔʴมߋͷ఻೻ ʻ ʉY^Y^Y^Y^Y^Y^Y^Y^Y^Y^Yʉ http://en.wikipedia.org/wiki/Reactive_programming ※ ఆٛͷҰྫͰ͢

  19. ී௨ͷ(खଓ͖ܕͷ)ίʔυ A = 1; B = 2; C = (A+1)

    + (B-1)*2; ※”Ͳ͏΍࣮ͬͯߦ͢Δ͔”͸ࣗ໌ ʢʹ ࣮ߦϞσϧʣ
  20. σʔλϑϩʔ g1 g2 h Input Input Output f ؔ਺ ೖྗ

    ग़ྗ ؔ਺ͱɺؔ਺ಉ࢜Λܨ͗߹Θͤͨάϥϑ
  21. -1 ×2 + +1 A B C σʔλϑϩʔͰදͯ͠ΈΔ A =

    1; B = 2; C = (A+1) + (B-1)*2; 1 2 4 1 2 2 ※Ͳ͏࣮ߦ͢Δ͔͸๨ΕΑ͏ ʢม਺ͱԋࢉࢠͷؔ܎͚ͩݟΔʣ
  22. -1 ×2 + +1 A B C ม਺Λߋ৽: खଓ͖ܕͷ৔߹ A

    = 1; B = 2; C = (A+1) + (B-1)*2;
 A = 2; 1 $ 2 2 4 C ʹ͸
 ൓ө͞Εͳ͍ खଓ͖ܕͷ࣮ߦϞσϧ × × × × × ×
  23. -1 ×2 + +1 A B C Reactive ͳมߋͷ఻೻ A

    := 1; B := 2;
 C := (A+1) + (B-1)*2;
 A := 2; 1 $ 2 4 $ 5 2 1 $ 3 1 2 A ͷมߋ͕
 C ʹ൓ө͞ΕΔ Reactive ͳ࣮ߦϞσϧ
  24. -1 ×2 + +1 A B C Reactive ͳมߋͷ఻೻ 2

    5 $ 7 2 $ 3 1 $ 2 2 $ 4 3 C := (A+1) + (B-1)*2;
 A := 2; B := 3; B ͷมߋ͕
 C ʹ൓ө͞ΕΔ Reactive ͳ࣮ߦϞσϧ
  25. -1 ×2 + +1 A B C Reactive ͳมߋͷ఻೻ 2

    $ 0 7 $ 5 3 2 4 3 $ 1 A := 2;
 B := 3;
 A := 0; A ͷมߋ͕
 C ʹ൓ө͞ΕΔ Reactive ͳ࣮ߦϞσϧ
  26. Reactive Programming ͷ࣮ݱखஈ 1. ”σʔλϑϩʔ”Λ
 هड़͢ΔͨΊͷϓϩάϥϛϯάϞσϧ 2. ”มߋͷ఻೻”Λ
 ϓϩάϥϚʹ୅Θ࣮ͬͯߦ͢ΔϥϯλΠϜ

  27. ϓϩάϥϚ͔ΒݟΔͱ: ؔ਺ ೖྗ ग़ྗ 1: ؔ਺ 2: σʔλϑϩʔ
 ʢґଘؔ܎ͷάϥϑʣ ͷೋͭΛ࢖ͬͯϓϩάϥϜΛهड़͢Δɻ

    1 2 A B C f g1 g2 +
  28. ྫ (RxJava): Observable<Integer> a = …; Observable<Integer> b = …;

    ! Observable<Integer> a1 = a.map( (i) -> i + 1 ); Observable<Integer> b1 = b.map( (j) -> j - 1 ) .map( (k) -> k * 2 ); ! ! Observable<Integer> c = Observable.combineLatest(a1, b1, (i, j) -> i + j ); A B C +1 —1 ×2 +
  29. ྫ (RxJava): Observable<Integer> a = …; Observable<Integer> b = …;

    ! Observable<Integer> a1 = a.map( (i) -> i + 1 ); Observable<Integer> b1 = b.map( (j) -> j - 1 ) .map( (k) -> k * 2 ); ! ! Observable<Integer> c = Observable.combineLatest(a1, b1, (i, j) -> i + j ); σʔλϑϩʔΛܨ͛Δϝιου ؔ਺ ೖྗ A B C +1 —1 ×2 +
  30. “What” ͱ “How” ͷ෼཭ ϓϩάϥϚ͸”ೖྗΛԿʹม׵͢Δ͔“͚ͩʹ
 ஫໨ͯ͠ϩδοΫΛॻ͘ɻ ”Ͳ͏΍࣮ͬͯߦ͢Δ͔”͸ϥϯλΠϜͷ໾ׂɻ ʢ࣮ߦϞσϧʣ G ?

    ? ? ? ? f g1 + g2
  31. Reactive ͳϞσϧͷϝϦοτ • มߋ͕ࣗಈతʹ൓ө͞ΕΔ • ඇಉظԽɾฒྻԽ͠΍͍͢ • ૊Έ߹ΘͤՄೳ (composable) ʹ͠΍͍͢

    } ϥϯλΠϜʹ೚ͤΔ (ੑೳ࠷దԽ΋༰қ) ؔ਺ ೖྗ ग़ྗ ֤ؔ਺͸ೖྗͷΈʹ൓Ԡ͢Δ → ֎෦ͷঢ়ଶʹӨڹ͞Εͳ͍ → ૄ݁߹ʹͳΔʂ
  32. ༨ஊ: ΤΫηϧᄻ͑࿩ͱͷൺֱ • Reactive Programming ͸“ΤΫηϧ”Ͱᄻ͑Δ ΑΓσʔλϑϩʔϞσϧͰઆ໌͢Δํ͕ྑ͍ • composable ͳؔ਺ͷඞཁੑ͕෼͔Γқ͍

    • ೖྗͱग़ྗͷґଘੑΛએݴͯ͠ϓϩάϥϛ ϯά͢Δ΍Γํ͕ຊ࣭Ͱ͋Δ͜ͱΛࣔͤΔ
  33. ͍Ζ͍Ζͳ
 Reactive Programming https://www.flickr.com/photos/deanhochman/12469727533

  34. RP ͷϓϩάϥϛϯάϞσϧ • ໋ྩܕ Reactive Programming • ΦϒδΣΫτࢦ޲ Reactive Programming

    (OORP) • ؔ਺ܕ Reactive Programming (FRP) • ͦͷଞʢFuture/PromiseɺActorʣ
  35. RP ͷϓϩάϥϛϯάϞσϧ • ໋ྩܕ Reactive Programming • ΦϒδΣΫτࢦ޲ Reactive Programming

    (OORP) • ؔ਺ܕ Reactive Programming (FRP) • ͦͷଞʢFuture/PromiseɺActorʣ ୯ʹ “RP” ͱݴ͏ͱ ͜Ε͕ଟ͍…ʁ {
  36. ؔ਺ܕ Reactive Programming • ਓʹΑͬͯࢦ͍ͯ͠Δ΋ͷ͕ҧ͏… ( ´-` ) • ຊདྷͷʢڱٛͷʣFRP

    • ΧδϡΞϧͳʢ޿ٛͷʣFRP
  37. ຊདྷͷ FRP • 1997 ೥ʹ Conal Elliott (@conal) Β͕ఏএ •

    FRP ͷຊՈ͸ͬͪ͜Ͱɺݫີͳఆ͕ٛ͋Δ Time A Behavior<A> ≈
 Function1[Time, A] Event<A> ≈ List<Pair<Time, A>> ࿈ଓతͳ ʮ࣌ؒతʹมԽ͢ΔৼΔ෣͍ʯ (t, a) ཭ࢄతͳ
 ʮΠϕϯτετϦʔϜʯ A Time +
  38. ΧδϡΞϧͳ FRP • ΠϕϯτετϦʔϜΛؔ਺ܕϓϩάϥϛϯάΛ׆༻͠
 ͏·͘ѻ͏ɺ͘Β͍ͷχϡΞϯε • ੈͷதͰࠓɺΑ͘ݴٴ͞ΕͯΔ ”FRP” ͸େମͬͪ͜ (Rx,

    Bacon.js, Elm, …) Time A Behavior<A> ≈
 Function1[Time, A] Event<A> ≈ List<Pair<Time, A>> ࿈ଓతͳ ʮ࣌ؒతʹมԽ͢ΔৼΔ෣͍ʯ (t, a) ཭ࢄతͳ
 ʮΠϕϯτετϦʔϜʯ A Time +
  39. ͳͥ FRP ͳͷ͔ʁ • σʔλϑϩʔΛએݴతʹॻ͚Δ • ෭࡞༻Λ੍ݶ͢Δख๏ಉ࢜Ͱ૬ੑ͕ྑ͍ • ߴ֊ؔ਺ (map,

    fold, filter, …) ͷ׆༻ • ڱٛ΋޿ٛ΋ composability ΛߴΊΔखஈͱ ͯؔ͠਺ܕͷख๏Λ࢖͍ͬͯΔ఺͸ಉ͡
  40. Future/Promise • RP ͷ஥ؒʹೖΕΔ͜ͱ͕͋Δ • σʔλϑϩʔ͸͋Δ͕มߋͷ఻೻͸ͳ͍ • ྺ࢙తʹ͸ 1970 ೥୅·ͰḪΔ͜ͱ͕Ͱ͖ɺRP

    ΍ Actor Ϟσϧͱ΋ਂ͍ؔ܎͕͋Δ • JavaScript ͳͲͷϑϩϯτΤϯυͷ΄͔ɺϚΠΫ ϩαʔϏεͷ࣮૷खஈͱͯ͠΋޿͘࢖ΘΕ͍ͯΔ ʢTwitter Finagle ΍ Node.js ͳͲʣ
  41. Actor Ϟσϧ • RP ͷ஥ؒʹೖΕͨΓೖΕͳ͔ͬͨΓ͢Δ • ϓϩάϥϛϯάϞσϧతʹ͸ composablity ͕௿ ͍ͷ͕೉఺

    • ۙ೥͸ϥϯλΠϜͱͯ͠ͷଆ໘͕஫໨͞Ε͍ͯΔ • Erlang ͕ఏএ͢Δ ”Let It Crash" ఩ֶʹجͮ͘଱ো ֐ੑɺෛՙ؅ཧɺεέʔϥϏϦςΟ • Actor ϥϯλΠϜͷ্ʹ RP ͷϓϩάϥϛϯάϞσ ϧΛࡌͤͨͷ͕ Akka Streamsʢޙड़ʣ
  42. Ϣʔεέʔεͱ࣮ྫ https://www.flickr.com/photos/sdstrowes/8248580877

  43. Ϣʔεέʔε • GUIɾϑϩϯτΤϯυ • ϚΠΫϩαʔϏε • Ϗοάσʔλॲཧ

  44. GUIɾϑϩϯτΤϯυ ྫ: RxJS Ϛ΢εΫϦοΫͷετϦʔϜ͔Β
 μϒϧΫϦοΫ͚ͩΛநग़͢Δ https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 ΫϦοΫΛ 250ms
 ͝ͱʹ·ͱΊͯ ͦΕͧΕΛ


    ௕͞ʹม׵͢Δ
  45. GUIɾϑϩϯτΤϯυ https://github.com/tehmou/rx-android-architecture RxJava + Android
 ͷσϞΞϓϦ ςΩετϘοΫε͕ߋ৽͞Ε
 Δ౓ʹ JSON API

    Ͱݕࡧɻ ໭͖ͬͯͨ݁ՌΛඇಉظʹ Widget ʹ൓ө
  46. ϚΠΫϩαʔϏε val userAndTweets = Future.join( userService.findByUserId(userId), tweetService.findByUserId(userId) ) find find

    userId userAndTweets User
 Service Tweet
 Service http://www.slideshare.net/knoldus/finagle-by-twitter-engineer/16 join ଞͷϚΠΫϩαʔϏε (UserService, TweetService) ʹΫΤϦΛඇಉظʹ౤͛ ͯɺ݁Ռ͕ೋͭͱ΋ฦ͖ͬͯͨ࣌఺Ͱ
 ଋͶͯग़ྗ ྫ: Twitter Finagle
  47. Ϗοάσʔλॲཧ https://speakerdeck.com/googlecloudjapan/google-cloud-dataflowwoli-jie-suru ྫ: Google Cloud Dataflow PCollection Λม׵͢Δσʔλϑϩʔͱͯ͠هड़

  48. Ϗοάσʔλॲཧ https://speakerdeck.com/googlecloudjapan/google-cloud-dataflowwoli-jie-suru Google Ϋϥ΢υ͕
 ϥϯλΠϜʹ͋ͨΔ σʔλϑϩʔఆٛ

  49. ͓·͚: Spark 1.4 ͷ
 DAG Visualization ػೳ https://cloud.githubusercontent.com/assets/2133137/7625997/e0878f8c-f9b4-11e4-8df3-7dd611b13c87.png

  50. RP ͷීٴ͕΋ͨΒͨ͠΋ͷ • ϓϩάϥϛϯάϞσϧͷ੔උ͕ਐΈɺඇಉظ ॲཧ΍ฒߦॲཧͷෑډ͕େ෯ʹԼ͕ͬͨ • ϞόΠϧ͔Βେن໛෼ࢄ؀ڥ·ͰɺϥϯλΠ Ϝͷબ୒ࢶ΋ͲΜͲΜ૿͍͑ͯΔ • UI

    ϓϩάϥϛϯάͷมֵ΍ɺϚΠΫϩαʔϏ εͷ୆಄ͳͲΛޙԡ͠
  51. ࠓ೔ͷΩʔϫʔυ Reactive Manifesto Reactive Programming Reactive Streams ϓϩάϥϛϯάϞσϧ ϥϯλΠϜ ΞʔΩςΫνϟ:

  52. The Reactive Manifest http://www.reactivemanifesto.org/ja

  53. Reactive Manifesto ͱ͸ • ”Reactive Systems” ΁ͷࢧ࣋Λݺͼ͔͚Δจॻ • Reactive ͳγεςϜ͕උ͑Δཁ݅ʹ͍ͭͯड़΂͍ͯΔ

    • Typesafe ࣾ (Scala ݴޠͷ։ൃݩ) ͷϝϯόʔ͕ओಋ • ͦͷଞʹ Dave Farley (”ܧଓతσϦόϦʔ”ͷஶऀ) ΍ Martin Thompson (ݩ LMAX CTO) ͳͲ • ϚʔέςΟϯάจॻͱͯ͠ͷ৭࠼͕ೱ͍ʢํ޲ੑͱͯ͠ ͸ “The Twelve-Factor App” ʹ͍͔ۙ΋ʣ
  54. γεςϜ΁ͷཁٻͷมԽ ਺೥લ ݱࡏ αʔό਺ ʙ਺ेϊʔυ ਺ઍҎ্ʙ ཁٻԠ౴࣌ؒ ʙ਺ඵ ʙϛϦඵ ཁٻՔಇ཰

    ਺࣌ؒͷΦϑϥΠϯ
 ϝϯςφϯεΛڐ༰ 100% σʔλྔ ΪΨόΠτ୯Ґ ϖλόΠτ୯Ґ
  55. γεςϜͷ࡞ΓํͷมԽ “େن໛γεςϜΛߏங͢Δ૊৫͸
 ͜ͷมԽʹରॲ͢ΔઃܭݪଇΛ
 ͢Ͱʹൃݟ͍ͯ͠Δ” ! ! ! “ͦͷΑ͏ͳಛ௃Λඋ͑ΔγεςϜΛ Reactive Systems

    ͱݺ΅͏”
  56. Reactive Systems ͷಛ௃ http://www.reactivemanifesto.org/ja

  57. Reactive Systems ͷಛ௃ http://www.reactivemanifesto.org/ja Ϣʔβ΁ਝ଎͔ͭ
 Ұ؏ͨ͠଎౓ͰԠ౴͢Δ ো֐࣌ʹ΋
 ଈԠੑΛҡ࣋͢Δ ඇಉظϝοηʔδύογϯά͕
 શͯͷج൫Ͱ͋Δ

    ෛՙ͕มಈͯ͠΋
 ଈԠੑΛҡ࣋͢Δ
  58. …۩ମతʹ͸ʁ • ۩ମྫ͸ಛʹڍ͛ΒΕ͍ͯͳ͍ • Actor Ϟσϧ ʹجͮ͘ΞʔΩςΫνϟΛڧ ͘૝ఆ͍ͯ͠Δ • ҰํͰϚΠΫϩαʔϏεͱ΋਌࿨ੑ͕ߴͦ

    ͏ʹݟ͑Δ
  59. Dave Farley ᐌ͘: ࢲ͕࢓ࣄͰؔΘͬͨɺେ͖ͳγεςϜͷΞʔΩςΫνϟʹ͸ྨ ࣅ఺͕͋ͬͨɻͦ͏ͨ͠γεςϜͰ͸ɺ໰୊ྖҬʹ͓͚Δ໌֬ ͳ Bounded Context Λ࣮૷ͨ͠αʔϏε͕ૄ݁߹Ͱ݁ͼ͍ͭ ͓ͯΓɺޓ͍ͷ௨৴͸ඇಉظϝοηʔδͷΈͰߦ͏ɻ͜ΕΒ

    ͸ɺRDB ͷ্ʹߏங͞Εͨശग़͠ͷඪ४తͳࡾ૚ΞʔΩςΫ νϟͷγεςϜͷ΍Γํͱ͸ࣅͯ΋ࣅ͔ͭͳ͍ɻ ιϑτ΢ΣΞΛ࣮ߦ͢Δϋʔυ΢ΣΞ؀ڥ͕มΘΓͭͭ͋Δɻ RAM ͱσΟεΫͷՁ͕֨ࠩॖ·ΓɺRAM ༰ྔ͸ܶతʹ૿Ճɻ ࠓ΍෼ࢄϓϩάϥϛϯά͸ඪ४ͰɺωοτϫʔΫ͸σΟεΫ ΑΓ଎͍ɻେ༰ྔͷඇشൃੑ RAM ͷ࣮༻Խ͸ؒۙͩɻ͜Ε͸ ͭ·Γɺʮඪ४తΞϓϩʔνʯΛࢧ࣋͢ΔԾઆ͕มԽͨ͠ͱ ͍͏͜ͱͩɻ “The Reactive Manifesto | Dave Farley's Weblog” ΑΓົ༁
  60. ਤʹ͢Δͱ: ϓϨθϯςʔγϣϯ ϏδωεϩδοΫ RDB ಉظݺग़ ಉظݺग़ RDB ඇಉظ ඇಉظ ඇಉظ

    ֎෦
 αʔϏε ඇಉظ ϚΠΫϩαʔϏε
 ͬΆ͘ݟ͑Δ
  61. STORAGE & RETRIEVAL LOGIC PRESENTATION ROUTING Redis Memcache Flock T-Bird

    MySQL Tweet User Timeline Social Graph DMs API Web Monorail TFE HTTP Thrift “Stuff” http://monkey.org/~marius/scala2015.pdf ࣮ࡍͷϚΠΫϩαʔϏε
 (Twitter)
  62. ϚΠΫϩαʔϏε ⊂ RSʁ • ϚΠΫϩαʔϏε͸ Reactive Systems ͷ۩ମ ྫͷҰͭͩͱߟ͑ͯΑͦ͞͏ •

    ஶऀୡ΋ΠϯλϏϡʔ౳ͰϚΠΫϩαʔϏε ͱͷؔ܎ʹݴٴ͍ͯ͠Δ[1][2] [2]: http://www.infoq.com/jp/news/2014/11/thompson-reactive-manifesto-2 [1]: http://www.infoq.com/articles/reactive-programming-anti-patterns
  63. vs. Reactive Programming • Reactive Manifesto ͸ Reactive Programming ͔Β༻ޠΛआΓ͖ͯͨผ෺ͱߟ͑Δͷ͕ྑ͍

    • Reactive ͷʮ֎෦͔Βͷܹࢗʹ൓Ԡ͢Δʯͱ ͍͏ҙຯ߹͍Λڧௐ͍ͯ͠Δ
 ʢΠϕϯτɺෛՙɺো֐ɺϢʔβɺ…ʣ
  64. vs. Reactive Programming • ͱ͸͍͑…ϝοηʔδۦಈʹΑΔඇಉظϓϩ άϥϛϯά͕େલఏʹͳΔͷͰɺ࣮૷खஈͷ Ұͭͱͯ͠ Reactive Programming ͸༗ྗ

    Reactive Manifesto Reactive Programming Callback- based … ??? ϓϩάϥϛϯά Ϟσϧ ϥϯλΠϜ ΞʔΩςΫνϟ Actor Model
  65. Programming × Systems • Reactive Manifesto ͷ໨ࢦ͢ੈք: • େن໛σʔλΛѻ͑ΔεέʔϥϒϧͰՄ༻ ੑͷߴ͍෼ࢄγεςϜ

    • RM ͷจ຺Ͱ RP Λ׆༻͢Δʹ͸ɺେن໛ॲཧ ʹରͯ͠ responsive ͳϥϯλΠϜ͕ඞཁ
  66. ࠓ೔ͷΩʔϫʔυ Reactive Manifesto Reactive Programming Reactive Streams ϓϩάϥϛϯάϞσϧ: ϥϯλΠϜ: ΞʔΩςΫνϟ:

  67. Reactive Streams https://www.flickr.com/photos/jeremybrooks/3546105970

  68. Ϣʔεέʔε • ԻָɾϏσΦͷετϦʔϛϯά • ωοτϫʔΫΛհͨ͠όϧΫσʔλͷసૹ • αʔό㱻ϞόΠϧΫϥΠΞϯτؒͷ௨৴ • IoT (Internet

    of Things) • ϦΞϧλΠϜσʔλղੳॲཧ • etc…
  69. Reactive Streams ͷਖ਼ମ ඇಉظετϦʔϜॲཧ ! Non-blocking back pressure + ↑͕͜͜ϙΠϯτʂ

  70. Reactive Streams ͷਖ਼ମ 2: https://www.flickr.com/photos/jeremybrooks/3546105970 1: https://www.flickr.com/photos/vinothchandar/8160520014 ૊Έࠐ·Εͨ
 ϑϩʔ੍ޚ ͩͩྲྀ͠

    [·͕͍ͪ] [͍͔͍ͤ]
  71. ͩͩྲྀͩ͠ͱԿ͕Ϛζ͍ͷ͔ https://www.flickr.com/photos/chiaralily/4978843623/ εύΠΫͨ͠σʔλ զʑͷγεςϜ

  72. σʔλϑϩʔͷܾյ • Publisher ͸ Subscriber ΁ϝοηʔδΛૹΔ Publisher Subscriber … 3

    msgs/sec 3 msgs/sec ϝοηʔδόοϑΝ ॲཧೳྗ ૹ৴ೳྗ =
  73. σʔλϑϩʔͷܾյ • Subscriber ͕଎͍ͱ͖͸ՋʹͳΔ
 → ܭࢉػࢿݯΛ༗ޮ׆༻Ͱ͖ͳ͍ … < Publisher Subscriber

    3 msgs/sec དྷͳ͍… ͋Μ·ૹΔͱ
 ѱ͍͔ͳ… 1 msg/sec
  74. σʔλϑϩʔͷܾյ • Publisher ͕଎͗͢ΔͱόοϑΝ͕ᷓΕͯࢮ͵ … > Publisher Subscriber 3 msgs/sec

    100 msgs/sec ΍Ίͯʂ Զʹ
 ·͔ͤΖʔʂ
  75. Back-Pressure • Publisher ͸ϦΫΤετ͞Εͨ෼͚ͩϝοηʔ δΛૹΔ → ҆શ … 100 msgs/sec

    [Request(3)] Publisher Subscriber 3 msgs/sec ࣍ɺ3 ݸ͍ͩ͘͞ ͍͋Αʔ
  76. Back-Pressure • Back-Pressure ͸఻೻͢Δ • C ͕஗͘ͳͬͨΒ B, A ΋εϩʔμ΢ϯ͢Δ

    B C … ࠓ͸͍͍Ͱ͢ A … ࠓ͸͍͍Ͱ͢ ͍͋Αʔ
  77. SPI ͷΠϯλϑΣʔε public interface Publisher<T> { public void subscribe(Subscriber<? super

    T> s); } public interface Subscriber<T> { public void onSubscribe(Subscription s); public void onNext(T t); public void onError(Throwable t); public void onComplete(); } public interface Subscription { public void request(long n); public void cancel(); } ͢΂ͯ໭Γ஋ͳ͠ʹඇಉظ ޠኮ͸ RxJava ͕ϕʔε
  78. ࢓༷ॻͱTCK • ϥϯλΠϜؒͷ૬ޓ઀ଓੑΛอূ͢Δ

  79. ओͳ࣮૷ • Akka Streams (Typesafe) • Ratpack • Reactor (Spring)

    • RxJava (Netflix) • Vert.x 3.0 (Red Hat)
  80. DB ΞΫηεϥΠϒϥϦͷ࣮૷ • Slick 3.0 a.k.a “Reactive Slick” (JDBC) •

    ReactiveMongo • Reactive Rabbit (AMQP) • Reactive Kafka • etc…
  81. ͳͥ Reactive ͳ DB Ξμϓλ͕ඞཁʁ • ྫ͑͹ɺJDBC ͸ಉظ API ͳͷͰɺඇಉظͳ

    ϑϨʔϜϫʔΫΛ࢖͍ͬͯͯ΋ worker thread ΁ͷॲཧͷҕৡΛࣗ෼Ͱॻ͘ඞཁ͕͋ͬͨ • context switch ͷίετɺεϨου਺ʹԠ͡ ͨϝϞϦফඅɺεϨουؒ௨৴ͷϩοΫڝ߹ • ϫʔΧεϨου਺ͱɺDB ίωΫγϣϯϓʔϧ ͷαΠζΛ”ਖ਼͘͠”ઃఆ͢Δඞཁ͕͋Δ
  82. ͢΂͕ͯ Reactive ʹͳΔ • Reactive ͳ DB Ξμϓλ͕ग़ἧͬͨ͜ͱͰɺ ೖޱ͔Βग़ޱ·Ͱ Reactive

    Streams ४ڌͷ෦ ඼Ͱ૊Έ্͛ΒΕΔΑ͏ʹͳͬͨ AMQP Rx
 Java Akka Vert.x RDB ྫ:
  83. Reactive Streams ͷࠓޙ • Java 9 Ͱͷඪ४Խ (j.u.c.Flow) • ೿ੜϓϩδΣΫτ

    • JavaScritp Interface (reactive-streams-js) • Network Protocol (reactive-streams-io) • Reactive IPC (http://reactive-ipc.io)
  84. Akka
 Streams

  85. Akka Streams ͱ͸ • Akka Λϕʔεʹͨ͠ Reactive Streams ࣮૷ͷ ͻͱͭ

    • σʔλϑϩʔΛهड़͢Δܕ҆શͳ DSL Λఏڙ ͢ΔʢJava ˍ Scalaʣ • কདྷతʹ͸ Play Framework ͷج൫ʹͳΔ༧ఆ
  86. جຊతͳߟ͑ํ • σʔλϑϩʔ
 
 
 
 Λ Actor ౳Λ࢖ͬͯ۩ݱԽ (materialize)

    ͢Δ • ૊Έ্͛ͨ FlowGraph ࣗମ͸ઃܭਤ (blueprint) ͳͷͰɺ৭ʑͳ Source ΍ Sink Ͱ࢖͍ճͤΔ Source Flow Sink
  87. ίʔυྫ (Scala) implicit val system = ActorSystem() implicit val mat

    = ActorFlowMaterializer() ! val source = Source[Int](1 to 5) val sink = Sink.foreach[Int](println) ! source.map(_ * 2).runWith(sink) Source ×2 Sink Actor ͷ४උ Materializer Λ࢖࣮ͬͯߦ
  88. FlowGraph DSL val g = FlowGraph { implicit builder =>

    import akka.stream.scaladsl.FlowGraphImplicits._ ! source ~> f1 ~> bcast ~> f2 ~> merge ~> f3 ~> sink bcast ~> f4 ~> merge } Source f2 Sink f4 f1 Broadcast f3 Merge
  89. Materializer ͷਐԽ • ݱࡏͷ Materializer ͸ Akka Actor ༻ͷΈ •

    FlowGraph Λ࣮ߦϓϥϯ (execution plan) ͱΈͳ ͢ͱɺ৭ʑͳ͜ͱΛ࣮ݱ͢Δ༨஍͕͋Γͦ͏ • άϥϑͷ࠷దԽ΍ݕূɺ݁ՌͷΩϟογϡ • akka-cluster ͱ૊Έ߹Θͤͯ෼ࢄΫϥελରԠ • ཧ۶্͸ Spark ରԠ΋Մೳ…ʂʁ
  90. ·ͱΊ

  91. ·ͱΊ • ΋͸΍ฒߦॲཧ΍ඇಉظϓϩάϥϛϯάΛڪ ΕΔඞཁ͸ͳ͍ • RP ͷීٴͱ͍͏ܗͰಓ۩͸੔͍ͭͭ͋Δ • ༷ʑͳϢʔεέʔεͰ࣮ࡍʹ׆༻͞Εͯ੒Ռ Λڍ͍͛ͯΔ

  92. ·ͱΊ • য఺͸͢ͰʹϥϯλΠϜ΍γεςϜΞʔΩςΫ νϟ΁ҠΓͭͭ͋ΔͷͰ͸ͳ͍͔ • Reactive Streams ͕࡞Γग़͢ΤίγεςϜ
 → ैདྷܕ

    Web ΞʔΩςΫνϟ΁ͷ௅ઓ • ϥϯλΠϜͷੑೳɺ଱ো֐ੑɺӡ༻ੑ͕બ ఆج४ͱͯ͠ΑΓॏཁʹ ҰํͰʮԿͰॻ͔͘ʯ͸
 ͲΜͲΜऔΓସ͑ՄೳʹͳΔʁ
  93. Thank you!