$30 off During Our Annual Pro Sale. View Details »

Reactive Programming with Spring & Kotlin: Serv...

Reactive Programming with Spring & Kotlin: Server-side Power Unleashed

This presentation aims to delve into the realm of Reactive Programming, elucidating how it can be harnessed by combining it with Spring and Kotlin. We will begin by elucidating the foundational concepts of Reactive Programming and expound on its paramount significance in the realm of modern web applications.

We will demonstrate how you can effectively implement Reactive Programming by leveraging the robust capabilities of Spring Framework and the versatility of Kotlin. You will gain insights into employing tools like WebFlux, Flow, and Coroutines to bolster the responsiveness and scalability of your server-side applications.

Additionally, we will take a deep dive into the merits of Server-Sent Events (SSE) and provide a comprehensive exploration of how to harness them for real-time data communication and updates.

This presentation is designed to be a comprehensive guide, bridging the gap between theory and practical application, to empower you with the knowledge and skills needed to fortify your server-side applications using Reactive Programming with Spring and Kotlin.

Gökhan Ayrancıoğlu

October 09, 2023
Tweet

More Decks by Gökhan Ayrancıoğlu

Other Decks in Technology

Transcript

  1. Kotlin is a great fit for developing server-side applications. Full

    compatibility with existing Java-based technology Server-side Kotlin?
  2. Reactive programming is a software paradigm based on asynchronous data

    streams and event-driven, real-time operations. Reactive Programming
  3. Reactive Terminology Let's not block threads. Non-Blocking Everything is a

    stream in Reactive. Data Stream Processing based on load Backpressure Anything that can be asynchronous should be asynchronous. Async
  4. Imperative Programming Due to blocking, only as many requests can

    be processed as the capacity of the thread pool allows.
  5. ★ Async and non-blocking. ★ Creates data flow through event-driven

    streams ★ Reactive programming != functional programming ★ There's backpressure in data streams ★ Predictable response times ★ Uses system resources much more efficiently. Reactive Programming Depths
  6. — DAVE SYER Reactive is that you can do more

    with less, specifically you can process higher loads with fewer threads.
  7. How does Reactive Programming Works ➔ The core of reactive

    programming is managing data through streams in an event-based way. ➔ When developing an application, you should create data flows for everything. ➔ An event or message is created for each result received from the data source. ➔ When a query is made to the database, the service immediately returns and pushes the data through the stream when it is ready. An event is created for each data item, and the data stream is completed with an onComplete.
  8. And Backpressure "If more load comes than we expect or

    can handle, we may not be able to process it. In case of a sudden increase in requests, there may be server or client crashes.
  9. Tell me about Benchmark. Resource: https://itembase.com/resources/blog/tech/reactive-programming-performance-and-trade-offs/ • Blocking app: default

    number of 200 threads to serve the requests • Reactive app: 1 thread for the server and $CPU_CORES for worker threads, quad-core processor = 4 worker threads to serve requests The Good
  10. The BAD, The UGLY • Learning difficulty • Debugging is

    not very easy • It's not easy to Keep it Simple(KISS) with Reactive programming • Great power, requires great attention. • Testing is not very easy either • Memory intensive •
  11. Types of Software Reactive Programming Can Be Applied to Monitoring,

    Logging Apps Games Social Media Apps Batch Apps IoT Apps
  12. A few Important Points: The Reactive manifesto (see: reactivemanifesto.org) •

    Responsive. • Resilient. • Elastic. • Message driven. Rx - Reactive Extensions: (see: http://reactivex.io/) • Java: RxJava — JavaScript: RxJS — C#: Rx.NET,(Unity): UniRx • Scala: RxScala — Clojure: RxClojure — C++: RxCpp — Lua: RxLua • Go: RxGo — Ruby: Rx.rb — Python: RxPY — Groovy: RxGroovy • Kotlin: RxKotlin — JRuby: RxJRuby — Swift: RxSwift • PHP: RxPHP — Elixir: reaxive — Dart: RxDart …
  13. Reactive Programming Mono Flux More and more.. Understanding and explaining

    reactive programming is much harder than applying it :)
  14. Kotlin – Reactive Programming ➔ Kotlin core reactive support ➔

    Reactive frameworks support (Spring Webflux, Ktor, Quarkus etc.) ➔ Coroutines feature for nonblocking and async processes ➔ Flow API for reactivity with data streams and supports backpressure ➔ Compatible with JVM improvements ➔ Integration with Reactive Extensions (RxJava/RxKotlin)
  15. Mono is used for Publishers that can contain 0 or

    1 event. Flux is used for Publishers that can contain 0..N events. FLUX Mono Spring Webflux Publishers
  16. Controller Layer Functional Endpoint HandlerFunction is a interface for handling

    "ServerRequest" and "ServerResponse" Mono. RouterFunctions for routing
  17. WebClient A sample request using Webclient RestTemplate is Dead, long

    live Webclient! - RestClient is coming soon for sync operations.
  18. R2DBC - Reactive Relational Database Reactiveway for relational databases. -

    Spring R2DBC is not a full ORM like JPA - We can't make relationships like OneToMany directly.
  19. Kotlin Coroutines Coroutines use a lightweight threading (not a thread)

    model, enabling you to pause and resume tasks without blocking threads, making it efficient for handling concurrency and parallelism. Source: https://kotlinlang.org/
  20. In a CoroutineScope, an async operation is performed for a

    suspend function. Firstly, even numbers are processed. Suspend function for coroutine usage.
  21. Reactive Kotlin: Flow API A way to handle asynchronous data

    streams and reactive programming within backpressure support.
  22. Build a flow Shared flow with map Special pancakes using

    transform and filter Consuming flow within collect
  23. every single operator will log detailed enable debug mode for

    that section of the strea consumes error observes all streams signals and traces them into logs
  24. Server-Sent Events It is a technology that enables the sending

    of messages/notifications/events from the server to the client through an HTTP connection.
  25. Server-sent events (SSE) List of pre-defined SSE field names include:

    • event: Event type • data: Event or message object • retry: Field specifying the time to establish a connection after a connection is lost • id: An assigned id for each message
  26. CREDITS: This presentation template was created by Slidesgo, including icons

    by Flaticon, and infographics & images by Freepik Thank You gokhana.medium.com gokhanadev [email protected] gokhana.dev G-khan gokhanadev Gökhan Ayrancıoğlu