not yet reactive but asyncronous mvc
TRANSCRIPT
![Page 2: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/2.jpg)
2
Monte-carlo simulation
![Page 3: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/3.jpg)
3
Monte-carlo simulation
java.util.Random – 1 per thread:1 thread ~ 80 sec2 threads~ 45 sec4 threads~ 26 sec
ThreadLocalRandom:1 thread ~ 8 sec2 threads~ 4 sec4 threads~ 2 sec
![Page 4: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/4.jpg)
4
Summary
java.util.Random:
• thread-safe aka synchronized• AtomicLong as seed
java.util.concurrent.ThreadLocalRandom:
• UNSAFE long as seed
Link to source: http://java-performance.info/java-util-random-java-util-concurrent-threadlocalrandom-multithreaded-environments/
![Page 6: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/6.jpg)
6
Signature of ‘typical’ @RequestMapping
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String getUser()
Link to source: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
![Page 7: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/7.jpg)
7
How does it processed
• Threaded model
Link to source: http://www.oracle.com/technetwork/articles/grid/asynchronous-servlets-088344.html
![Page 8: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/8.jpg)
Now imagine a service
8
@RestControllerclass EventsController {
}
def fastEvent(): Event = {…}
def blockingOp(): Int = {…}
![Page 9: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/9.jpg)
9
Now imagine a service
def blockingOp(): Int = { val processingTime = ThreadLocalRandom.current.nextInt(1000, 2000) // IO bounded operation intended here Thread.sleep(processingTime) processingTime}
def fakeEvent(): Event = { Event("fakeId", System.currentTimeMillis, Array.empty)}
With one “heavy” operation
And a light one
![Page 11: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/11.jpg)
11
Side note
Link to source: http://gatling.io/
Load test are written in Gatling
Try to simulate C10K with JMeterthreaded model, again
Why not Jmeter?
Link to source: https://github.com/alkersan/timebench/blob/master/modules/loadtest/src/test/scala/SpringBootSimulation.scala
![Page 12: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/12.jpg)
12
Asynchronous completion
@RequestMapping(value = "/user", method = RequestMethod.GET)
public Callable<String> getUser()
• Servlet 3.0 and 3.1• Spring 3.2
Simply change signature to utilize this:
or Future<T>
or DeferredResult<T>
Link to source: http://docs.spring.io p.17.3.4
![Page 14: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/14.jpg)
14
SLA
def fakeEvent(): Event = { Event("fakeId", System.currentTimeMillis, Array.empty)}
Light operations:
Can be served under load
![Page 15: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/15.jpg)
15
Why this still is not “reactive”?
Because in reality it doesn’t solve the reason of blocking
}
def blockingOp(): Int = {
// JDBC call
// or Remote WS call
// or File IO
// or remote RPC call
![Page 16: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/16.jpg)
16
For example: SpringData-Elastic call
• Intuitive interface• Reduces boilerplate
public interface UserRepository extends CrudRepository<User, Long> {
Long deleteByLastname(String lastname); List<User> removeByLastname(String lastname);
}Link to source: http://docs.spring.io/spring-data/elasticsearch/docs/1.1.0.M1/reference/htmlsingle/
![Page 17: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/17.jpg)
17
“Doubt everything we know” - Descartes
Basic construct is: ElasticsearchTemplate
public <T> Page<T> queryForPage (StringQuery query, …args…, ..args…) { SearchResponse response = repareSearch(query,clazz) .setQuery(query.getSource()) .execute() .actionGet();
return mapper.mapResults(response, clazz, query.getPageable());}
ListenableFuture
Synchronously Awaits(i.e. Blocks!)
Link to source: https://github.com/spring-projects/spring-data-elasticsearch
![Page 18: Not yet reactive but asyncronous mvc](https://reader036.vdocument.in/reader036/viewer/2022062313/55c3c519bb61eb535c8b4606/html5/thumbnails/18.jpg)
18
Conclusion
Always look under the hood