What is reactive programming?

preface

At present, reactive programming is still in the era of chaos, and each family has its own definition and implementation. The responsive manifesto defines the characteristics that the responsive should satisfy. Spring5 has its own reactive solution technology stack, and the well-known reactiveX belonging to rxJava also has its own understanding. Some say that responsiveness is event-based, and some say that it is data flow-based. Some people say that reactive programming is actually just an api library that facilitates concurrent programming. Some people say that reactive programming is a programming idea and a programming paradigm. Technology circles are vague, as Ma Ying-jeou said: 一种技术,各自表述. Reactive programming is ambiguous, somewhat hollow, and somewhat exaggerated, which inevitably makes it difficult for ordinary learners to get started and is a headache.

The following is mainly based on the reactive manifesto and reactiveX to explain reactive programming. If you have any objections, please feel free to discuss and advise in the comment area.

What is reactive programming?

It is a reactive programming idea using asynchronous data flow programming. (from here )

Reactive ProgrammingIt is based on the observer model. This is the consensus of everyone . It provides non-blocking and asynchronous features to facilitate the processing of asynchronous scenarios, thereby avoiding 回调地狱and breaking through Futurethe limitations: difficult to combine. (I don't agree with this advantage, java8's completeFuture has been well improved).

In reactiveX, API libraries are provided for several languages , which extend the observer mode and support 数据OR- 事件based drivers. Allows to 声明式combine observer sequences. Abstract synchronous programming and asynchronous programming into a unified.

The embodiment of asynchronous programming and synchronous programming in the code can be reflected in the following table. Different from the traditional pull type, responsive programming usually uses push type data transmission, and the producer controls the consumption speed, but when the consumer consumption speed is insufficient, it can also prompt the producer to reduce the production speed (backPressure), backPressure Will be explained below.

函数式的思想Reactive programming usually comes with , but this is used to simplify the bloated object-oriented grammar. The core of reactive programming lies in Reactive, which only has some of the features of Functional, not even Functional Reactive Programming . This is a completely different thing. .

Responsive declaration

What characteristics should a responsive system have

When a system has immediate responsiveness (Responsive), resilience (Resilient), elasticity (Elastic) and message-driven (Message Driven). We call such a system a reactive system (Reactive System).

img

As shown in the figure, the value of reactive programming lies in 1. Easy to write and maintain (especially asynchronous programming) 2. Timely response.

What is 及时响应? No matter what goes wrong, power failure, network jitter, or code bug, we can respond in time and provide sufficient usability.

In the responsive declaration, the exception is also regarded as a message to simplify the handling of the error , and the error can be simply regarded as a variety of Exception.

What is elasticity? Resilience?

Elasticity means that there is no difference in system throughput and responsiveness under different loads. Responsiveness makes the system free from resource contention points and central bottlenecks through scaling algorithms.

Resilience means that when the system fails, such as power outages, hardware failures, resource exhaustion, etc., through replication , containment, isolation, and delegation to ensure that it still has immediate responsiveness.

Message-driven or event-driven?

Messages are data and events are signals. The information contained in the message is richer.

For a message-driven system , if no message comes, the recipient asynchronously non-blocks and waits for the thread to arrive (sleep).

For event-driven systems , callback methods are often used.

It is recommended to use the 消息驱动design in the responsive manifesto , because event-driven systems are difficult to achieve resilience:

For event-driven systems, when the processing process is ready and the listeners have been set up to respond to the results and transform the results, these listeners will usually directly handle the success or failure and report the execution results to the original client .

(These listeners) respond to the failure of the component in order to restore its normal function (referring to the failed component). On the other hand, what needs to be processed is those that are not bundled with short-lived client requests, but affect The health status of the entire component fails.

Reactive programming related discussions

Why use observer mode

This mode from the developer 回调地狱freed in. ReactiveX uses streams to unify asynchronous and synchronous 订阅者operations , and to combine various operations to improve code legibility.

What is callback hell? In fact, when callback functions need to be combined, it often greatly increases the complexity of the code and greatly reduces the legibility of the code.

The observer mode of ReactiveX is very flexible, not only supports push but also pull.

What is backpressure (backPressure)

In fact, back pressure is not well translated, so it is more appropriate to call back pressure. There is a back pressure explanation on Zhihu. I think it’s very good. Back pressure is actually just a phenomenon, but in responsive programming is backPressureactually a processing strategy to solve the problem when consumption can’t keep up with production, which slows down producers. Click the push button, or drop it directly...

In the process of data stream transmission from upstream producers to downstream consumers, the upstream production speed is greater than the downstream consumption speed, causing the downstream buffer to overflow. This phenomenon is called Backpressure.

Personal thoughts

When I was a student, I often heard people talk about reactive programming and webflux. At that time, I was confused by various new technologies. I read a few related introductions randomly, and then listened to the various types of reactive programming. The blog post boasted so good and advanced, and I felt that I had to learn at the time.

After working, I discovered that the actual production does not require much advanced things. Most of the time, online system stability is the most important thing. Java 8 is still popular in China. Many people have not figured out the characteristics of Java 8, but they still support themselves in their jobs.

For developers, don't be fooled by all kinds of "technical marketing" on the Internet. Think about whether you really want to learn before you learn a new technology. What code, what technology, what framework is just a hammer in the hands of migrant workers on the Internet. It is not that the newer and more fancy a hammer is, the better, and it can solve the actual work content.

Sometimes instead of learning a variety of new technologies and frameworks, it is better to watch movies and go shopping with your family. We are not the screws of the Internet. We are human beings. It just coincides with the era of capitalism. Most people have been alienated. "Tool people", but we all have our own lives, our family and friends, everyone is different, and we all have our own lives.

All in all, there are various types of reactive programming on the market, with various definitions and insufficient proofs of merits. Don't rush into the market.

Related documents

  1. http://reactivex.io/intro.html
  2. GitHub rxjava
  3. Reactive-Programming-a technology-respectively expressed
  4. Responsive declaration
  5. Responsive declaration glossary

Guess you like

Origin blog.csdn.net/qq_38619183/article/details/112430869