Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Who are you? • Ԭຊ ༤ଠ (@okapies) • ւ֎ͷϚΠφʔͳ Scala OSS ͷൃ۷͕झຯ • ʢγΣϧεΫϦϓτ৬ਓʣ • Scala Matsuri 2016 ४උҕһʢ຋༁νʔϜʣ

Slide 3

Slide 3 text

Scala Matsuri 2016 ! ! • དྷ೥1݄30ʙ31೔ʹ։࠵ܾఆʂ http://scalamatsuri.org/ • ४උҕһձͷελοϑืूதʂ • ࣍ճఆྫ͸ 7/11ʢ౔ʣϦϞʔτࢀՃ࿮༗

Slide 4

Slide 4 text

࡞ͬͨ΋ͷ • finagle-kafka (https://github.com/okapies/ finagle-kafka) • sircuit (https://github.com/okapies/sircuit) • rx-process (https://github.com/okapies/rx- process)

Slide 5

Slide 5 text

ॻ͍ͨ΋ͷ • ຋༁: • 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 ΛબͿ̏ͭͷཧ༝ ࠓ೔ͷ͓୊ʂ

Slide 6

Slide 6 text

Reactive
 Streams https://www.flickr.com/photos/vinothchandar/8160520014

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Reactive Streams ͱ͸ʁ • ”Back-Pressure ෇͖ͷඇಉظετϦʔϜॲཧ“ͷඪ ४ΛఆΊΔ࢓༷ • ඇಉظετϦʔϜॲཧʹ͖ͭ·ͱ͏ϑϩʔ੍ޚ ͷ՝୊ʢόοϑΝᷓΕ໰୊ʣͷղܾࡦΛఏࣔ • ࢓༷ʹ४ڌͨ͠ϥϯλΠϜؒͷ૬ޓ઀ଓੑΛอ ূ͢Δ • ࠷ऴ໨ඪ͸ Java 9 Ͱͷඪ४Խ ΤϯυϢʔβ޲͚ͷ
 API ͸είʔϓ֎
 ʢ࣮૷ຖʹ༻ҙ͢Δʣ

Slide 9

Slide 9 text

ओͳࢀՃϕϯμʔ • (Akka) • (RxJava) • (Reactor) • (Vert.x) (Scala ݴޠͷ։ൃݩ) (ถࠃ࠷େखͷ
 ಈը഑৴αʔϏε)

Slide 10

Slide 10 text

ओͳࢀՃऀ • Doug Lea • JSR 166 (java.util.concurrent)
 ͷ Specification Lead • “Java Concurrency in Practice”
 ͷஶऀͷҰਓ • ඪ४Խ͸ࢯͷओಋͰਐΊΒΕ͍ͯΔ http://www.amazon.co.jp/dp/4797337206/

Slide 11

Slide 11 text

ܦҢ • Reactive Manifesto ͷൃද(2013೥9݄) • Typesafe ͷओಋͰɺඇಉظετϦʔϜٕज़Λ ։ൃ͢Δϕϯμʔ֤ࣾͷؒͰɺϑϩʔ੍ޚͷٕ ज़՝୊ͷղܾࡦΛ࿩͠߹͏ίϛϡχςΟ͕ൃ଍ • GitHub ʹ reactive-streams άϧʔϓΛ࡞੒ɻ ਖ਼ࣜʹڞಉͰ࢓༷ࡦఆΛ࢝ΊΔ(2014೥ॳ಄)

Slide 12

Slide 12 text

Reactive? https://www.flickr.com/photos/livenature/204420128

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Ͳ͜Ͱ࢖ΘΕ͍ͯΔ͔ • GUIɾϑϩϯτΤϯυͷ࣮૷ • (ϚΠΫϩ)αʔϏεͷ࣮૷ • Ϗοάσʔλॲཧ • etc… • Πϕϯτۦಈ • ඇಉظϓϩάϥϛϯά • ฒߦॲཧ ͕ඞཁͳͱ͜Ζશൠ

Slide 18

Slide 18 text

ʊਓਓਓਓਓਓਓਓਓਓਓਓਓਓʊ ʼ σʔλϑϩʔʴมߋͷ఻೻ ʻ ʉY^Y^Y^Y^Y^Y^Y^Y^Y^Y^Yʉ http://en.wikipedia.org/wiki/Reactive_programming ※ ఆٛͷҰྫͰ͢

Slide 19

Slide 19 text

ී௨ͷ(खଓ͖ܕͷ)ίʔυ A = 1; B = 2; C = (A+1) + (B-1)*2; ※”Ͳ͏΍࣮ͬͯߦ͢Δ͔”͸ࣗ໌ ʢʹ ࣮ߦϞσϧʣ

Slide 20

Slide 20 text

σʔλϑϩʔ g1 g2 h Input Input Output f ؔ਺ ೖྗ ग़ྗ ؔ਺ͱɺؔ਺ಉ࢜Λܨ͗߹Θͤͨάϥϑ

Slide 21

Slide 21 text

-1 ×2 + +1 A B C σʔλϑϩʔͰදͯ͠ΈΔ A = 1; B = 2; C = (A+1) + (B-1)*2; 1 2 4 1 2 2 ※Ͳ͏࣮ߦ͢Δ͔͸๨ΕΑ͏ ʢม਺ͱԋࢉࢠͷؔ܎͚ͩݟΔʣ

Slide 22

Slide 22 text

-1 ×2 + +1 A B C ม਺Λߋ৽: खଓ͖ܕͷ৔߹ A = 1; B = 2; C = (A+1) + (B-1)*2;
 A = 2; 1 $ 2 2 4 C ʹ͸
 ൓ө͞Εͳ͍ खଓ͖ܕͷ࣮ߦϞσϧ × × × × × ×

Slide 23

Slide 23 text

-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 ͳ࣮ߦϞσϧ

Slide 24

Slide 24 text

-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 ͳ࣮ߦϞσϧ

Slide 25

Slide 25 text

-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 ͳ࣮ߦϞσϧ

Slide 26

Slide 26 text

Reactive Programming ͷ࣮ݱखஈ 1. ”σʔλϑϩʔ”Λ
 هड़͢ΔͨΊͷϓϩάϥϛϯάϞσϧ 2. ”มߋͷ఻೻”Λ
 ϓϩάϥϚʹ୅Θ࣮ͬͯߦ͢ΔϥϯλΠϜ

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

ྫ (RxJava): Observable a = …; Observable b = …; ! Observable a1 = a.map( (i) -> i + 1 ); Observable b1 = b.map( (j) -> j - 1 ) .map( (k) -> k * 2 ); ! ! Observable c = Observable.combineLatest(a1, b1, (i, j) -> i + j ); σʔλϑϩʔΛܨ͛Δϝιου ؔ਺ ೖྗ A B C +1 —1 ×2 +

Slide 30

Slide 30 text

“What” ͱ “How” ͷ෼཭ ϓϩάϥϚ͸”ೖྗΛԿʹม׵͢Δ͔“͚ͩʹ
 ஫໨ͯ͠ϩδοΫΛॻ͘ɻ ”Ͳ͏΍࣮ͬͯߦ͢Δ͔”͸ϥϯλΠϜͷ໾ׂɻ ʢ࣮ߦϞσϧʣ G ? ? ? ? ? f g1 + g2

Slide 31

Slide 31 text

Reactive ͳϞσϧͷϝϦοτ • มߋ͕ࣗಈతʹ൓ө͞ΕΔ • ඇಉظԽɾฒྻԽ͠΍͍͢ • ૊Έ߹ΘͤՄೳ (composable) ʹ͠΍͍͢ } ϥϯλΠϜʹ೚ͤΔ (ੑೳ࠷దԽ΋༰қ) ؔ਺ ೖྗ ग़ྗ ֤ؔ਺͸ೖྗͷΈʹ൓Ԡ͢Δ → ֎෦ͷঢ়ଶʹӨڹ͞Εͳ͍ → ૄ݁߹ʹͳΔʂ

Slide 32

Slide 32 text

༨ஊ: ΤΫηϧᄻ͑࿩ͱͷൺֱ • Reactive Programming ͸“ΤΫηϧ”Ͱᄻ͑Δ ΑΓσʔλϑϩʔϞσϧͰઆ໌͢Δํ͕ྑ͍ • composable ͳؔ਺ͷඞཁੑ͕෼͔Γқ͍ • ೖྗͱग़ྗͷґଘੑΛએݴͯ͠ϓϩάϥϛ ϯά͢Δ΍Γํ͕ຊ࣭Ͱ͋Δ͜ͱΛࣔͤΔ

Slide 33

Slide 33 text

͍Ζ͍Ζͳ
 Reactive Programming https://www.flickr.com/photos/deanhochman/12469727533

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

RP ͷϓϩάϥϛϯάϞσϧ • ໋ྩܕ Reactive Programming • ΦϒδΣΫτࢦ޲ Reactive Programming (OORP) • ؔ਺ܕ Reactive Programming (FRP) • ͦͷଞʢFuture/PromiseɺActorʣ ୯ʹ “RP” ͱݴ͏ͱ ͜Ε͕ଟ͍…ʁ {

Slide 36

Slide 36 text

ؔ਺ܕ Reactive Programming • ਓʹΑͬͯࢦ͍ͯ͠Δ΋ͷ͕ҧ͏… ( ´-` ) • ຊདྷͷʢڱٛͷʣFRP • ΧδϡΞϧͳʢ޿ٛͷʣFRP

Slide 37

Slide 37 text

ຊདྷͷ FRP • 1997 ೥ʹ Conal Elliott (@conal) Β͕ఏএ • FRP ͷຊՈ͸ͬͪ͜Ͱɺݫີͳఆ͕ٛ͋Δ Time A Behavior ≈
 Function1[Time, A] Event ≈ List> ࿈ଓతͳ ʮ࣌ؒతʹมԽ͢ΔৼΔ෣͍ʯ (t, a) ཭ࢄతͳ
 ʮΠϕϯτετϦʔϜʯ A Time +

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

ͳͥ FRP ͳͷ͔ʁ • σʔλϑϩʔΛએݴతʹॻ͚Δ • ෭࡞༻Λ੍ݶ͢Δख๏ಉ࢜Ͱ૬ੑ͕ྑ͍ • ߴ֊ؔ਺ (map, fold, filter, …) ͷ׆༻ • ڱٛ΋޿ٛ΋ composability ΛߴΊΔखஈͱ ͯؔ͠਺ܕͷख๏Λ࢖͍ͬͯΔ఺͸ಉ͡

Slide 40

Slide 40 text

Future/Promise • RP ͷ஥ؒʹೖΕΔ͜ͱ͕͋Δ • σʔλϑϩʔ͸͋Δ͕มߋͷ఻೻͸ͳ͍ • ྺ࢙తʹ͸ 1970 ೥୅·ͰḪΔ͜ͱ͕Ͱ͖ɺRP ΍ Actor Ϟσϧͱ΋ਂ͍ؔ܎͕͋Δ • JavaScript ͳͲͷϑϩϯτΤϯυͷ΄͔ɺϚΠΫ ϩαʔϏεͷ࣮૷खஈͱͯ͠΋޿͘࢖ΘΕ͍ͯΔ ʢTwitter Finagle ΍ Node.js ͳͲʣ

Slide 41

Slide 41 text

Actor Ϟσϧ • RP ͷ஥ؒʹೖΕͨΓೖΕͳ͔ͬͨΓ͢Δ • ϓϩάϥϛϯάϞσϧతʹ͸ composablity ͕௿ ͍ͷ͕೉఺ • ۙ೥͸ϥϯλΠϜͱͯ͠ͷଆ໘͕஫໨͞Ε͍ͯΔ • Erlang ͕ఏএ͢Δ ”Let It Crash" ఩ֶʹجͮ͘଱ো ֐ੑɺෛՙ؅ཧɺεέʔϥϏϦςΟ • Actor ϥϯλΠϜͷ্ʹ RP ͷϓϩάϥϛϯάϞσ ϧΛࡌͤͨͷ͕ Akka Streamsʢޙड़ʣ

Slide 42

Slide 42 text

Ϣʔεέʔεͱ࣮ྫ https://www.flickr.com/photos/sdstrowes/8248580877

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

GUIɾϑϩϯτΤϯυ https://github.com/tehmou/rx-android-architecture RxJava + Android
 ͷσϞΞϓϦ ςΩετϘοΫε͕ߋ৽͞Ε
 Δ౓ʹ JSON API Ͱݕࡧɻ ໭͖ͬͯͨ݁ՌΛඇಉظʹ Widget ʹ൓ө

Slide 46

Slide 46 text

ϚΠΫϩαʔϏε 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

Slide 47

Slide 47 text

Ϗοάσʔλॲཧ https://speakerdeck.com/googlecloudjapan/google-cloud-dataflowwoli-jie-suru ྫ: Google Cloud Dataflow PCollection Λม׵͢Δσʔλϑϩʔͱͯ͠هड़

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

RP ͷීٴ͕΋ͨΒͨ͠΋ͷ • ϓϩάϥϛϯάϞσϧͷ੔උ͕ਐΈɺඇಉظ ॲཧ΍ฒߦॲཧͷෑډ͕େ෯ʹԼ͕ͬͨ • ϞόΠϧ͔Βେن໛෼ࢄ؀ڥ·ͰɺϥϯλΠ Ϝͷબ୒ࢶ΋ͲΜͲΜ૿͍͑ͯΔ • UI ϓϩάϥϛϯάͷมֵ΍ɺϚΠΫϩαʔϏ εͷ୆಄ͳͲΛޙԡ͠

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

Reactive Manifesto ͱ͸ • ”Reactive Systems” ΁ͷࢧ࣋Λݺͼ͔͚Δจॻ • Reactive ͳγεςϜ͕උ͑Δཁ݅ʹ͍ͭͯड़΂͍ͯΔ • Typesafe ࣾ (Scala ݴޠͷ։ൃݩ) ͷϝϯόʔ͕ओಋ • ͦͷଞʹ Dave Farley (”ܧଓతσϦόϦʔ”ͷஶऀ) ΍ Martin Thompson (ݩ LMAX CTO) ͳͲ • ϚʔέςΟϯάจॻͱͯ͠ͷ৭࠼͕ೱ͍ʢํ޲ੑͱͯ͠ ͸ “The Twelve-Factor App” ʹ͍͔ۙ΋ʣ

Slide 54

Slide 54 text

γεςϜ΁ͷཁٻͷมԽ ਺೥લ ݱࡏ αʔό਺ ʙ਺ेϊʔυ ਺ઍҎ্ʙ ཁٻԠ౴࣌ؒ ʙ਺ඵ ʙϛϦඵ ཁٻՔಇ཰ ਺࣌ؒͷΦϑϥΠϯ
 ϝϯςφϯεΛڐ༰ 100% σʔλྔ ΪΨόΠτ୯Ґ ϖλόΠτ୯Ґ

Slide 55

Slide 55 text

γεςϜͷ࡞ΓํͷมԽ “େن໛γεςϜΛߏங͢Δ૊৫͸
 ͜ͷมԽʹରॲ͢ΔઃܭݪଇΛ
 ͢Ͱʹൃݟ͍ͯ͠Δ” ! ! ! “ͦͷΑ͏ͳಛ௃Λඋ͑ΔγεςϜΛ Reactive Systems ͱݺ΅͏”

Slide 56

Slide 56 text

Reactive Systems ͷಛ௃ http://www.reactivemanifesto.org/ja

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

…۩ମతʹ͸ʁ • ۩ମྫ͸ಛʹڍ͛ΒΕ͍ͯͳ͍ • Actor Ϟσϧ ʹجͮ͘ΞʔΩςΫνϟΛڧ ͘૝ఆ͍ͯ͠Δ • ҰํͰϚΠΫϩαʔϏεͱ΋਌࿨ੑ͕ߴͦ ͏ʹݟ͑Δ

Slide 59

Slide 59 text

Dave Farley ᐌ͘: ࢲ͕࢓ࣄͰؔΘͬͨɺେ͖ͳγεςϜͷΞʔΩςΫνϟʹ͸ྨ ࣅ఺͕͋ͬͨɻͦ͏ͨ͠γεςϜͰ͸ɺ໰୊ྖҬʹ͓͚Δ໌֬ ͳ Bounded Context Λ࣮૷ͨ͠αʔϏε͕ૄ݁߹Ͱ݁ͼ͍ͭ ͓ͯΓɺޓ͍ͷ௨৴͸ඇಉظϝοηʔδͷΈͰߦ͏ɻ͜ΕΒ ͸ɺRDB ͷ্ʹߏங͞Εͨശग़͠ͷඪ४తͳࡾ૚ΞʔΩςΫ νϟͷγεςϜͷ΍Γํͱ͸ࣅͯ΋ࣅ͔ͭͳ͍ɻ ιϑτ΢ΣΞΛ࣮ߦ͢Δϋʔυ΢ΣΞ؀ڥ͕มΘΓͭͭ͋Δɻ RAM ͱσΟεΫͷՁ͕֨ࠩॖ·ΓɺRAM ༰ྔ͸ܶతʹ૿Ճɻ ࠓ΍෼ࢄϓϩάϥϛϯά͸ඪ४ͰɺωοτϫʔΫ͸σΟεΫ ΑΓ଎͍ɻେ༰ྔͷඇشൃੑ RAM ͷ࣮༻Խ͸ؒۙͩɻ͜Ε͸ ͭ·Γɺʮඪ४తΞϓϩʔνʯΛࢧ࣋͢ΔԾઆ͕มԽͨ͠ͱ ͍͏͜ͱͩɻ “The Reactive Manifesto | Dave Farley's Weblog” ΑΓົ༁

Slide 60

Slide 60 text

ਤʹ͢Δͱ: ϓϨθϯςʔγϣϯ ϏδωεϩδοΫ RDB ಉظݺग़ ಉظݺग़ RDB ඇಉظ ඇಉظ ඇಉظ ֎෦
 αʔϏε ඇಉظ ϚΠΫϩαʔϏε
 ͬΆ͘ݟ͑Δ

Slide 61

Slide 61 text

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)

Slide 62

Slide 62 text

ϚΠΫϩαʔϏε ⊂ 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

Slide 63

Slide 63 text

vs. Reactive Programming • Reactive Manifesto ͸ Reactive Programming ͔Β༻ޠΛआΓ͖ͯͨผ෺ͱߟ͑Δͷ͕ྑ͍ • Reactive ͷʮ֎෦͔Βͷܹࢗʹ൓Ԡ͢Δʯͱ ͍͏ҙຯ߹͍Λڧௐ͍ͯ͠Δ
 ʢΠϕϯτɺෛՙɺো֐ɺϢʔβɺ…ʣ

Slide 64

Slide 64 text

vs. Reactive Programming • ͱ͸͍͑…ϝοηʔδۦಈʹΑΔඇಉظϓϩ άϥϛϯά͕େલఏʹͳΔͷͰɺ࣮૷खஈͷ Ұͭͱͯ͠ Reactive Programming ͸༗ྗ Reactive Manifesto Reactive Programming Callback- based … ??? ϓϩάϥϛϯά Ϟσϧ ϥϯλΠϜ ΞʔΩςΫνϟ Actor Model

Slide 65

Slide 65 text

Programming × Systems • Reactive Manifesto ͷ໨ࢦ͢ੈք: • େن໛σʔλΛѻ͑ΔεέʔϥϒϧͰՄ༻ ੑͷߴ͍෼ࢄγεςϜ • RM ͷจ຺Ͱ RP Λ׆༻͢Δʹ͸ɺେن໛ॲཧ ʹରͯ͠ responsive ͳϥϯλΠϜ͕ඞཁ

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

Ϣʔεέʔε • ԻָɾϏσΦͷετϦʔϛϯά • ωοτϫʔΫΛհͨ͠όϧΫσʔλͷసૹ • αʔό㱻ϞόΠϧΫϥΠΞϯτؒͷ௨৴ • IoT (Internet of Things) • ϦΞϧλΠϜσʔλղੳॲཧ • etc…

Slide 69

Slide 69 text

Reactive Streams ͷਖ਼ମ ඇಉظετϦʔϜॲཧ ! Non-blocking back pressure + ↑͕͜͜ϙΠϯτʂ

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

σʔλϑϩʔͷܾյ • Publisher ͸ Subscriber ΁ϝοηʔδΛૹΔ Publisher Subscriber … 3 msgs/sec 3 msgs/sec ϝοηʔδόοϑΝ ॲཧೳྗ ૹ৴ೳྗ =

Slide 73

Slide 73 text

σʔλϑϩʔͷܾյ • Subscriber ͕଎͍ͱ͖͸ՋʹͳΔ
 → ܭࢉػࢿݯΛ༗ޮ׆༻Ͱ͖ͳ͍ … < Publisher Subscriber 3 msgs/sec དྷͳ͍… ͋Μ·ૹΔͱ
 ѱ͍͔ͳ… 1 msg/sec

Slide 74

Slide 74 text

σʔλϑϩʔͷܾյ • Publisher ͕଎͗͢ΔͱόοϑΝ͕ᷓΕͯࢮ͵ … > Publisher Subscriber 3 msgs/sec 100 msgs/sec ΍Ίͯʂ Զʹ
 ·͔ͤΖʔʂ

Slide 75

Slide 75 text

Back-Pressure • Publisher ͸ϦΫΤετ͞Εͨ෼͚ͩϝοηʔ δΛૹΔ → ҆શ … 100 msgs/sec [Request(3)] Publisher Subscriber 3 msgs/sec ࣍ɺ3 ݸ͍ͩ͘͞ ͍͋Αʔ

Slide 76

Slide 76 text

Back-Pressure • Back-Pressure ͸఻೻͢Δ • C ͕஗͘ͳͬͨΒ B, A ΋εϩʔμ΢ϯ͢Δ B C … ࠓ͸͍͍Ͱ͢ A … ࠓ͸͍͍Ͱ͢ ͍͋Αʔ

Slide 77

Slide 77 text

SPI ͷΠϯλϑΣʔε public interface Publisher { public void subscribe(Subscriber super T> s); } public interface Subscriber { 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 ͕ϕʔε

Slide 78

Slide 78 text

࢓༷ॻͱTCK • ϥϯλΠϜؒͷ૬ޓ઀ଓੑΛอূ͢Δ

Slide 79

Slide 79 text

ओͳ࣮૷ • Akka Streams (Typesafe) • Ratpack • Reactor (Spring) • RxJava (Netflix) • Vert.x 3.0 (Red Hat)

Slide 80

Slide 80 text

DB ΞΫηεϥΠϒϥϦͷ࣮૷ • Slick 3.0 a.k.a “Reactive Slick” (JDBC) • ReactiveMongo • Reactive Rabbit (AMQP) • Reactive Kafka • etc…

Slide 81

Slide 81 text

ͳͥ Reactive ͳ DB Ξμϓλ͕ඞཁʁ • ྫ͑͹ɺJDBC ͸ಉظ API ͳͷͰɺඇಉظͳ ϑϨʔϜϫʔΫΛ࢖͍ͬͯͯ΋ worker thread ΁ͷॲཧͷҕৡΛࣗ෼Ͱॻ͘ඞཁ͕͋ͬͨ • context switch ͷίετɺεϨου਺ʹԠ͡ ͨϝϞϦফඅɺεϨουؒ௨৴ͷϩοΫڝ߹ • ϫʔΧεϨου਺ͱɺDB ίωΫγϣϯϓʔϧ ͷαΠζΛ”ਖ਼͘͠”ઃఆ͢Δඞཁ͕͋Δ

Slide 82

Slide 82 text

͢΂͕ͯ Reactive ʹͳΔ • Reactive ͳ DB Ξμϓλ͕ग़ἧͬͨ͜ͱͰɺ ೖޱ͔Βग़ޱ·Ͱ Reactive Streams ४ڌͷ෦ ඼Ͱ૊Έ্͛ΒΕΔΑ͏ʹͳͬͨ AMQP Rx
 Java Akka Vert.x RDB ྫ:

Slide 83

Slide 83 text

Reactive Streams ͷࠓޙ • Java 9 Ͱͷඪ४Խ (j.u.c.Flow) • ೿ੜϓϩδΣΫτ • JavaScritp Interface (reactive-streams-js) • Network Protocol (reactive-streams-io) • Reactive IPC (http://reactive-ipc.io)

Slide 84

Slide 84 text

Akka
 Streams

Slide 85

Slide 85 text

Akka Streams ͱ͸ • Akka Λϕʔεʹͨ͠ Reactive Streams ࣮૷ͷ ͻͱͭ • σʔλϑϩʔΛهड़͢Δܕ҆શͳ DSL Λఏڙ ͢ΔʢJava ˍ Scalaʣ • কདྷతʹ͸ Play Framework ͷج൫ʹͳΔ༧ఆ

Slide 86

Slide 86 text

جຊతͳߟ͑ํ • σʔλϑϩʔ
 
 
 
 Λ Actor ౳Λ࢖ͬͯ۩ݱԽ (materialize) ͢Δ • ૊Έ্͛ͨ FlowGraph ࣗମ͸ઃܭਤ (blueprint) ͳͷͰɺ৭ʑͳ Source ΍ Sink Ͱ࢖͍ճͤΔ Source Flow Sink

Slide 87

Slide 87 text

ίʔυྫ (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 Λ࢖࣮ͬͯߦ

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

·ͱΊ

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

·ͱΊ • য఺͸͢ͰʹϥϯλΠϜ΍γεςϜΞʔΩςΫ νϟ΁ҠΓͭͭ͋ΔͷͰ͸ͳ͍͔ • Reactive Streams ͕࡞Γग़͢ΤίγεςϜ
 → ैདྷܕ Web ΞʔΩςΫνϟ΁ͷ௅ઓ • ϥϯλΠϜͷੑೳɺ଱ো֐ੑɺӡ༻ੑ͕બ ఆج४ͱͯ͠ΑΓॏཁʹ ҰํͰʮԿͰॻ͔͘ʯ͸
 ͲΜͲΜऔΓସ͑ՄೳʹͳΔʁ

Slide 93

Slide 93 text

Thank you!