1 of 45

Bootiful RSocket with Coroutines

github.com/renatomrcosta

@renatomrcosta

2 of 45

Bootiful RSocket with Coroutines

  • Intro
  • Spring
  • Kotlin Coroutines
  • Why Reactive?
  • RSocket
  • Demo

3 of 45

Intro

4 of 45

The goal for today

  • Understand the tools available:
    • Introduce the Concepts we will explore;

  • Make deliberate choices:
    • Let’s look at our options, and understand if our technological choices fit the problem we are trying to solve;

  • Be Productive:
    • Let’s code a small demo.

5 of 45

Spring

6 of 45

Spring Boot

7 of 45

Coroutines

8 of 45

Coroutines

9 of 45

10 of 45

Why are Coroutines?

  • It is still important to care about individual blocking of Threads even when you can use many of them:
    • UI Threads
    • Process intensive operations that may overload the Threads pools and make the entire thing go unresponsive.

  • In this interconnected world of services, waiting efficiently can be more important than raw computing.

11 of 45

What are Coroutines?

  • They are devices that allow the non-blocking execution of code.
  • This code, in turn, can be suspended and later resumed.
  • In Kotlin, they are essentially State Machine Objects

12 of 45

What are (Kotlin) Coroutines?

  • A library provided that aims to give tools to help developers to write non-blocking code with coroutine primitives;
  • The three main aspects (in my opinion) are:
    • Providing an inexpensive substitute for threads, both in terms of application resources and developer mental model;
      • Threads are expensive, whereas Coroutines are very lightweight.
    • Helping to simplify code:
      • Avoiding things as callback hell / operator hell code when using Callbacks or typical Futures / Promise library implementations;
    • Helping to make it EASIER to write idiomatic code that can behave asynchronously.

13 of 45

Coroutines Sample

14 of 45

Why Reactive?

15 of 45

Servlet Based Approach

Requests

Endpoint /

Servlet

Request #1

Request #2

Request #3

Thread #1

Thread #2

Thread #3

16 of 45

Spring Web - Controller Sample

17 of 45

Spring Web - Controller Sample

18 of 45

Spring Web - Reference

  • Spring Web Reference Docs

19 of 45

Servlet Based Approach

  • Do you need asynchronous or reactive programming?
  • Do you need to scale sensibly, or handle bursts of requests?
  • Do you require backpressure support?

  • Spring Web
    • Spring Data
      • JPA
      • JDBC
      • Redis
      • Mongo
      • ...

NO

20 of 45

Backpressure

  • “Resistance or force opposing the desired flow of data through software” - Jay Phelps

  • Classic Example: Reading from a Hard Drive
    • Read Speeds: 150MB/s
    • Write Speeds: 100MB/s
    • 50MB/s in the buffer!

21 of 45

Reactive approach

Requests

Request #1

Response (Immediate) #1

Request #2

Response (Immediate) #2

22 of 45

Spring Webflux - Controller Sample

23 of 45

Spring Webflux - Controller Sample

24 of 45

Spring Webflux - Controller Sample

25 of 45

Spring Webflux - Controller Sample

26 of 45

Reactive approach

  • Do you need asynchronous or reactive programming?
  • Do you need to scale sensibly, or handle bursts of requests?
  • Do you require backpressure support?

  • Spring Webflux
    • Spring Data Reactive
      • R2DBC
      • Redis
      • Mongo
    • Reactor / Coroutines
  • RSocket

Yes

27 of 45

What is RSocket then?

28 of 45

RSocket

  • A protocol that enables high-performance, fault-tolerant messaging over a multiplexed, duplex connection.
  • Is Transport, Message Format, and Language agnostic
  • Built on top of the Reactive Streams specification

29 of 45

Multiplexed

  • Multiple requests and responses in the same connection

30 of 45

Duplex

  • Requester / Receiver instead of Client / Server

31 of 45

Agnostic

  • Transport: TCP, Websocket, HTTP/2, Aeron…

  • Client Implementation: Kotlin, C++, Java, Javascript, .NET, Go, Python

  • Payload: Binary (so it can essentially be anything: CBOR, Protobuf, JSON, XML)

32 of 45

Interaction Models

33 of 45

Request / Response

  • Single request that may contain a payload, that may yield a response
  • Maps to the normal request / response semantics in HTTP / REST

34 of 45

Fire-and-Forget

  • A Request that may contain a payload, but does not wait for a response

35 of 45

Request / Stream

  • The requester sends a single request and the responder returns a stream of results.

36 of 45

Request / Channel

  • A requester may send multiple values and the responder may return multiple values
  • Bi-directional stream of messages

37 of 45

RSocket Server

38 of 45

The Spring Boot approach

39 of 45

Spring Boot + RSocket

40 of 45

Spring Boot + RSocket

41 of 45

Demo Time

42 of 45

References - Coroutines

43 of 45

References - RSocket / Spring

44 of 45

Questions

45 of 45

Thank you!

github.com/renatomrcosta

@renatomrcosta