Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Programming

Avatar for Mike Farad Mike Farad
November 03, 2020
81

Reactive Programming

An overview of reactive systems and how reactive programming works

Avatar for Mike Farad

Mike Farad

November 03, 2020

Transcript

  1. What is it ? There has been a lot of

    buzz about reactive systems over the past couple of years. Along with the buzz comes the collection of relevant keyword salads like reactive streams, reactive extensions, reactive programming, functional reactive programming, etc. which we’ll look at in a moment
  2. Reactive System - Reactive systems are an architectural style that

    allows multiple individual applications to blend into one unit, reacting to their environment, while staying aware of each other. - They are Capable of responding to queries very quickly and this even in case of failure or overload.
  3. How this is achieved Reactive Systems Manifesto Responsive - This

    type of system is able to ensure a constant quality of service, based on minimum and consistent response times. Resilient - The system remains available in the event of an error . Elastic - The system remains available regardless of the workload. Message Driven - The system uses message passing asynchronous between its components to ensure low coupling.
  4. Why Reactive Systems? Have you ever thought about how well-known

    social networks, large banking systems, and popular trading platforms are implemented? Why do they quickly and reliably cope with a large number of requests to the system? How is it that they are able to: - Manage breakdowns and relies - Manage load peaks - Able to send, receive, and forward messages in an unreliable network
  5. How to Build a Reactive System Message Driven Approach -

    All components interact through messages sent and received asynchronously which creates a temporal boundary between the components, making decoupling possible over time (allowing competition) and space (allowing for distribution and mobility) • Elasticity - Decoupling in space (Load Balancing Strategy) and at peak load more consumer instances are launched • Resilience - Failure is handle locally (Replication) Upon Failure Message may be processed by another • Responsive - Achieved by having both Elasticity and Resilience
  6. Reactive Programming: Introduction - It is a programming paradigm for

    building reactive systems that emphasises formulating asynchronous programming logic as data streams, and automatic propagation of changes to values of correlated variables in the system. - It belongs to the declarative programming paradigm family where the end result is simply described without necessarily outlining the intermediate steps to get there - By design it favours Functional Programming
  7. Data Streams Or Observables What are They? In Reactive Programming,

    Streams are cheap and Ubiquitous Anything Can be a stream in Reactive Programming: :variables, user inputs, properties, caches, data structures, etc. Example: Your Twitter feed would be a data stream in the same fashion that click events are. You can listen to that stream and react accordingly.
  8. Observers What are They? Observers Consume Data emitted by streams/

    observables. Whenever the observable emits the data all the registered observer receives the data . After which they can perform various operations like parsing the JSON response or updating the UI. If there is an error thrown from observable, the observer will receive it and react accordingly.
  9. Back to the, Message Driven Architecture - As earlier stated

    A message-driven architecture is the foundation of Reactive applications. A message-driven application may be event-driven, actor-based, or a combination of the two. - An event-driven system is based on events which are monitored by zero or more observers. This is different than imperative programming because the caller doesn’t need to block waiting for a response from the invoked routine. Events are not directed to a specific address, but rather watched (or listened) for.
  10. Back to the, Message Driven Architecture - Actor-based concurrency is

    an extension of the message-passing architecture, where messages are directed to a recipient, which happens to be an actor. Messages may cross thread boundaries or be passed to another actor’s mailbox on a different physical server. This enables elasticity — scaling out on demand — as actors can be distributed across the network, yet still communicate with each other as if they were all on the same machine
  11. The Difference - The main difference between messages and events

    is that messages are directed while events happen. Messages have a clear destination while events may be observed by zero or more (0-N) observers.
  12. A little more about Event Driven Concurrency - Superficially, event-driven

    applications are focused on triggering events. Events may be encoded as messages that are placed in a queue that is monitored by zero or more observers. The big difference between event-driven and imperative style is that the caller does not block and hold onto a thread while waiting for a response. The event-loop itself may be single threaded, but concurrency is still achieved while invoked routines go about their business (and potentially block on IO themselves) while allowing the (sometimes single) threaded event-loop to process incoming requests. Instead of blocking on a request unless completely processed, the caller’s identity is passed along with the body of the request message so that — if the invoked routine chooses to do so — the caller can be called back with a response.
  13. The main consequence of choosing an event-driven architecture is that

    they can suffer from a phenomenon called callback hell Checkout: http://callbackhell.com/
  14. A little more about Actor Based Concurrency Actor-based applications revolve

    around asynchronous message passing between multiple actors. - An actor is a construct with the following properties: ❖ A mailbox for receiving messages. ❖ The actor’s logic, which relies on pattern matching to determine how to handle each type of message it receives. ❖ Isolated state — rather than shared state — for storing context between requests.
  15. A little more about Actor Based Concurrency - A huge

    benefit of actor-based concurrency is that in addition to the benefits gained by an event-driven architecture, scaling computation out across network boundaries is even easier, and callback-hell is avoided because messages are directed to actors. - This is a powerful concept that makes it easy to build hyper-scalable applications that are also easy to design, build, and maintain. Rather than thinking about time and space, or deeply nested callbacks, you only need to think about how messages flow between actors.