OBJECTIVE • Understand the best practices for multi-thread app • Understand a paradigm shift in daily coding • Learn how Spring Framework is going to apply these solutions 2
AGENDA • Best practice to code multi-thread app, and in reality • You are not coding a procedure! • Project Reactor the latest approach • Can we use Project Reactor in production? 4
WHY MULTI-THREAD APP IS NECESSARY • Current architecture of computer: bottleneck exists in CPU and I/O • One machine has many CPU cores • Network is enough fast • Compute & store in parallel for better throughput 6
IN REALITY… • We need state! • We everyday persist some onto database • Cache is necessary but it’s also a kind of state • Ownership needs support from system 9
IN REALITY… • Immutable data is hard to handle • Do we need Builder & copy constructor everywhere? • Java’s clone() is almost broken (see EffectiveJava) • Java has no Object.assign() in JavaScript • Some framework expects that data is not immutable (!!??) 10
IN REALITY… • Non-blocking I/O makes callback hell • Callable makes deep nest, lambda is not solution for it • CompletableFuture is better solution, have a try with it • similar with Promise in JavaScript • It works with @Async in Spring Framework 11
CONCLUSION • We need to consider how to handle necessary state and mutability, with considering better async method handling. • To consider them simply, we need a paradigm shift in your mind. 12
WHAT YOUR PROGRAM IS? • We learned that program runs from top to bottom in your editor • in context of structured programming just has sequence, selection, and repetition 14
SEQUENCIAL PROCEDURE example: A traditional web app that displays post and widget 15 load user load post combine data persist combined data load widget render
SEQUENCIAL PROCEDURE example: A traditional web app that displays post and widget 16 load user load post combine data persist combined data load widget render Why we don’t start loading before persisting combined data? why don’t we load user and post in parallel?
HOW ABOUT MULTI-THREAD APP? • You pass value/message to another thread, • You wait until other threads finish their task, and • You zip (combine) them to start a task in another thread. • You may need to care which thread we run each task (e.g. UI thread) 17
HOW TO CODE A GRAPH? • Wire relation between tasks (lambda or method) • Keep tasks stateless, idempotence and thread safe • Prefer CompletableFuture than Callable • Encapsulate state/mutability in each task • disclose immutable data to others 20
NOT SO PRODUCTIVE… • How to specify the thread to run tasks? • How to handle the exception in tasks? • How to make implementation testable? • How to cache, concat, filter, group, retry and throttle data? 22
CONCLUSION • Multi-thread app is not a procedure but a graph • Wire tasks that encapsulate mutability in it • JDK8 provides CompletableFuture and it fulfills a part of requirements 23
EXISTING APPROACH • We have many approaches like… • Rx.NET, Akka-streams, RxJava and Project Reactor (ref for diff) • JDK9 Flow could be standard in future • Project Reactor is most flesh and well-integrated with Spring 25
BEFORE DIVING REACTOR… • Summarize existing problems: • How to specify the thread to run tasks? • How to handle the exception in tasks? • How to make implementation testable? • How to cache, concat, filter, group, retry and throttle data? 26
THREAD HANDLING • Scheduler helps you to decide which thread to run tasks • Schedulers.parallel() for non-blocking tasks, to use fixed size thread pool. • Schedulers.elastic() for blocking tasks (JDBC etc.), to launch new thread if necessary. 27
THREAD HANDLING • Set scheduler by Flux#publishOn() and Flux#subscribeOn() 29 published on parallel-1 published on parallel-1 published on parallel-1 published on parallel-1 published on parallel-1 : :
THREAD HANDLING • To run task in multiple thread, use Flux#parallel() 31 run on parallel-1 run on parallel-2 run on parallel-3 run on parallel-3 run on parallel-4 : :
BEFORE DIVING REACTOR… • Summarize existing problems: • How to specify the thread to run tasks? • How to handle the exception in tasks? • How to make implementation testable? • How to cache, concat, filter, group, retry and throttle data? 32
BEFORE DIVING REACTOR… • Summarize existing problems: • How to specify the thread to run tasks? • How to handle the exception in tasks? • How to make implementation testable? • How to cache, concat, filter, group, retry and throttle data? 35
BEFORE DIVING REACTOR… • Summarize existing problems: • How to specify the thread to run tasks? • How to handle the exception in tasks? • How to make implementation testable? • How to cache, concat, filter, group, retry and throttle data? 38
PUBLISHERS PROVIDES CONVENIENCE WAY • cache() to reuse emitted signals for further Subscribers • concat() Subscribers to make single subscriber • filter() source value before emit it (same with Stream API) • groupBy() some key, to make multiple publishers (similar with groupingBy collector) • retry() subscribe when subscribers throw error • for throttling, sampleTimeout() could be help 39
PUBLISHERS PROVIDES CONVENIENCE WAY • We cannot use try-with-resources to close resource • because finally block will be executed before resource is used at timing to subscribe • use Flux.using() instead 40
KNOWN LIMITATION OF WEBFLUX • Community still needs time to support webflux • Actuator (monitoring) : done • Springfox (Swagger): not yet • We have sample project depending on snapshot • Validation: not yet for functional endpoint (or apply hack like this) 44
CLI APPLICATION • Simply use Project Reactor on spring-boot • My sandbox project: DependJ • CommandLineRunner helps you to touch raw command line parameter • ApplicationRunner helps you to handle command line parameter • You can simplify code to run tasks in parallel 45
GUI APPLICATION • Use project-reactor with reactor-extra on spring-boot • reactor-extra provides schedulers for event dispatch thread • like RxJava for Android app, we can use project reactor to separate backend computation and frontend operation 46
CHANGE MIND TO CODE A GRAPH • To code multi-thread app, • Encapsulate mutability into task, • Wire tasks by Project Reactor, then • Your app will be high-performance, non-blocking and testable! 48
WHAT REACTIVE PROGRAMMING IS? • Reactive Programming is a paradigm focuses on dataflow, and what I explained partially in this slide • Spread sheet like Excel provides reactive feature (Wikipedia) • “=B1+C1” will be updated when B1 and/or C1 is changed 51
THIS PARADIGM IS NOT ONLY FOR MULTI-THREAD APP • We can apply it to MSA & distributed service • Project Reactor provides backpressure • Reactive Systems (that can be achieved by Reactive Streams) can make distributed system responsive, elastic, resilient and message driven. 52