KEMBAR78
play framework async with scala | PPTX
Play framework
Asynchronous with Scala
Vũ Hải Ninh
1
Why asynchronous
2
3
For example: blocking scala code
3
// Use Apache HttpClient make a remote call to example.com
val client = new HttpClient()
val method = new GetMethod("http://www.example.com/")
// executeMethod is a blocking, synchronous call
val statusCode = client.executeMethod(method)
// When this line is executed, we are guaranteed that the call to
example.com
// has completed
println("Server responsed with %d".format(statusCode))
=> Use one thread per request and blocking IO
4
Multi-threaded Server
4
5
Problems
5
1. Threads spend most of the time idle, waiting on I/O
2. Too many threads in the thread pool:
- Memory overhead
- Context switching overhead
3. Too few threads in the thread pool:
- Run out of threads, latency goes up
- Sensitive to downstream latency!
6
Play framework support Async I/O - non-blocking code
6
Non-blocking applications are normally implemented with message passing (or events). “Asynchronous” is related to
that, as you send your request events and then get response to them in a different thread, at a different time –
asynchronously
7
Event Server
7
8
Advantage & Disadvantage non-blocking
8
Advantage:
- Thread can continue it's work in parallel
- Reducing thread switching
- Use less memory
- Ability to handle 10,000 concurrent connections
Disadvantage:
- It prevents from using the full performance of the machine, because we might end up driving one processor to
100% and letting all other processors idle around.
- Need CPU to control event loop, unsuitable with problem which has logic need calculate.
- Difficult mange data and state.
9
Need apply Non-Blocking API on Play Framework
9
- Because Play Framework base on Akka
server, use one thread for all connections. If
we code follow blocking api, request need to
wait to execute, latency goes up.
10
Event server
10
Initiating idea
11
12
Java.NIO
12
- Base on Non Blocking I/O Operation
- Buffer Oriented
- Channels
- Selector
13
Runnable & Callable
13
- java.lang.runnable
trait Runnable {
def run(): Unit
}
- java.util.concurrent.Callable
trait Callable[V] {
def call(): V
}
14
Thread
14
val myThread = new Thread(new Runnable{
def run(){
println(“Hello!”)
}
})
myThread.start()
15
Memory in multi-thread
15
Concurrency Options for
Scala
16
17
Future
17
A future is a place holder which holds a value that may become available at some point.
val f = Future {
// your long-running task here that returns an Int ...
}
18
Future
18
A future is a place holder which holds a value that may become available at some point.
val f = Future {
// your long-running task here that returns an Int ...
}
19
Access result of Future
19
A common way to use a Future is to use its callback methods
f.onComplete {
case Success(value) => println(s"Got the callback, meaning = $value")
case Failure(e) => e.printStackTrace
} or
f onSuccess {
case 0 => println("got a zero")
} or
f onFailure {
case t => println("D'oh! Got an error: " + t.getMessage)
}
20
20
Can chain several of these together like this:
val productsFuture = Future{
getUser()
}.map{ user =>
Database.save(user)
}.map { dbResponse =>
Products.get(dbResponse.user.id)
}
Combinators
21
21
Can chain several of these together like this:
val first:Future[Int] = Future{
….
10 //return 10
}
val second:Future[Int] = Future{
….
100 //return 100
}
val addResult:Future[Int] = for{
one <- first //here one is of of type int
two <- second
} yield one + two //yield returns the Future of value computed by adding two integers
val finalVerifiedResult:Future[Int] = addResult.recover{case ex:Exception => -1}
Combinators
22
The async / await language extension like C#
22
We can use scala-async library to write code more readable like this:
val combined: Future[Int] = async {
await(first) + await(second)
}
https://github.com/scala/scala-async
Summary
23
24
24
- Pure Function
- Immutability
- Function as parameter
- Function composition
- Map, filter, apply
Scalar is suitable with non-blocking
25
25
- Batch processing task
- Transaction
- Performance & Memory
Non-blocking Problems
26
Thank you for listening

play framework async with scala

  • 1.
    Play framework Asynchronous withScala Vũ Hải Ninh 1
  • 2.
  • 3.
    3 For example: blockingscala code 3 // Use Apache HttpClient make a remote call to example.com val client = new HttpClient() val method = new GetMethod("http://www.example.com/") // executeMethod is a blocking, synchronous call val statusCode = client.executeMethod(method) // When this line is executed, we are guaranteed that the call to example.com // has completed println("Server responsed with %d".format(statusCode)) => Use one thread per request and blocking IO
  • 4.
  • 5.
    5 Problems 5 1. Threads spendmost of the time idle, waiting on I/O 2. Too many threads in the thread pool: - Memory overhead - Context switching overhead 3. Too few threads in the thread pool: - Run out of threads, latency goes up - Sensitive to downstream latency!
  • 6.
    6 Play framework supportAsync I/O - non-blocking code 6 Non-blocking applications are normally implemented with message passing (or events). “Asynchronous” is related to that, as you send your request events and then get response to them in a different thread, at a different time – asynchronously
  • 7.
  • 8.
    8 Advantage & Disadvantagenon-blocking 8 Advantage: - Thread can continue it's work in parallel - Reducing thread switching - Use less memory - Ability to handle 10,000 concurrent connections Disadvantage: - It prevents from using the full performance of the machine, because we might end up driving one processor to 100% and letting all other processors idle around. - Need CPU to control event loop, unsuitable with problem which has logic need calculate. - Difficult mange data and state.
  • 9.
    9 Need apply Non-BlockingAPI on Play Framework 9 - Because Play Framework base on Akka server, use one thread for all connections. If we code follow blocking api, request need to wait to execute, latency goes up.
  • 10.
  • 11.
  • 12.
    12 Java.NIO 12 - Base onNon Blocking I/O Operation - Buffer Oriented - Channels - Selector
  • 13.
    13 Runnable & Callable 13 -java.lang.runnable trait Runnable { def run(): Unit } - java.util.concurrent.Callable trait Callable[V] { def call(): V }
  • 14.
    14 Thread 14 val myThread =new Thread(new Runnable{ def run(){ println(“Hello!”) } }) myThread.start()
  • 15.
  • 16.
  • 17.
    17 Future 17 A future isa place holder which holds a value that may become available at some point. val f = Future { // your long-running task here that returns an Int ... }
  • 18.
    18 Future 18 A future isa place holder which holds a value that may become available at some point. val f = Future { // your long-running task here that returns an Int ... }
  • 19.
    19 Access result ofFuture 19 A common way to use a Future is to use its callback methods f.onComplete { case Success(value) => println(s"Got the callback, meaning = $value") case Failure(e) => e.printStackTrace } or f onSuccess { case 0 => println("got a zero") } or f onFailure { case t => println("D'oh! Got an error: " + t.getMessage) }
  • 20.
    20 20 Can chain severalof these together like this: val productsFuture = Future{ getUser() }.map{ user => Database.save(user) }.map { dbResponse => Products.get(dbResponse.user.id) } Combinators
  • 21.
    21 21 Can chain severalof these together like this: val first:Future[Int] = Future{ …. 10 //return 10 } val second:Future[Int] = Future{ …. 100 //return 100 } val addResult:Future[Int] = for{ one <- first //here one is of of type int two <- second } yield one + two //yield returns the Future of value computed by adding two integers val finalVerifiedResult:Future[Int] = addResult.recover{case ex:Exception => -1} Combinators
  • 22.
    22 The async /await language extension like C# 22 We can use scala-async library to write code more readable like this: val combined: Future[Int] = async { await(first) + await(second) } https://github.com/scala/scala-async
  • 23.
  • 24.
    24 24 - Pure Function -Immutability - Function as parameter - Function composition - Map, filter, apply Scalar is suitable with non-blocking
  • 25.
    25 25 - Batch processingtask - Transaction - Performance & Memory Non-blocking Problems
  • 26.