Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Survival Tips on Project Reactor and Spring Web...

Survival Tips on Project Reactor and Spring WebFlux

Yuriy Artamonov

October 27, 2024
Tweet

More Decks by Yuriy Artamonov

Other Decks in Programming

Transcript

  1. Plan 1. Why do we use Reactive on JVM 2.

    Existing tools and resources to simplify it 3. How you can improve it for your team 2
  2. Project Reactor Reactor is a fully non-blocking reactive programming foundation

    for the JVM, with efficient demand management. val users: Flux<User> return users .map { it.email } .flatMap { sendEmail(it) } 4
  3. Tip #1 Learn Some Functional Programming 1. Java Stream /

    Kotlin Sequence 2. Optional 3. Try & Either 4. .map, .flatMap, .zip concepts 9
  4. Tip #2 Switch to Kotlin 1. Easier to write tons

    of lambdas 2. Nullability under control 3. Cleaner syntax with less () and ; 4. Simpler DI and data classes 10
  5. Tip #3 Check Operators FAQ and Diagrams Diagrams for streams

    in JavaDoc: projectreactor.io/docs/core/release/reference/#which-operator 13
  6. Tip #4 Enable Tracebacks ▪ Reactor Debug mode ▪ Instrumentation

    for production Maven: io.projectreactor:reactor-tools projectreactor.io/docs/core/release/reference/#reactor-tools-debug Error has been observed at the following site(s): *________Flux.map ⇢ at guide.FakeRepository.findAllUserByName(FakeRepository.java:27) |_ Flux.map ⇢ at guide.FakeRepository.findAllUserByName(FakeRepository.java:28) |_ Flux.filter ⇢ at guide.FakeUtils1.lambda$static$1(FakeUtils1.java:29) |_ Flux.transform ⇢ at guide.GuideExtraTests.withDeepTraceback(GuideExtraTests.java:39) |_ Flux.elapsed ⇢ at guide.FakeUtils2.lambda$static$0(FakeUtils2.java:30) |_ Flux.transform ⇢ at guide.GuideDebuggingExtraTests.withDeepTraceback(GuideExtraTests.java:40) 15
  7. Tip #5 Develop with reactor-debug-agent 1. Add reactor-tools to classpath

    2. Enable ReactorDebugAgent in code or via IDE Settings ReactorDebugAgent.init(); 16
  8. Blocking in Reactive Systems Want to have such a nice

    picture in your VisualVM? Try wrk!
  9. Guess What is Going On 1. Prints “data is :suffix”

    2. Throws exception 3. Does not print anything
  10. Tip #7 Always Return Publisher 1. You must always return

    publisher to the caller 2. But! Keep in mind Fire-and-Forget idiom 21
  11. Tip #9 Do Not Throw Project Reactor will handle throws,

    but throw is harmfull for your system performance! 23
  12. Trap #1 Dangerous Flux.flatMap { } Get many results from

    database and then start network requests to a thirdparty API in parallel for all of them. What can go wrong? 26
  13. Structural Search for Inspections Check code without plugin: ▪ Enable

    Inspections - Structured Search Inspection ▪ Setup search templates Example: @Inject $Field$ 27
  14. Trap #2 Mapping Mono<Void> myService.process("a") // returns Mono<Void> .zipWith(myMethod.process("b"), combinator)

    .subscribe(); See: projectreactor.io/docs/core/release/reference/#faq.monoThen 28
  15. Recommended Talks ▪ Sergei Egorov - Do’s and Don’ts: Avoiding

    First-Time Reactive Programmer Mines ▪ Simon Basle - Flight of the Flux: A Look at Reactor Execution Model ▪ Phil Clay - Avoiding Reactor Meltdown