From Streams to
Reactive Streams
Oleg Tsal-Tsalko
JavaDay Kyiv 2017
SOLUTION ARCHITECT AT EPAM SYSTEMS.
PASSIONATE DEVELOPER, SPEAKER,
ACTIVE MEMBER OF KIEV JUG.
PARTICIPATE IN DIFFERENT EDUCATIONAL
INITIATIVES, ENGINEERING EVENTS AND
JCP/ADOPTJSR PROGRAMS.
MEMBER OF ARCHITECTURE EXCELLENCE
INITIATIVE AND ENGINEERING
PRODUCTIVITY GROUPS IN EPAM.
OLEG TSAL-TSALKO
CONFIDENTIAL 3
Agenda
Reactive Manifesto
Reactive Programming
Reactive Streams spec
Project Reactor
CONFIDENTIAL 4
Nowadays challenges
CONFIDENTIAL 5
CONFIDENTIAL 6
CONFIDENTIAL 7
CONFIDENTIAL 8
Non-blocking architecture
• Can’t write imperative code anymore
• Can’t assume single thread of control
• Must deal with async results (listeners/callbacks)
• Everything becomes stream of events
CONFIDENTIAL 9
What is Reactive Programming?
CONFIDENTIAL 10
Concurrent
Programming
Reactive
Programming
CONFIDENTIAL 11
Reactive Use Cases
External Non-blocking Service Calls
Highly Concurrent Message Consumers
Spreadsheets
Abstraction Over (A)synchronous Processing
…
CONFIDENTIAL 12
Existing implementations
CONFIDENTIAL 13
One spec to rule them all
CONFIDENTIAL 14
Reactive Streams spec
Asynchronous stream processing with non-blocking back pressure
The Problem
Handling streams of “live” data
whose volume is not predetermined so that
fast data source does not overwhelm the stream destination
Scope
Minimal set of interfaces, methods and protocols to build
asynchronous streams of data with non-blocking back pressure
CONFIDENTIAL 15
What is back-pressure?
CONFIDENTIAL 16
CONFIDENTIAL 17
Back-pressure
CONFIDENTIAL 18
Reactive Streams API
CONFIDENTIAL 19
Publish-Subscribe Flow
CONFIDENTIAL 20
Difference to CompletableFuture
If you have a reference to the Future,
it means the task processing an asynchronous result is already
executing!
Deferred - nothing happens before the call to subscribe()
Pull - Subscriber pull the next chunk of data when needed
Push - Producer push new data to Consumer according to demand
CompletableFuture is push only model
ReactiveStreams is deferred pull-push model
CONFIDENTIAL 21
Spring Reactor
CONFIDENTIAL 22
Spring Reactor evolution
CONFIDENTIAL 23
Reactor in Spring ecosystem
CONFIDENTIAL 24
Three things you can do with Flux/Mono:
1. operate on it
2. subscribe to it
3. configure it
Simple and clear, isn’t it?
CONFIDENTIAL 25
Fluent API
CONFIDENTIAL 26
CONFIDENTIAL 27
Programming model is very similar
Both heavily use operators chaining and lambdas
Java8 Streams
Reactive
Streams
However purpose is different!!!
Reactive Streams operate on
reactive data
and represent data over time
Java8 streams operate on
collections
and have all or nothing semantics
CONFIDENTIAL 28
Reactive streams lifecycle
• Assembly-time: When you compose your stream
just().subscribeOn().map()
• Subscription-time: When Subscriber subscribes to a stream
and it triggers a "storm" of subscriptions under the hood
• Runtime: When items are generated followed by zero or one
terminal event of error/completion
CONFIDENTIAL 29
Links
Exercises to practice: https://github.com/reactor/lite-rx-api-
hands-on
Examples shown:
https://github.com/olegts/ReactiveStreams/tree/master/src/
test/java/org/reactivestreams/reactor/javaday
Reactive Streams: https://github.com/reactive-
streams/reactive-streams-jvm
Project Reactor: https://projectreactor.io/docs
CONFIDENTIAL 30

From Streams to Reactive Streams