Reactive とは何か? #reactive_shinjuku

Reactive とは何か? #reactive_shinjuku

2015/08/18 Reactive System Meetup in 西新宿 by @okapies
http://reactive-shinjuku.connpass.com/event/17991/

Ef45a84b10aa3cb5acd2ea0c9846bd3b?s=128

Yuta Okamoto

August 18, 2015
Tweet

Transcript

  1. 2.

    Who are you? • Ԭຊ ༤ଠ (@okapies) • ڵຯ͕͋Δ͜ͱɿ •

    Scala ͱ͔ؔ਺ܕϓϩάϥϛϯάͱ͔ • ΢ΣϒαʔϏεͷΠϯϑϥपΓʢBash ৬ਓʣ • Scala Matsuri 2016 ४උҕһʢ຋༁νʔϜʣ
  2. 3.

    ॻ͍ͨهࣄ • ւ֎จݙͷ຋༁ • Reactive Manifesto (http://www.reactivemanifesto.org/) • Effective Scala

    (http://twitter.github.io/effectivescala/) • ϒϩάهࣄ (http://okapies.hateblo.jp/) • ʮؔ਺ܕϓϩάϥϚͷͨΊͷ Rx ೖ໳ʯγϦʔζ • ʮϚΠΫϩαʔϏε͕ Scala ΛબͿ̏ͭͷཧ༝ʯ
  3. 5.

    ࠓ೔͓࿩͍ͨ͜͠ͱ • Reactive ͱ͸Կ͔ʁʢʹ͍ͭͯͷࢲͷཧղʣ • Reactive ͷഎܠʹ͋ΔύϥμΠϜγϑτ • खଓ͖ܕ (how)

    ͔Βએݴܕ (what) ΁ • ϓϩάϥϛϯάϞσϧ͔ΒϥϯλΠϜ΁ • ಉظతͳϞϊϦε͔Βඇಉظతͳ෼ࢄγεςϜ΁
  4. 10.

    Reactive ͕ٻΊΒΕΔ৔໘ • Frontend • GUIɾϑϩϯτΤϯυ • Backend • ϚΠΫϩαʔϏε

    • Ϗοάσʔλॲཧ ϓϩάϥϛϯάϞσϧ͕ओͳؔ৺ Ճ͑ͯΞʔΩςΫνϟʹؔ৺
  5. 11.

    ղܾ͍ͨ͠՝୊ • ػೳ໘: • Πϕϯτʢϝοηʔδʣۦಈ • ඇಉظͳίϯϙʔωϯτؒ࿈ܞ • ฒߦɾฒྻॲཧ •

    ඇػೳ໘: • ଱ো֐ੑɺεέʔϥϏϦςΟ ՝୊΍ͦΕʹର͢ΔΞϓϩʔν͸
 ڞ௨͍ͯ͠Δ͕…
  6. 16.

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

    αʔϏεʹର͢ΔཁٻͷมԽ ਺೥લ ݱࡏ αʔό਺ ʙ਺ेϊʔυ ਺ઍҎ্ʙ ཁٻԠ౴࣌ؒ ʙ਺ඵ ʙϛϦඵ ཁٻՔಇ཰

    ਺࣌ؒͷΦϑϥΠϯ
 ϝϯςφϯεΛڐ༰ 100% σʔλྔ ΪΨόΠτ୯Ґ ϖλόΠτ୯Ґ
  8. 21.

    Reactive Manifesto • Reactive Systems ΁ͷࢧ࣋Λݺͼ͔͚Δจॻ • ࣾͷ CTO Β͕ओಋ

    • ଞʹ Dave Farley (”ܧଓతσϦόϦʔ”ͷஶऀ) ΍ Martin Thompson (ݩ LMAX CTO) ͳͲ • ϚʔέςΟϯάจॻͱͯ͠ͷ৭࠼͕ೱ͍ʢํ޲ੑ ͱͯ͠͸ “The Twelve-Factor App” ʹ͍ۙʣ ͦΖͦΖࢍಉऀ
 12,000 ਓ௒͑
  9. 22.

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

    • ࢓༷ʹ४ڌͨ͠ϥϯλΠϜͷ૬ޓ઀ଓੑΛอূ • JDK 9 Ͱͷඪ४Խʹ޲͚ͯ࡞ۀத Reactive Systems
 ͷٕज़త՝୊Λղܾ
  10. 24.

    ओͳࢀՃϕϯμʔ • (Akka) • (RxJava) • (Reactor) • (Vert.x) Scala

    ݴޠͷ։ൃݩ ถࠃ࠷େखͷ
 ಈը഑৴αʔϏε Spring Framework
 ͷ։ൃݩ
  11. 25.

    ओͳࢀՃऀ • Doug Lea • JSR 166 (java.util.concurrent)
 ͷ Specification

    Lead • “Java Concurrency in Practice”
 ͷஶऀͷҰਓ • JDK Ͱͷඪ४Խ͸ࢯͷओಋͰਐΊΒΕ͍ͯΔ http://www.amazon.co.jp/dp/4797337206/
  12. 29.

    Reactive ͳίϯϙʔωϯτ • σʔλͷೖྗʹ൓Ԡ (react) ͯ͠ɺͦΕΛม׵ ͯ͠ग़ྗ͢Δ ʁ ೖྗ ग़ྗ

    • ؔ਺ʁ • ΦϒδΣΫτʁ • ΞΫλʔʁ • αϒγεςϜʁ ίϯϙʔωϯτͷதͰ
 ಈ࡞͕׬݁͢Δ
  13. 30.

    ๬·͍͠ੑ࣭: ૄ݁߹ੑɺִ཭ੑ • ϞδϡʔϧԽ, ඇಉظԽ, ฒྻԽ͕༰қʹͳΔ ʁ ೖྗ ग़ྗ ?

    ? ࣗ෼͕Կͷίϯϙʔωϯτ ʹґଘ͢Δ͔͸ҙࣝ͠ͳ͍ × × ֎ଆͷঢ়ଶ΍ಈ࡞ʹ
 ʢ҉໧తʹʣ
 ӨڹΛ༩͑ͳ͍ ೖྗσʔλʹͷΈґଘ͢Δ
  14. 35.

    ैདྷͷखଓ͖ܕϓϩάϥϜ A = 1; B = 2; C = (A+1)

    + (B-1)*2; ※”Ͳ͏΍࣮ͬͯߦ͢Δ͔”͸ࣗ໌ ʢʹ ࣮ߦϞσϧʣ ্͔Βॱ൪ʹ࣮ߦ͢Δ
  15. 37.

    -1 ×2 + +1 A B C खଓ͖͸σʔλϑϩʔͰදݱͰ͖Δ A =

    1; B = 2; C = (A+1) + (B-1)*2; 1 2 4 1 2 2 ※Ͳ͏࣮ߦ͢Δ͔͸๨ΕΑ͏ ʢม਺ͱԋࢉࢠͷؔ܎͚ͩݟΔʣ
  16. 39.

    -1 ×2 + +1 A B C ม਺Λߋ৽: खଓ͖ܕͷ৔߹ A

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

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

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

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

    ྫ (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 +
  21. 44.

    ྫ (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 + “Ͳ͏࣮ߦ͢Δ͔” ͸
 Ͳ͜ʹ΋ॻ͍ͯͳ͍ʂ
  22. 45.

    ϓϩάϥϛϯάϞσϧ + ϥϯλΠϜ Input Input Output (2) ϥϯλΠϜ (1) ϓϩάϥϛϯάϞσϧ

    ʢมߋͷ఻೻ (how) ΛϓϩάϥϚʹ୅Θ࣮ͬͯߦ͢Δʣ ʢσʔλϑϩʔ (what) Λهड़͢Δखஈʣ
  23. 46.

    ϓϩάϥϛϯάϞσϧ + ϥϯλΠϜ Reactive Systems Reactive Programming ??? ϓϩάϥϛϯάϞσϧ: ϥϯλΠϜ:

    γεςϜΞʔΩςΫνϟ ϥϯλΠϜʹΑΔ
 มߋͷ఻೻͕ RP ͷಛ௃
  24. 47.

    ղܾ͍ͨ͠՝୊ʢ࠶ܝʣ • ػೳ໘: • Πϕϯτʢϝοηʔδʣۦಈ • ඇಉظͳίϯϙʔωϯτؒ࿈ܞ • ฒߦɾฒྻॲཧ •

    ඇػೳ໘: • ଱ো֐ੑɺεέʔϥϏϦςΟ खଓ͖తͳϓϩάϥϛϯάख๏
 Ͱهड़͢Δͷ͸େมͩͬͨ
  25. 48.

    Reactive ͳϞσϧͷϝϦοτ • ֎͔ΒͷΠϕϯτ͕
 ࣗಈతʹ఻೻͞ΕΔ • ඇಉظԽɾฒྻԽ͠΍͍͢ • “What” ͱ

    “How” ͕෼཭͞ΕΔͷͰ
 ૊Έ߹ΘͤՄೳ (composable) ʹ͠΍͍͢ }ϥϯλΠϜʹ೚ͤΔ
  26. 51.

    ྫ: Akka Streams • ܕ҆શͳ DSL Ͱهड़ͨ͠σʔλϑϩʔΛ Akka Actor ্Ͱ࣮ߦ͢ΔϥϯλΠϜ

    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) Actor ͷ४උ Materializer Λ࢖࣮ͬͯߦ
  27. 52.

    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
  28. 53.

    Materializer ʹΑΔ࠷దԽ • DSL Ͱهड़ͨ͠ FlowGraph Λ Materializeʢ۩ ݱԽʣ࣮ͯ͠ߦ͢Δ •

    Materializer ͷ࣮૷࣍ୈͰ༷ʑͳ࠷దԽΛࢪ ͤΔ࢓૊Έʹͳ͍ͬͯΔ • Akka ࣗମ͕෼ࢄϑϨʔϜϫʔΫͳͷͰɺকདྷ తʹ͸ࣗಈతͳ෼ࢄԽʹ΋ରԠͰ͖Δʁ
  29. 58.

    ཁٻͷมԽʢ࠶ܝʣ ਺೥લ ݱࡏ αʔό਺ ʙ਺ेϊʔυ ਺ઍҎ্ʙ ཁٻԠ౴࣌ؒ ʙ਺ඵ ʙϛϦඵ ཁٻՔಇ཰

    ਺࣌ؒͷΦϑϥΠϯ
 ϝϯςφϯεΛڐ༰ 100% σʔλྔ ΪΨόΠτ୯Ґ ϖλόΠτ୯Ґ
  30. 61.

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

    ͏ɻ ͜ΕΒ͸ɺRDB ͷ্ʹߏங͞Εͨശग़͠ͷඪ४తͳࡾ૚ ΞʔΩςΫνϟͷγεςϜͷ΍Γํͱ͸ࣅͯ΋ࣅ͔ͭͳ ͍ɻ “The Reactive Manifesto | Dave Farley's Weblog” ΑΓົ༁
  31. 64.

    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 ͷ
 ϚΠΫϩαʔϏεߏ଄
  32. 69.

    ϝοηʔδۦಈͱ஄ྗੑ • ஄ྗੑ = Scale Out + Scale In •

    ϝοηʔδۦಈͷγεςϜͰ͸ɿ • γϟʔσΟϯά΍ϨϓϦέʔγϣϯʹΑͬ ͯϦΫΤετΛ෼ࢄͰ͖Δ • Ґஔಁաੑ͕͋ΔͷͰɺίϯϙʔωϯτΛ ೚ҙͷ৔ॴʹ഑උͰ͖Δ
  33. 73.

    Reactive Systems ͷ՝୊ • Reactive Systems ͸େ༰ྔσʔλΛѻ͏ඇಉ ظσʔλετϦʔϜ • ಉظܕͱҧ͍ɺγεςϜʹྲྀΕࠐΉσʔλྲྀ

    ྔΛ੍ޚ͢Δ࢓૊Έʢϑϩʔ੍ޚʣ͕ඞཁ • ϑϩʔ੍ޚʹࣦഊ͢Δͱɺσʔλϑϩʔ͕ܾ յͯ͠γεςϜશମͷΫϥογϡʹܨ͕Δ
  34. 74.

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

    msgs/sec 3 msgs/sec ϝοηʔδόοϑΝ ॲཧೳྗ ૹ৴ೳྗ =
  35. 79.

    Back-Pressure • Publisher ͸ϦΫΤετ͞Εͨ෼͚ͩϝοηʔ δΛૹΔ → ҆શ … 100 msgs/sec

    [Request(3)] Publisher Subscriber 3 msgs/sec ࣍ɺ3 ݸ͍ͩ͘͞ ͍͋Αʔ
  36. 81.

    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 ͕ϕʔε
  37. 83.

    ४ڌ࣮૷ • Akka Streams (Typesafe) • Ratpack • Reactor (Spring)

    • RxJava (Netflix) • Vert.x 3.0 (Red Hat)
  38. 84.

    DB ΞΫηεϥΠϒϥϦͷ४ڌ࣮૷ • Slick 3.0 a.k.a “Reactive Slick” (JDBC) •

    ReactiveMongo • Reactive Rabbit (AMQP) • Reactive Kafka • etc…
  39. 85.

    ͳͥ Reactive ͳ DB Ξμϓλ͕ඞཁʁ • ྫ͑͹ɺJDBC ͸ಉظ API ͳͷͰɺඇಉظͳ

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