Reactive Programming: Asynchronous and Event-Driven Architecture by Unlocking the Power of Data Streams

Roman Glushach
6 min readJun 15, 2023

--

Reactive Programming

Reactive Programming is a declarative, event-driven programming paradigm concerned with data streams and the propagation of change. It describes a design paradigm that relies on asynchronous programming logic to handle real-time updates to otherwise static content that allows you to build applications that are highly responsive, scalable, and resilient. Reactive Programming is all about streams, which are time-ordered sequences of related event messages. A given stream will generally start with an observer, which can be either a producer or a consumer. By structuring your program around data streams, you are writing asynchronous code. Reactive Programming changes how you design and write your code.

Terminology

Event-Driven Architecture

Is a way of building software systems that are based on the idea of events. An event is a signal that something has happened in the system. It could be a user clicking a button, a sensor detecting a change in temperature, or a database record being updated. In an event-driven architecture, the system is designed to respond to events as they occur. When an event occurs, the system triggers a set of actions that are designed to handle that event. These actions could include updating a database, sending a notification to a user, or triggering another event

Asynchronous Programming

Is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. It’s like ordering food at a restaurant. You can do other things while your food is being prepared, such as talking to your friends or checking your phone. When your food is ready, the waiter will bring it to you and you can enjoy it. You don’t have to wait at the counter until your food is done and not being able to perform any other actions.

Reactive Programming

Is a paradigm that focuses on how data flows and changes in an application. It is based on the idea of reacting to events, rather than controlling the flow of execution

Reactive Systems

Systems that respond to changes in their environment in a timely and consistent manner and can handle failures gracefully and recover from them without losing functionality or data. They can scale up or down according to the demand and resources available, without compromising performance or quality

Producer (provider)

Is an entity that emits data or events to one or more subscribers in a reactive programming model. It can be a source of data, such as a database, a web service, a sensor, or a user input but also can be a transformation of data, such as a filter, a map, a reduce, or a combine operation. A producer can be synchronous or asynchronous, depending on whether it emits data immediately or later and can be hot or cold, depending on whether it starts emitting data before or after a subscriber subscribes to it. It can be also unicast or multicast, depending on whether it emits the same or different data to each subscriber

Consumer (subscriber)

Is an object that receives and processes data from a source, such as a publisher or an observable. Consumer can subscribe to one or more sources and perform actions on the data, such as printing, filtering, transforming, storing, or sending to another consumer and can also handle errors and completion events from the source, and dispose the subscription when it is no longer needed

Stream

Stream is a sequence of data items that are produced and consumed asynchronously. Streams can represent anything that varies over time, such as user input, network requests, sensor data, etc. Streams can also be composed, transformed, filtered, and combined to create complex data flows

Data Stream

Is a sequence of data items that are emitted over time from a source. Data Stream can represent any kind of asynchronous or event-driven data, such as user input, network requests, sensor readings, etc. Data Stream can be subscribed to by observers that react to the data items as they arrive and be either finite or infinite, depending on whether they have a definite end or not

Observable

An observable is a type of stream that can be observed by one or more observers. An observer is a function that reacts to the data items emitted by the observable

Observer Pattern

Is a design pattern that defines a one-to-many dependency between objects, such that when one object changes state, all its dependents are notified and updated automatically. As an example, stock ticker that displays the latest prices of various stocks. The stock prices are obtained from an external source that is an observable object that emits data events when new prices are available, and the stock ticker is an observer object that subscribes to the source and displays the prices. Or another example: a chat application that allows users to send and receive messages. The messages are exchanged through a server that is an observable object that emits data events when new messages arrive, and the chat application is an observer object that subscribes to the server and shows the messages

Subscription

Subscription is a link between an observable and an observer. A subscription allows the observer to receive the data items from the observable and to handle any errors or completion signals. A subscription can also be cancelled to stop receiving data from the observable

Scheduler

Scheduler is an object that controls when and how the data items are emitted by the observable and processed by the observer. A scheduler can also manage concurrency and parallelism in reactive programming. There are different types of schedulers, such as immediate, timeout, interval, animation frame, etc.

Backpressure

Backpressure is a situation where the observable produces data faster than the observer can consume it. Backpressure can cause memory leaks, performance issues, or data loss. To handle backpressure, you can use various strategies, such as buffering, dropping, throttling, or switching observables

How Does it Work?

Reactive programming works by implementing the observer pattern. The observer pattern is a design pattern that defines a one-to-many relationship between an observable (the producer) and one or more observers (the consumers). The observable notifies its observers whenever it changes its state or emits an event. The observers react to these notifications by performing some actions or side effects.

In reactive programming, the observable represents a data stream, and the observer represents a subscriber. The observable can emit three types of notifications: onNext (a new event), onError (an error), and onComplete (the end of the stream). The observer can implement three methods to handle these notifications: onNext (to process each event), onError (to handle errors), and onComplete (to perform cleanup).

The observable and the observer communicate through a subscription object. The subscription object allows the observer to unsubscribe from the observable when it is no longer interested in receiving notifications. It also allows the observer to request more events from the observable using back pressure.

Next Steps

Traditional programming models are based on the idea of sequential execution. In this model, the program executes one instruction at a time, in a predetermined order. This model works well for simple programs, but it quickly breaks down when you need to build complex, highly responsive applications.Reactive programming provides a way to build applications that can handle large amounts of data, respond quickly to user input, and scale easily to meet changing demands. It allows you to build applications that are more resilient to failure, more responsive to user input, and more scalable than traditional programming models.

Follow for more:

--

--

Roman Glushach
Roman Glushach

Written by Roman Glushach

Senior Software Architect & Engineer Manager at Freelance

No responses yet