100% found this document useful (1 vote)
359 views101 pages

Reactive Spring

The document discusses building reactive microservices using Spring WebFlux and reactive programming. It introduces reactive programming and its advantages over traditional programming models. It covers writing reactive code using Project Reactor and building reactive services with Spring WebFlux. The target audience is developers interested in learning reactive programming and Spring WebFlux. Source code examples are provided to demonstrate reactive REST services.

Uploaded by

Sunjay Bhalla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
359 views101 pages

Reactive Spring

The document discusses building reactive microservices using Spring WebFlux and reactive programming. It introduces reactive programming and its advantages over traditional programming models. It covers writing reactive code using Project Reactor and building reactive services with Spring WebFlux. The target audience is developers interested in learning reactive programming and Spring WebFlux. Source code examples are provided to demonstrate reactive REST services.

Uploaded by

Sunjay Bhalla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as 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 • Embrace Distributed Systems


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
External
Service

• 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 App 2 {API}

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 App 2 {API}

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 di cult to read and
maintain

• Callbackhell

ffi
Future
Concurrency APIs in Java
Future CompletableFuture

• Released in Java 5
• Released in Java8

• Write Asynchronous code in a functional


• Write Asynchronous Code
style

• Disadvantages: • Easy to compose/combine MultipleFutures

• No easy way to combine the • Disadvantages:

result from multiple futures


• Future that returns many elements

• Eg., CompletableFuture<List<Result> will need


• Future.get()
to wait for the whole collection to built and
readily available

• This is a blocking call • CompletableFuture does not have a handle for


in nite values

fi
Drawbacks of Spring MVC
• Concurrency is limited in Spring MVC

• Blocking code leads to ine cient usage of threads.

• Servlet API at the server level is a blocking one


ffi
Reactive
Is 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 ows as an Event/Message driven stream


fl
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 ows as an Event/Message driven stream

• Functional Style Code

• BackPressure on Data Streams


fl
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

App 2 {API}

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 Speci cation is created by engineers from multiple
organizations:

• Lightbend

• Net ix

• VmWare (Pivotal)
fl
fi
Reactive Streams Specification
• Reactive Streams Speci cation:

• Publisher

• Subscriber

• Subscription

• Processor
fi
Publisher
public interface Publisher<T>

public void subscribe(Subscriber<? super T> s) 1

• 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
onNext( 2 ) 4.1

.
4.n
onNext( 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

APIClient {API}

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

MoviesInfo Name, cast,


year
Service
Annotated Controller

MoviesService Functional Endpoints

MoviesReview Rating, Review


Service

Movies Application using MicroServices Pattern


Annotated Controller

MoviesInfo
Service
Annotated Controller

MoviesService Functional Endpoints

MoviesReview
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

Integration
Controller Service Repository DB
Test
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

Unit Test Controller Service Mock Repository DB


Movies Application using MicroServices Pattern
Annotated Controller

MoviesInfo
Service
Annotated Controller

MoviesService Functional Endpoints

MoviesReview
Service

Integration Tests using Embedded MongoDB

Integration In-Memory
Controller Service Repository DB
Mongo
Test 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

Controller Service Repository DB

Unit Test Controller Mock

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

APIClient {API}

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 di erent events that occurs in

the lifetime of a channel

ff
Channel Lifecycle

1 ChannelUnregistered Channel is Created and its not registered with the Eventloop

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 con gured
is located

Router Handler
fi

Is there an advantage in building


RestFul APIs using Functional Web?
Functional Web
• Bene ts:

• All the RestFul APIs endpoints are con gured in one single le

• Code is lightweight compared to the Controller alternative

• Challenges:

• Need to have knowledge about functional programming

• Bean Validation is di erent in Functional Web

• Exception handling in Functional Web is di erent from the Controller approach


fi
ff
fi
ff
fi
Movies Application using MicroServices Pattern
Annotated Controller

MoviesInfo
Service
Annotated Controller

MoviesService Functional Endpoints

MovieReview
Service

Movies Application using MicroServices Pattern


Annotated Controller

MoviesInfo
Service
Annotated Controller

MoviesService 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 con gured in to the application by Spring Boot


fi
Exceptions
in
Service to Service
Communication

Movies Application using MicroServices Pattern

MoviesInfo
Service

MoviesService

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

MoviesService

MovieReview
Service

Movies Application using MicroServices Pattern

Mock
MoviesInfo
MoviesInfo
Service
Service

MoviesService

Mock
MoviesReview
MovieReview
Service
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

MoviesService

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 backo

• Retry Speci c Exceptions

• Retry only 5xx not 4xx exceptions


fi
ff
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