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

Reactive とは何か? #reactive_shinjuku

Reactive とは何か? #reactive_shinjuku

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

Yuta Okamoto

August 18, 2015
Tweet

More Decks by Yuta Okamoto

Other Decks in Programming

Transcript

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

    Scala ͱ͔ؔ਺ܕϓϩάϥϛϯάͱ͔ • ΢ΣϒαʔϏεͷΠϯϑϥपΓʢBash ৬ਓʣ • Scala Matsuri 2016 ४උҕһʢ຋༁νʔϜʣ
  2. ॻ͍ͨهࣄ • ւ֎จݙͷ຋༁ • Reactive Manifesto (http://www.reactivemanifesto.org/) • Effective Scala

    (http://twitter.github.io/effectivescala/) • ϒϩάهࣄ (http://okapies.hateblo.jp/) • ʮؔ਺ܕϓϩάϥϚͷͨΊͷ Rx ೖ໳ʯγϦʔζ • ʮϚΠΫϩαʔϏε͕ Scala ΛબͿ̏ͭͷཧ༝ʯ
  3. ࠓ೔͓࿩͍ͨ͜͠ͱ • Reactive ͱ͸Կ͔ʁʢʹ͍ͭͯͷࢲͷཧղʣ • Reactive ͷഎܠʹ͋ΔύϥμΠϜγϑτ • खଓ͖ܕ (how)

    ͔Βએݴܕ (what) ΁ • ϓϩάϥϛϯάϞσϧ͔ΒϥϯλΠϜ΁ • ಉظతͳϞϊϦε͔Βඇಉظతͳ෼ࢄγεςϜ΁
  4. Reactive ͕ٻΊΒΕΔ৔໘ • Frontend • GUIɾϑϩϯτΤϯυ • Backend • ϚΠΫϩαʔϏε

    • Ϗοάσʔλॲཧ ϓϩάϥϛϯάϞσϧ͕ओͳؔ৺ Ճ͑ͯΞʔΩςΫνϟʹؔ৺
  5. ղܾ͍ͨ͠՝୊ • ػೳ໘: • Πϕϯτʢϝοηʔδʣۦಈ • ඇಉظͳίϯϙʔωϯτؒ࿈ܞ • ฒߦɾฒྻॲཧ •

    ඇػೳ໘: • ଱ো֐ੑɺεέʔϥϏϦςΟ ՝୊΍ͦΕʹର͢ΔΞϓϩʔν͸
 ڞ௨͍ͯ͠Δ͕…
  6. ϚΠΫϩαʔϏε 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. αʔϏεʹର͢ΔཁٻͷมԽ ਺೥લ ݱࡏ αʔό਺ ʙ਺ेϊʔυ ਺ઍҎ্ʙ ཁٻԠ౴࣌ؒ ʙ਺ඵ ʙϛϦඵ ཁٻՔಇ཰

    ਺࣌ؒͷΦϑϥΠϯ
 ϝϯςφϯεΛڐ༰ 100% σʔλྔ ΪΨόΠτ୯Ґ ϖλόΠτ୯Ґ
  8. Reactive Manifesto • Reactive Systems ΁ͷࢧ࣋Λݺͼ͔͚Δจॻ • ࣾͷ CTO Β͕ओಋ

    • ଞʹ Dave Farley (”ܧଓతσϦόϦʔ”ͷஶऀ) ΍ Martin Thompson (ݩ LMAX CTO) ͳͲ • ϚʔέςΟϯάจॻͱͯ͠ͷ৭࠼͕ೱ͍ʢํ޲ੑ ͱͯ͠͸ “The Twelve-Factor App” ʹ͍ۙʣ ͦΖͦΖࢍಉऀ
 12,000 ਓ௒͑
  9. Reactive Streams • “Back-Pressure ෇͖ͷඇಉظετϦʔϜॲཧ” ͷ ඪ४ΛఆΊΔ࢓༷ • ඇಉظετϦʔϜॲཧʹ͖ͭ·ͱ͏ϑϩʔ੍ޚ ͷ՝୊ʢόοϑΝᷓΕ໰୊ʣͷղܾࡦΛఏࣔ

    • ࢓༷ʹ४ڌͨ͠ϥϯλΠϜͷ૬ޓ઀ଓੑΛอূ • JDK 9 Ͱͷඪ४Խʹ޲͚ͯ࡞ۀத Reactive Systems
 ͷٕज़త՝୊Λղܾ
  10. ओͳࢀՃϕϯμʔ • (Akka) • (RxJava) • (Reactor) • (Vert.x) Scala

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

    Lead • “Java Concurrency in Practice”
 ͷஶऀͷҰਓ • JDK Ͱͷඪ४Խ͸ࢯͷओಋͰਐΊΒΕ͍ͯΔ http://www.amazon.co.jp/dp/4797337206/
  12. Reactive ͳίϯϙʔωϯτ • σʔλͷೖྗʹ൓Ԡ (react) ͯ͠ɺͦΕΛม׵ ͯ͠ग़ྗ͢Δ ʁ ೖྗ ग़ྗ

    • ؔ਺ʁ • ΦϒδΣΫτʁ • ΞΫλʔʁ • αϒγεςϜʁ ίϯϙʔωϯτͷதͰ
 ಈ࡞͕׬݁͢Δ
  13. ๬·͍͠ੑ࣭: ૄ݁߹ੑɺִ཭ੑ • ϞδϡʔϧԽ, ඇಉظԽ, ฒྻԽ͕༰қʹͳΔ ʁ ೖྗ ग़ྗ ?

    ? ࣗ෼͕Կͷίϯϙʔωϯτ ʹґଘ͢Δ͔͸ҙࣝ͠ͳ͍ × × ֎ଆͷঢ়ଶ΍ಈ࡞ʹ
 ʢ҉໧తʹʣ
 ӨڹΛ༩͑ͳ͍ ೖྗσʔλʹͷΈґଘ͢Δ
  14. ैདྷͷखଓ͖ܕϓϩάϥϜ A = 1; B = 2; C = (A+1)

    + (B-1)*2; ※”Ͳ͏΍࣮ͬͯߦ͢Δ͔”͸ࣗ໌ ʢʹ ࣮ߦϞσϧʣ ্͔Βॱ൪ʹ࣮ߦ͢Δ
  15. -1 ×2 + +1 A B C खଓ͖͸σʔλϑϩʔͰදݱͰ͖Δ A =

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

    = 1; B = 2; C = (A+1) + (B-1)*2;
 A = 2; 1 $ 2 2 4 C ʹ͸
 ൓ө͞Εͳ͍ खଓ͖ܕͷ࣮ߦϞσϧ × × × × × ×
  17. -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. -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. -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. ྫ (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. ྫ (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. ϓϩάϥϛϯάϞσϧ + ϥϯλΠϜ Input Input Output (2) ϥϯλΠϜ (1) ϓϩάϥϛϯάϞσϧ

    ʢมߋͷ఻೻ (how) ΛϓϩάϥϚʹ୅Θ࣮ͬͯߦ͢Δʣ ʢσʔλϑϩʔ (what) Λهड़͢Δखஈʣ
  23. ϓϩάϥϛϯάϞσϧ + ϥϯλΠϜ Reactive Systems Reactive Programming ??? ϓϩάϥϛϯάϞσϧ: ϥϯλΠϜ:

    γεςϜΞʔΩςΫνϟ ϥϯλΠϜʹΑΔ
 มߋͷ఻೻͕ RP ͷಛ௃
  24. ղܾ͍ͨ͠՝୊ʢ࠶ܝʣ • ػೳ໘: • Πϕϯτʢϝοηʔδʣۦಈ • ඇಉظͳίϯϙʔωϯτؒ࿈ܞ • ฒߦɾฒྻॲཧ •

    ඇػೳ໘: • ଱ো֐ੑɺεέʔϥϏϦςΟ खଓ͖తͳϓϩάϥϛϯάख๏
 Ͱهड़͢Δͷ͸େมͩͬͨ
  25. Reactive ͳϞσϧͷϝϦοτ • ֎͔ΒͷΠϕϯτ͕
 ࣗಈతʹ఻೻͞ΕΔ • ඇಉظԽɾฒྻԽ͠΍͍͢ • “What” ͱ

    “How” ͕෼཭͞ΕΔͷͰ
 ૊Έ߹ΘͤՄೳ (composable) ʹ͠΍͍͢ }ϥϯλΠϜʹ೚ͤΔ
  26. ྫ: 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. 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. Materializer ʹΑΔ࠷దԽ • DSL Ͱهड़ͨ͠ FlowGraph Λ Materializeʢ۩ ݱԽʣ࣮ͯ͠ߦ͢Δ •

    Materializer ͷ࣮૷࣍ୈͰ༷ʑͳ࠷దԽΛࢪ ͤΔ࢓૊Έʹͳ͍ͬͯΔ • Akka ࣗମ͕෼ࢄϑϨʔϜϫʔΫͳͷͰɺকདྷ తʹ͸ࣗಈతͳ෼ࢄԽʹ΋ରԠͰ͖Δʁ
  29. ཁٻͷมԽʢ࠶ܝʣ ਺೥લ ݱࡏ αʔό਺ ʙ਺ेϊʔυ ਺ઍҎ্ʙ ཁٻԠ౴࣌ؒ ʙ਺ඵ ʙϛϦඵ ཁٻՔಇ཰

    ਺࣌ؒͷΦϑϥΠϯ
 ϝϯςφϯεΛڐ༰ 100% σʔλྔ ΪΨόΠτ୯Ґ ϖλόΠτ୯Ґ
  30. Reactive Systems ͷ۩ମྫ ࢲ͕࢓ࣄͰؔΘͬͨɺେ͖ͳγεςϜͷΞʔΩςΫνϟ ʹ͸ྨࣅ఺͕͋ͬͨɻ ͦ͏ͨ͠γεςϜͰ͸ɺ໰୊ྖҬʹ͓͚Δ໌֬ͳ Bounded Context Λ࣮૷ͨ͠αʔϏε͕ૄ݁߹Ͱ݁ͼ ͍͓ͭͯΓɺޓ͍ͷ௨৴͸ඇಉظϝοηʔδͷΈͰߦ

    ͏ɻ ͜ΕΒ͸ɺRDB ͷ্ʹߏங͞Εͨശग़͠ͷඪ४తͳࡾ૚ ΞʔΩςΫνϟͷγεςϜͷ΍Γํͱ͸ࣅͯ΋ࣅ͔ͭͳ ͍ɻ “The Reactive Manifesto | Dave Farley's Weblog” ΑΓົ༁
  31. 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. ϝοηʔδۦಈͱ஄ྗੑ • ஄ྗੑ = Scale Out + Scale In •

    ϝοηʔδۦಈͷγεςϜͰ͸ɿ • γϟʔσΟϯά΍ϨϓϦέʔγϣϯʹΑͬ ͯϦΫΤετΛ෼ࢄͰ͖Δ • Ґஔಁաੑ͕͋ΔͷͰɺίϯϙʔωϯτΛ ೚ҙͷ৔ॴʹ഑උͰ͖Δ
  33. Reactive Systems ͷ՝୊ • Reactive Systems ͸େ༰ྔσʔλΛѻ͏ඇಉ ظσʔλετϦʔϜ • ಉظܕͱҧ͍ɺγεςϜʹྲྀΕࠐΉσʔλྲྀ

    ྔΛ੍ޚ͢Δ࢓૊Έʢϑϩʔ੍ޚʣ͕ඞཁ • ϑϩʔ੍ޚʹࣦഊ͢Δͱɺσʔλϑϩʔ͕ܾ յͯ͠γεςϜશମͷΫϥογϡʹܨ͕Δ
  34. σʔλϑϩʔͷܾյ • Publisher ͸ Subscriber ΁ϝοηʔδΛૹΔ Publisher Subscriber … 3

    msgs/sec 3 msgs/sec ϝοηʔδόοϑΝ ॲཧೳྗ ૹ৴ೳྗ =
  35. Back-Pressure • Publisher ͸ϦΫΤετ͞Εͨ෼͚ͩϝοηʔ δΛૹΔ → ҆શ … 100 msgs/sec

    [Request(3)] Publisher Subscriber 3 msgs/sec ࣍ɺ3 ݸ͍ͩ͘͞ ͍͋Αʔ
  36. 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. ४ڌ࣮૷ • Akka Streams (Typesafe) • Ratpack • Reactor (Spring)

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

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

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