Reactive programming

20
Reactive Programing Yasuki Okumura

description

 

Transcript of Reactive programming

Page 1: Reactive programming

Reactive ProgramingYasuki Okumura

Page 2: Reactive programming

自己紹介

● シャノン所属● Play + Scala歴: 1.5 年● PHP, Javascript, C++, python, ruby, Octave● Web full stack engineer ● 趣味: ジム, MOOC受講

Page 3: Reactive programming

Reactive とは?

http://www.reactivemanifesto.org/

Page 4: Reactive programming

背景

*クラウド = Scale out/in up/down*Scale Up = CPUコア数UP*ユーザ = 人間 + 物(Intenet of Things)

Page 5: Reactive programming

Play

Page 6: Reactive programming

Reactive Programing

1. Non-blocking2. Future3. Akka4. DB

Page 7: Reactive programming

Non-blocking

Blocking: 無駄にThreadを占有してしまうこと

Non-blocking: Non-blocking IO を使っている

● Nginx● WS● Reactive mongo

Page 8: Reactive programming

Future[A] - Await

● 基本的には使わない。● 例外

○ テストコード○ アプリケーション起動時 or 終了時

Page 9: Reactive programming

Future[A] - map

def map[B](f: A => B): Future[B]

val json: Future[Option[JsValue]] = WS.url(“http://example.com/index”).get.map { res => if(200 == res.status) { Some(res.json) } else { None } }

Page 10: Reactive programming

Future[A] - flatMap

def flatMap[B](f: A => Future[B]): Future[B]val json: Future[Option[JsValue]] = WS.url(url1).get.flatMap { res => val id = (res.json ¥ “id” ).as[String] WS.url(url2 + id).get.map{ res => if(200 == res.status){ Some(res.json) } else { None } } }

Page 11: Reactive programming

Future[A] - Future.successful

def successful(result:B): Future[B]val json: Future[Option[JsValue]] = WS.url(url1).get.flatMap { res => if( 200 == res.status ){ val id = (res.json ¥ “id” ).as[String] WS.url(url2 + id).get.map{ res => if(200 == res.status){ Some(res.json) } else { None } } } else { Future.successful(None) } }

Page 12: Reactive programming

データの操作は?

● synchronized ?

 → Deadlock or Livelock, Blocking

class Counter { private var count = 0 def increment = this.synchronized { count = count + 1 } def getCount = count}

Page 13: Reactive programming

Akka

● 分散処理フレームワーク● Actor model

Page 14: Reactive programming

Actor

class Counter extends Actor { var count = 0 def receive = { case “incr” => count += 1 case “get” => sender ! count }}

Page 15: Reactive programming

DBは?

Page 16: Reactive programming

Database Challenge

● 速さ● 量● 複雑なデータ● マルチコアでのConcurrency control

Page 17: Reactive programming

Modern database - VoltDB

● disk -> In-memory● Multi thread -> single thread● Dynamic lock -> Timestamp order● Data log -> Command log● SQL -> Stored procedure● Auto partitioning

Page 18: Reactive programming
Page 19: Reactive programming

まとめ

● 時代が代わり求められている物も変わって来ている

-> 新しいアーキテクチャが必要

Page 20: Reactive programming

参考

https://class.coursera.org/reactive-001

http://voltdb.com/stonebraker-says