Going Reactive
Mesut Can Gürle
● Extreme Programmer
● Open Source
Enthusiast
● PhD Student
● Devops Turkey, Istanbul
Coders
● #793 @LKD
● @mesutcang
● Jedi
Agenda ❏ Basics
❏ Java toolset
❏ Rx Java
❏ Questions
Reactive
Programming
● A programming paradigm that helps you to
build ‘Reactive Systems’.
● In a reactive world, we can't just wait for a
function result, a network call, or a database
query to return.
● Every moment we wait for something, we
lose the opportunity to do other things in
parallel.
● Reactive programming can be done in
several ways
Akka Actors
Reactive Extensions (Rx) – reactive + functional
Use Cases
● External Service Calls
● Highly Concurrent Message Consumers
● Spreadsheets
● Abstraction Over (A)synchronous Processing
Iterator?
Callback?
Responsive
● Responsiveness is the cornerstone of usability and utility
● Responsiveness means that problems may be detected quickly and dealt
with effectively.
● Responsive systems focus on providing rapid and consistent response
times, establishing reliable upper bounds so they deliver a consistent
quality of service.
● This consistent behaviour in turn simplifies error handling, builds end user
confidence, and encourages further interaction.
Resilient
● The system stays responsive in the face of failure.
● Resilience is achieved by replication, containment, isolation and
delegation. Failures are contained within each component, isolating
components from each other and thereby ensuring that parts of the system
can fail and recover without compromising the system as a whole.
Elastic
● The system stays responsive under varying workload.
● Reactive Systems can react to changes in the input rate by increasing or
decreasing the resources allocated to service these inputs.
● They achieve elasticity in a cost-effective way on commodity hardware and
software platforms.
Message Driven
● Reactive Systems rely on asynchronous message-passing to establish a
boundary between components that ensures loose coupling, isolation and
location transparency.
● Employing explicit message-passing enables load management, elasticity,
and flow control by shaping and monitoring the message queues in the
system and applying back-pressure when necessary.
● Non-blocking communication allows recipients to only consume resources
while active, leading to less system overhead.
Reactive Extensions
● ReactiveX is a library for composing asynchronous and event-based
programs by using observable sequences.
● ‘The Observer pattern done right.’
● Initially implemented for C# by Microsoft, later ported to many languages
by Netflix Rx.
● Rx-Java is the Java implementation.
Before
● Call a method
● Wait for result
● Store the return value from that method in a variable
● Use that variable and its new value to do something
useful
Rx Java
● The Subscribe method connects an Observer to an
Observable
● Once you Subscribe, no need to block the thread
● Values will come to your Observer when they are
ready
Schedulers ● immediate
● newThread
● computaion
● io
subscribeOn - specify the Scheduler on which an
Observable will operate
observeOn - specify the Scheduler on which an
observer will observe this Observable
Reactive programming

Reactive programming

  • 1.
  • 2.
    ● Extreme Programmer ●Open Source Enthusiast ● PhD Student ● Devops Turkey, Istanbul Coders ● #793 @LKD ● @mesutcang ● Jedi
  • 3.
    Agenda ❏ Basics ❏Java toolset ❏ Rx Java ❏ Questions
  • 5.
    Reactive Programming ● A programmingparadigm that helps you to build ‘Reactive Systems’. ● In a reactive world, we can't just wait for a function result, a network call, or a database query to return. ● Every moment we wait for something, we lose the opportunity to do other things in parallel. ● Reactive programming can be done in several ways Akka Actors Reactive Extensions (Rx) – reactive + functional
  • 6.
    Use Cases ● ExternalService Calls ● Highly Concurrent Message Consumers ● Spreadsheets ● Abstraction Over (A)synchronous Processing
  • 7.
  • 9.
  • 12.
    Responsive ● Responsiveness isthe cornerstone of usability and utility ● Responsiveness means that problems may be detected quickly and dealt with effectively. ● Responsive systems focus on providing rapid and consistent response times, establishing reliable upper bounds so they deliver a consistent quality of service. ● This consistent behaviour in turn simplifies error handling, builds end user confidence, and encourages further interaction.
  • 13.
    Resilient ● The systemstays responsive in the face of failure. ● Resilience is achieved by replication, containment, isolation and delegation. Failures are contained within each component, isolating components from each other and thereby ensuring that parts of the system can fail and recover without compromising the system as a whole.
  • 14.
    Elastic ● The systemstays responsive under varying workload. ● Reactive Systems can react to changes in the input rate by increasing or decreasing the resources allocated to service these inputs. ● They achieve elasticity in a cost-effective way on commodity hardware and software platforms.
  • 15.
    Message Driven ● ReactiveSystems rely on asynchronous message-passing to establish a boundary between components that ensures loose coupling, isolation and location transparency. ● Employing explicit message-passing enables load management, elasticity, and flow control by shaping and monitoring the message queues in the system and applying back-pressure when necessary. ● Non-blocking communication allows recipients to only consume resources while active, leading to less system overhead.
  • 16.
    Reactive Extensions ● ReactiveXis a library for composing asynchronous and event-based programs by using observable sequences. ● ‘The Observer pattern done right.’ ● Initially implemented for C# by Microsoft, later ported to many languages by Netflix Rx. ● Rx-Java is the Java implementation.
  • 18.
    Before ● Call amethod ● Wait for result ● Store the return value from that method in a variable ● Use that variable and its new value to do something useful
  • 19.
    Rx Java ● TheSubscribe method connects an Observer to an Observable ● Once you Subscribe, no need to block the thread ● Values will come to your Observer when they are ready
  • 26.
    Schedulers ● immediate ●newThread ● computaion ● io subscribeOn - specify the Scheduler on which an Observable will operate observeOn - specify the Scheduler on which an observer will observe this Observable