Reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. With this paradigm it is possible to express static (e.g., arrays) or dynamic (e.g., event emitters) data streams with ease, and also communicate that an inferred dependency within the associated execution model exists, which facilitates the automatic propagation of the changed data flow.
Is too generic and theoretical as usual.
Stackoverflow's canonical answer is obviously not suitable for newcomers.
https://stackoverflow.com/questions/1028250/what-is-functional-reactive-programming
Reactive Manifesto sounds like the kind of thing you show to your project manager or the businessmen at your company.
https://www.reactivemanifesto.org/
Microsoft's Rx terminology is so heavy and Microsoftish that most of us are left confused.
The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators.
Rx = Observables + LINQ + Schedulers
https://archive.codeplex.com/?p=rx
Terms like "reactive" and "propagation of change" don't convey anything specifically different to what your typical MV* and favorite language already does.
Of course my framework views react to the models. Of course change is propagated. If it wouldn't, nothing would be rendered.
Reactive programming is programming with asynchronous data streams.
It's not new. Event buses or click events in your projects are an asynchronous event stream.
Reactive is that idea on steroids.
Create streams. It's cheap. Anything can be source: var, list, input, caches, etc. On top is toolbox with stream manipulations: combine, create, filter, map and other operations.
a = b + c
change b:
b = 2
a stays the same until:
a = b + c
change b:
b = 2
a automatically recalculated
A stream is a sequence of ongoing events ordered in time.
--a---b-c---d---X---|->
a, b, c, d are emitted values X is an error | is the 'completed' signal ---> is the timeline
It can emit three different things:
- value
- error
- completed signal
We capture these emitted events only asynchronously, by defining a function that will execute when a value is emitted, another function when an error is emitted, and another function when 'completed' is emitted.
Subscribing is the "listening" to the stream.
Observers are the functions we are defining.
Subject/Observable is the stream is being observed.
It's hard to reason about reactive programming.
Why RP? Why RxJava? Where is Android?
Why should we learn this?
we are in 19th century
horses are main transpotation
somebody from future shows you a car
how to use it?
driving signs and conventions
learning and exams
parallel parking
roads
you seat at the car and nothing happens
You love horses
conquer a whole continent
nothing bad about horses
but cars are better
Resource | 10 years ago | now |
---|---|---|
Server nodes | 10's | 1000's |
Response times | seconds | milliseconds |
Maintenance downtimes | hours | none |
Data volume | GBs | TBs -> PBs |
Clients | 1-2 | up to 10's |
Technologies count is constantly increasing. Learning curve is high. Complexity is proportional to software size.
- Software development
- Hardware
- Maintenance
Erik Meijer https://en.wikipedia.org/wiki/Erik_Meijer_(computer_scientist), reactive programming author in Cloud Programmability Team in Microsoft.
David Karnok, Research Assistant at MTA SZTAKI (Hungary, Budapest) https://www.linkedin.com/in/david-karnok-725b3189/
He started in 2010 and made reactive programming in java 2 years before Netflix.
In 2011 published reactive4java https://github.com/akarnokd/reactive4java
In 2013 he merged efforts with Netflix to create RxJava.
Now RxJava counts more than 5k commits since then.
Ben Christensen (http://benjchristensen.com/), one of men behind Netflix API, RxJava and more, now a Facebook engineer.
Jonas Bonér (http://jonasboner.com/) and Roland Kuhn (https://rolandkuhn.com/) ‒ creators of Akka (Lightbend) and Reactive Manifesto authors.
Stephane Maldini (https://github.com/smaldini), project Reactor lead, prinipal engineer at Pivotal.
André Staltz (https://staltz.com/), JavaScript expert and reactive programming evangelist.
Martin Odersky (http://lampwww.epfl.ch/~odersky/), creator of Scala.
Venkat Subramaniam (https://agiledeveloper.com/aboutus.html), java expert and tech evangelist.
Java <9 CompletableFuture / Java 9 http://www.reactive-streams.org/
RxJava http://reactivex.io/
Reactor https://projectreactor.io/
Coroutines and channels in Kotlin https://github.com/Kotlin/kotlinx.coroutines
akka in Scala https://doc.akka.io/docs/akka/current/guide/actors-motivation.html
Targets older Java version
A mature and solid library
https://en.wikipedia.org/wiki/Iterator_pattern
- the elements of an aggregate object should be accessed and traversed without exposing its representation (data structures)
- new traversal operations should be defined for an aggregate object without changing its interface
- define a separate (iterator) object that encapsulates accessing and traversing an aggregate object
- clients use an iterator to access and traverse an aggregate without knowing its representation (data structures)
https://en.wikipedia.org/wiki/Observer_pattern
- a one-to-many dependency between objects should be defined without making the objects tightly coupled
- it should be ensured that when one object changes state an open-ended number of dependent objects are updated automatically
- it should be possible that one object can notify an open-ended number of other objects
- define Subject and Observer objects
- so that when a subject changes state, all registered observers are notified and updated automatically
Just a stream of data events, but made right
- push vs pull (backpressure)
- async vs sync
- concurrency and parallelism (onNext(), onCompleted(), onError() cannot be emmited concurrently, aka thread-safe)
Why not just allow onNext() to be invoked concurrently?
- defensive code to check threads
- some operations has to be sync (scan/reduce) to accumulate data
- performance
- lazy vs eager
- subscription, not construction starts work
- observables can be reused
- duality
An Rx Observable is the async "dual" of an Iterable
- cardinality
The Observable supports asynchronously pushing multiple values
/ | One | Many |
---|---|---|
synchronous | T getData() | Iterable getData() |
asynchronous | throws Exception | Observable getData() |
asynchronous
values
events
push
functional reactive
lambdas
closures
pure
composable
Functional code is idempotent, depends only on arguments.
Pure functions has no side-effects (no state).
https://en.wikipedia.org/wiki/Reactive_programming
- message-driven (react to events / event-driven)
- elastic (react to load / scalable)
- resilient (react to failures)
- responsive (react to users)
responsive
↗ ↖
scalable ↑ resilient
↖ ↗
message-driven
Reactive Manifesto https://www.reactivemanifesto.org/
Reactive Manifesto 2.0 https://www.lightbend.com/blog/reactive-manifesto-20
RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.
It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.
Source: https://github.com/ReactiveX/RxJava
An Observable is the asynchronous "dual" to the synchronous/pull Iterable
event | Iterable (pull) | Observable |
---|---|---|
retrieve data | T next() | onNext(T) |
discover error | throws Exception | onError(Exception) |
complete | returns | onCompleted() |
Completable (0 emits)
Single (1 emit)
Observable/Flowable (many emits)
Rx makes asynchronous operations easier
https://gist.github.com/benjchristensen/4670979
https://gist.github.com/benjchristensen/4671081
Rx avoids callback hell
https://gist.github.com/benjchristensen/4677544
How to implement it in Rx
easy switching back and forth between threads
http://reactivex.io/documentation/operators.html#tree
Create
Just
From
Interval
Timer
Range
Filter
Distinct
Skip
Debounce
Throttle
http://demo.nimius.net/debounce_throttle/
Map
Buffer
FlatMap
CombineLatest
Merge
Zip
Concat
Contains
Reduce
Sync and async work
Easy thread switching
Standard schedulers
- computation
- io
- from(Executor executor)
- single
- trampoline (fifo)
AndroidSchedulers
- mainThread
PublishSubject
BehaviourSubject
ReplaySubject
AsyncSubject
Flowable
BackpressureStrategy
- buffer
- drop
- latest
https://medium.com/over-engineering/rxify-exponential-backoff-on-retry-48bb66912391
Detecting and testing stalled streams https://www.nurkiewicz.com/2017/09/detecting-and-testing-stalled-stream.html Fixed-rate vs. fixed-delay https://www.nurkiewicz.com/2017/09/fixed-rate-vs-fixed-delay-rxjava-faq.html
Managing State with RxJava, Jake Wharton https://www.youtube.com/watch?v=0IKHxjkgop4
v1 vs v2
Tomcat vs Rx performance measures
Netflix point by Tomasz Nurkiewicz (backend)
In October 2018 15% of world traffic is Netflix.
Netflix point.
Little's Law https://en.wikipedia.org/wiki/Little%27s_law
L = λ ⋅ W
Tomcat, 100 threads (L), 100 ms/request (W), λ - ?
λ = 1k requests/second (e.g. on a laptop)
L — среднее по времени число запросов в рассматриваемой части системы [шт.],
W — среднее время, за которое запросы проходят через данную часть системы [с],
λ — скорость поступления запросов в систему [шт./с]
https://habr.com/ru/company/yandex/blog/431650/ - latency home reading with Little's law explanation
Have you heard about 'space-time trade off'? (caching)
What about 'human-hardware trade off'?
Usually, to the left of Netflix point, you want to spend as least as possible on development understanding that you could make balance on pure software buying more servers to solve problems.
But in some cases, to the rigth of Netflix point, humans are more precious than hardware, but in scale you save on hardware more.
It's almost the same, but without hardware.
Spend less on development and you get difficult to maintain products.
Spend more and you get less difficulties to maintain.
May you live in interesting times (Chinese curse)
Ubiquitous language (https://martinfowler.com/bliki/UbiquitousLanguage.html)
What is a universal measure of a code quality?
- simple (simple is a matter of taste and expericence)
- tested (crappy code and poor tests)
- open/closed
- SOLID
- high cohesion
- low coupling
- cyclomatic complexity
- DRY ...
boring
I don't care about language, framework, and particularly reactive library
Who is 10x developer?
Who enables 10 other developers
It takes some time to dive in
Stacktraces are meaningless (no context)
It is difficult to follow a request as events and callbacks are processed
... unhandled exceptions, and incorrectly handled state changes ... These types of issues have proven to be quite difficult to debug
Exceptions from hell (NPE)
Timeout exceptions (with no hint where it happend)
Order is no longer guaranteed
Backpressure is difficult
Everyone makes own implementation and terms
https://en.wikipedia.org/wiki/Reactive_programming https://stackoverflow.com/questions/1028250/what-is-functional-reactive-programming https://www.reactivemanifesto.org/ Reactive Manifesto 2.0 https://www.lightbend.com/blog/reactive-manifesto-20 https://archive.codeplex.com/?p=rx The introduction to Reactive Programming you've been missing, Andre Medeiros (alias "Andre Staltz"), 2014, https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 Ubiquitous language, Eric Evans (https://martinfowler.com/bliki/UbiquitousLanguage.html)
https://en.wikipedia.org/wiki/Observer_pattern https://en.wikipedia.org/wiki/Iterator_pattern
Erik Meijer https://en.wikipedia.org/wiki/Erik_Meijer_(computer_scientist) Jonas Bonér http://jonasboner.com/ Roland Kuhn https://rolandkuhn.com/ Martin Odersky http://lampwww.epfl.ch/~odersky/ Stephane Maldini https://github.com/smaldini André Staltz https://staltz.com/ Venkat Subramaniam https://agiledeveloper.com/aboutus.html
Podcast with David Karnok https://github.com/artem-zinnatullin/TheContext-Podcast/blob/master/show_notes/Episode_3_Part_1.md David Karnok reactive4Java repo https://github.com/akarnokd/reactive4java https://projectreactor.io/ http://www.reactive-streams.org/ http://reactivex.io/ https://github.com/Kotlin/kotlinx.coroutines https://doc.akka.io/docs/akka/current/guide/actors-motivation.html https://github.com/ReactiveX/RxJava
Netflix architecture https://medium.com/netflix-techblog/optimizing-the-netflix-api-5c9ac715cf19 Netflix embrace concurrency https://medium.com/netflix-techblog/reactive-programming-in-the-netflix-api-with-rxjava-7811c3a1496a
Some collection of info around RxJava https://github.com/xiaomeixw/NoRxJava
Reactive Streams with Rx, Ben Christensen https://www.youtube.com/watch?v=g-ixzOcMDF4 http://benjchristensen.com/ https://speakerdeck.com/benjchristensen/reactive-streams-with-rx-at-javaone-2014 https://gist.github.com/benjchristensen/4670979 https://gist.github.com/benjchristensen/4671081 https://gist.github.com/benjchristensen/4677544 https://stackoverflow.com/questions/28402376/how-to-compose-observables-to-avoid-the-given-nested-and-dependent-callbacks https://github.com/Netflix/ReactiveLab/blob/952362b89a4d4115ae0eecf0e73f273ecb27ba98/reactive-lab-gateway/src/main/java/io/reactivex/lab/gateway/routes/RouteForDeviceHome.java
Cycle.js and functional reactive user interfaces, Andre Staltz, https://youtu.be/uNZnftSksYg?t=545 What is reactive programming, Martin Odersky https://www.youtube.com/watch?v=7D9QfMj_KwI Reactive programming: lessons learned by Tomasz Nurkiewicz, https://www.youtube.com/watch?v=5TJiTSWktLU Exploring RxJava 2 for Android, Jake Wharton https://www.youtube.com/watch?v=htIXKI5gOQU Managing State with RxJava, Jake Wharton https://www.youtube.com/watch?v=0IKHxjkgop4 Java Streams vs Reactive Streams: Which, When, How, and Why? by Venkat Subramaniam https://www.youtube.com/watch?v=kG2SEcl1aMM Reactive Programming in Java by Venkat Subramaniam https://www.youtube.com/watch?v=f3acAsSZPhU RxJava доставляет, Artem Zinnatullin https://www.youtube.com/watch?v=3jdvLrYZfB4
Latency explained with Little's Law (ru) https://habr.com/ru/company/yandex/blog/431650/ Tomcat vs rx performance measures https://speakerdeck.com/benjchristensen/applying-reactive-programming-with-rxjava-at-goto-chicago-2015
Simple RxJava examples https://gist.github.com/cesarferreira/510aa2456dc0879f559f
Exponential backoff https://medium.com/over-engineering/rxify-exponential-backoff-on-retry-48bb66912391
Detecting and testing stalled streams https://www.nurkiewicz.com/2017/09/detecting-and-testing-stalled-stream.html Fixed-rate vs. fixed-delay https://www.nurkiewicz.com/2017/09/fixed-rate-vs-fixed-delay-rxjava-faq.html
debounce vs throttle http://demo.nimius.net/debounce_throttle/