0% found this document useful (0 votes)
48 views101 pages

36701070-reactive-spring

Uploaded by

Slim Amiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views101 pages

36701070-reactive-spring

Uploaded by

Slim Amiri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 101

Build Reactive MicroServices

using
Spring WebFlux/SpringBoot
Dilip
About Me

• Dilip

• Building Software’s since 2008

• Teaching in UDEMY Since 2016


What’s Covered ?
• Introduction to Reactive Programming

• Advantages of Reactive Programming over traditional programming


models

• Writing Reactive Programming code using Project Reactor

• Introduction Spring WebFlux

• Reactive Services using Spring WebFlux

• Three Reactive Rest Services using Spring WebFlux

• JUnit test cases using JUnit5


Targeted Audience

• Any Developer who is curious to learn about Reactive

Programming

• Any Developer who is interested in learning Spring WebFlux

• Any Developer who is interested in Building Reactive(Non

Blocking) APIs
Source Code
Thank You!
Why Reactive Programming ?
Evolution of Programming
Past (10 -15 ) years ago Current

• Monolith Applications • Microservices Applications

• Deployed in Application Server • Deployed in Cloud Environments


• Does not embrace distributed
systems • Embrace Distributed Systems
Expectations of the Application

• Response times are expected in milliseconds

• No Downtime is expected

• Scale up automatically based on the load


Restful API using Spring Boot/MVC

Spring MVC
App
Tomcat

Dispatcher
Client Servlet Controller Service Dao

DB
Thread
pool API Client API
Ex
S te
er r n
vi a
ce l
• Concurrency is Thread Per Request model
• This style of building APIs are called Blocking APIs
• Wont scale for today’s application needs
Restful API using Spring Boot/MVC

DB
1

Spring MVC 2
{API
App }
Embedded Server 3
{API
}

Latency = Summation of (DB + API + API) response times


Spring MVC Limitations

• Thread pool size of Embedded tomcat in Spring MVC’s is 200

• Can we increase the thread pool size based on the need ?

• Yes, only to a certain limit.

• Let’s say you have a use case to support 10000 concurrent users.

• Can we create a thread pool of size 10000 Threads ?

• No
Thread and its Limitations

• Thread is an expensive resource

• It can easily take up to 1MB of heap space

• More threads means more memory consumption by the thread itself

• Less heap space for actually processing the request


Restful API using Spring Boot/MVC

Spring MVC
App
Tomcat

Dispatcher
Client Servlet Controller Service Dao

DB
Thread
pool
Restful API using Spring Boot/MVC

Make these calls in Parallel


DB
1

Spring MVC 2
{API
App }
Embedded Server 3
{API
}

Latency = Summation of (DB + API + API) response times


Lets explore the asynchrony options
in Java
• Callbacks

• Futures
Callbacks
Callbacks

• Asynchronous methods that accept a callback as a parameter and


invokes it when the blocking call completes.

• Writing code with Callbacks are hard to compose and difficult to read
and maintain

• Callbackhell
Future
Concurrency APIs in Java
Future CompletableFuture

• Released in Java 5 • Released in Java8


• Write Asynchronous code in a
• Write Asynchronous Code functional style
• Disadvantages: • Easy to compose/combine
MultipleFutures
• No easy way to combine • Disadvantages:
the result from multiple
futures • Future that returns many elements
• Eg., CompletableFuture<List<Result> will
• Future.get() need to wait for the whole collection to
built and readily available
• This is a blocking call • CompletableFuture does not have a handle
for infinite values
Drawbacks of Spring MVC

• Concurrency is limited in Spring MVC

• Blocking code leads to inefficient usage of threads.

• Servlet API at the server level is a blocking one


Is
Reactive
there a programming
better option to the
available?
rescue
Summary

• Does this mean we should stop using Spring MVC?

• No

• This still works very well for many use cases


What is Reactive
Programming ?
What is Reactive Programming ?

• Reactive Programming is a new programming paradigm

• Asynchronous and non blocking

• Data flows as an Event/Message driven stream


Reactive Programming

requestForData( )

request(n)

onNext(1) DB
App onNext(2)
..
.
onNext(n)

onComplete( )

• This is not a blocking call anymore


Push Based data streams model
• Calling thread is released to do useful work
What is Reactive Programming ?

• Reactive Programming is a new programming paradigm

• Asynchronous and non blocking

• Data flows as an Event/Message driven stream

• Functional Style Code

• BackPressure on Data Streams


Backpressure

requestForData( )

request(n)

onNext(1)
DB
App onNext(2)
..
.
onNext(n)

onComplete( )

Overwhelm the app with more data


Backpressure

requestForData( )

request(2 )
DB
App onNext(1)

onNext(2)

cancel( )

Backpressure
Push-based data flow model
Push-Pull based data flow
model
When to use Reactive
Programming ?

Use Reactive Programming when there is need


to build and support app that can handle high
load
Reactive App Architecture

Make these calls in nonblocking style


DB
1

{API
App 2
}
Embedded Server 3
{API
}

• Handle request using non blocking style


• Netty is a non blocking Server uses Event Loop Model
• Using Project Reactor for writing non blocking code
• Spring WebFlux uses the Netty and Project Reactor for building non
blocking or reactive APIs
Reactive Streams
Reactive Streams are the
foundation for Reactive
programming.
Reactive Streams

• Reactive Streams Specification is created by engineers from multiple


organizations:

• Lightbend

• Netflix

• VmWare (Pivotal)
Reactive Streams Specification

• Reactive Streams Specification:

• Publisher

• Subscriber

• Subscription

• Processor
Publisher
public interface Publisher<T> {
1
public void subscribe(Subscriber<? super T> s);
}

• Publisher represents the DataSource


• Database
• RemoteService etc.,
Subscriber
public interface Subscriber<T> {

public void onSubscribe(Subscription s); 1


public void onNext(T t); 2
public void onError(Throwable t); 3
public void onComplete(); 4
}

requestForData( )

onNext(1)
DB
App onNext(2)
..
.
onNext(n)

onComplete( )
Subscription
public interface Subscription {

public void request(long n);

public void cancel();


}

requestForData( )

request(2 )
DB
App onNext(1)

onNext(2)

cancel( )

Subscription is the one which connects the app and datasource


Reactive Streams - How it works
together ?
Success Scenario

subscribe( this ) 1
DB
onSubscribe( ) 2
Subscription
request( n ) 3

onNext( 1 ) 4
Subscriber Publisher
4.
onNext( 2 ) 1
..
. 4.
onNext( n ) n

{API}
onComplete( ) 5
Reactive Streams - How it works
together ?
Error/Exception Scenario

subscribe( this ) 1
DB
onSubscribe( ) 2
Subscription

request( n ) 3

Subscriber Publisher

onError( ) 4
{API}

• Exceptions are treated like the data


• The Reactive Stream is dead when an
exception is thrown
Processor
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {

• Processor extends Subscriber and Publisher


• Processor can behave as a Subscriber and
Publisher
• Not really used this on a day to day basis
Flow API

• Release as part of Java 9

• This holds the contract for reactive streams

• No implementation of the Reactive Streams is available as part of

JRE
What is a
Nonblocking or Reactive
RestFul API ?
NonBlocking or Reactive RestFul API
• A Non-Blocking or Reactive RestFul API has the behavior of providing
end to end non-blocking communication between the client and
service

• Non-Blocking or Reactive == Not Blocking the thread


• Thread involved in handling the httprequest and httpresponse is
not blocked at all
• Spring WebFlux is a module that’s going to help us in achieving
the Non-Blocking or Reactive behavior
NonBlocking or Reactive API using Spring
WebFlux

Spring WebFlux Project Reactor


App 2
1

Dao

Client Netty Controller Service

{API
APIClient
}
Options for
Reactive RestFul API
Using
Spring WebFlux
Setting up the Project
For this
Course
Section Overview
Section Overview

• Explore Project Reactor


Spring WebFlux

Spring WebFlux

Annotated Functional
Controllers Endpoints
What are we going to build in
this course ?
Movies Application using MicroServices
Pattern
Annotated Controller

Name, cast,
MoviesInfo
year
Service
Annotated Controller

MoviesServic
e Functional Endpoints

MoviesRevie
Rating, Review
w
Service
Movies Application using MicroServices
Pattern
Annotated Controller

MoviesInfo
Service
Annotated Controller

MoviesServic
e Functional Endpoints

MoviesRevie
w
Service
Streaming Endpoint
Using
Spring Webflux
Streaming Endpoint

• Streaming Endpoint is a kind of Endpoint which continuously sends

updates to the clients as the new data arrives

• This concept is similar to Server Sent Events(SSE)

• Easy to implement in Spring WebFlux

• Examples : Stock Tickers, Realtime updates of Sports Events


Automated Testing
Using JUnit5
Automated Tests

• Automated Tests plays a vital role in delivering quality Software

• Two types of Automated Tests:

• Integration Tests

• Unit Tests
Integration Tests

• Integration test is a kind of test which actually test the

application end to end


1 2 3

Integrati Controlle Repositor


Service DB
onTest r y
Unit Tests

• Unit test is a kind of test which tests only the class and method of

interest and mocks the next layer of the code

Controlle Repositor
Unit Test Service Mock DB
r y
Movies Application using MicroServices
Pattern
Annotated Controller

MoviesInfo
Service
Annotated Controller

MoviesServic
e Functional Endpoints

MoviesRevie
w
Service
Integration Tests using Embedded
MongoDB

In-
Memory
Integrati Controlle Repositor
Service DB
Mongo
onTest r y DB

testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
Unit Tests

• Unit test is a kind of test which tests only the class and method of

interest and mocks the next layer of the code


Controlle Repositor
Service DB
r y

Controlle
Unit Test Mock
r

Mockito
Benefits of Unit Tests

• Unit Tests are faster compared to Integration Tests

• Unit Tests are handy for performing Bean Validations


Spring WebFlux Test has an
annotation named “@WebFlux”
test
Using ResponseEntity
In
Spring WebFlux
How Netty Works
with
Spring WebFlux ?
NonBlocking or Reactive API using Spring
WebFlux

Spring WebFlux
App 2
1

Dao

Client Netty Controller Service

{API
APIClient
}
How does
Netty
handle the request?
Netty (WebFlux’s Default Server)

Spring WebFlux App

Channel

Client

Netty

• Channel represents an open connection


between the client and server
• Request and Response is sent via the
channel
Channel

• Channel has ChannelHandlers


• Accepting the client connection
• Reading the data as bytes from the network to a Java
Object(Transformation)
• Writing the data back to the client
• This is all taken care for us by Spring WebFlux
• As a developer, we just focus on writing the application related code
Channel and EventLoop
• Netty , uses EventLoop model to handle the connections in a
nonblocking fashion

• An EventLoop is powered by one single thread

• NodeJs uses the same pattern.


• Node js has just one thread/one eventloop to handle client requests

• Number of Eventloops to handle the request is equal to no of cores in


your machine
• Eventloops are part of the EventLoopGroup
EventLoop

Req1
Req2

Event Queue Loop


How Channel and EventLoop linked ?

• Any time a channel is created it gets assigned to an EventLoop

• This EventLoop is responsible for handling the different events that

occurs in the lifetime of a channel


Channel Lifecycle

ChannelUnregister
1 Channel is Created and its not registered with the Eventloop
ed

2 ChannelRegistered Channel is registered with the Eventloop

3 ChannelActive Channel is active and its now possible to send and receive the data

4 ChannelInActive Channel is not connected to the client anymore and ready to be closed

All these Lifecycle changes are treated as events


How Netty handles the request ?
• Netty had two EventloopGroups
• One to just accept connections Spring WebFlux App

• Other one to handle them


Controller Service Dao

Client Netty

Req1 Thread1

Response

Event Queue Loop


Netty in Action
Functional Web
In
Spring WebFlux
Functional Web
• This is an alternative programming model for building RESTFUL APIs
in Spring WebFlux
• Functional web module uses the functional programming aspects:
• Lambdas
• Method References
• Functional Interfaces
Functional Web

Code to handle the request


Rest Endpoints are configured
is located

Router Handler
Is there an advantage in building
RestFul APIs using Functional
Web?
Functional Web
• Benefits:

• All the RestFul APIs endpoints are configured in one single file

• Code is lightweight compared to the Controller alternative

• Challenges:

• Need to have knowledge about functional programming

• Bean Validation is different in Functional Web

• Exception handling in Functional Web is different from the Controller approach


Movies Application using MicroServices
Pattern
Annotated Controller

MoviesInfo
Service
Annotated Controller

MoviesServic
e Functional Endpoints

MovieReview
Service
Movies Application using MicroServices
Pattern
Annotated Controller

MoviesInfo
Service
Annotated Controller

MoviesServic
e Functional Endpoints

MovieReview
WebClient Service
Webclient
WebClient

• It is a reactive non-blocking Rest Client

• It uses a functional style API

• It allows the application to interact with other services in a non-

blocking fashion

• Its auto configured in to the application by Spring Boot


Exceptions
in
Service to Service
Communication
Movies Application using MicroServices
Pattern

MoviesInfo
Service

MoviesServic
e

MovieReview
Service
Http Failures

• Http Failure falls in to two categories:


• 4xx - Client Error
• 400(Bad Request), 404 (Not Found) and etc.,
• 5xx - Server Error
• 500(Internal Server Error), 503 (Service Unavailable) and etc.,
Introduction to Wiremock
Movies Application using MicroServices
Pattern

MoviesInfo
Service

MoviesServic
e

MovieReview
Service
Movies Application using MicroServices
Pattern

Mock
MoviesInfo
MoviesInfo
Service
Service

MoviesServic
e

MoviesRevie
Mock
w
MovieReview
Service

Wiremock to the Rescue


Benefits of WireMock
• Easy to test the success scenarios (2xx)
• Test the contract
• Serialization/Deserialization
• Easy to simulate error scenarios
• 4xx
• 5xx
• SocketTimeout Exceptions and more..,
Why Retry failed HTTP calls ?
Movies Application using MicroServices
Pattern

MoviesInfo
Service

MoviesServic
e

MovieReview
Service

• Intermittent Network Issues


• Slow Network
• External Service is down
Handle Network Errors

• Retrying Failed Calls

• Retry the failed call N number of times before giving up

• Retry the failed call with a backoff

• Retry Specific Exceptions

• Retry only 5xx not 4xx exceptions


Server Sent Events (SSE)
Sever Sent Events

Server
Client
(API)

• Uber App - Realtime


updates of the driver
location
• Data is sent in the form of events
• Dominos, DoorDash etc. • Its Unidirectional once the
connection is established between
the client and server

• Long live client connections


Publish new MovieInfo as a
ServerSentEvent(SSE)
Sinks

You might also like