not yet reactive but asynchronous mvc -...

14
Not yet reactive but asynchronous MVC [email protected] Link to source 1

Upload: others

Post on 14-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

Not yet reactive but asynchronous MVC

[email protected]

Link to source 1

Page 4: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

Now imagine a service

4

@RestController class EventsController {

}

def fastEvent(): Event = {…}

def blockingOp(): Int = {…}

Page 5: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

Now imagine a service

5

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 6: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

Load test sample

6

Link to source: http://goo.gl/abYAO8

Page 7: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

Side note

7

Link to source: http://gatling.io/

Load test are written in Gatling

Try to simulate C10K with JMeter threaded model, again

Why not Jmeter?

Link to source: https://github.com/alkersan/timebench/blob/master/modules/loadtest/src/test/scala/SpringBootSimulation.scala

Page 8: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

Asynchronous completion

8

@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 9: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

What’s changed under load

9

Link to source: http://goo.gl/hYl0Ie

Page 10: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

SLA

10

def fakeEvent(): Event = { Event("fakeId", System.currentTimeMillis, Array.empty) }

Lightweight operations:

Can be served under load

Page 11: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

Why this still is not “reactive”?

11

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 12: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

For example: SpringData-Elastic call

12

• 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 13: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

“Doubt everything we know” - Descartes

13

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 14: Not yet reactive but asynchronous MVC - JUGjug.ua/wp-content/uploads/2014/12/Not_only_Play_can_be_reactive.pdf · Not yet reactive but asynchronous MVC Dmytro_Aleksandrov@epam.com

Conclusion

14

Always look under the hood