Reactive programming using Java

Reactive programming using Java

This article is about Reactive Programming, and how to achieve the same in Java.

Reactive programming helps to create systems that are event-driven, scalable, resilient, and responsive. With the advent of big data and the need for processing streams of data instead of simple CRUD applications, reactive programming is being adopted widely. For eg., if you have to process a live feed of stock prices or tweets based on some hashtag, then reactive programming is the way to go. Many reactive extensions are now available to enable reactive programming in different languages and frameworks, eg. RxJava library for Java-based applications. 

 Some important terms and classifications:

3 important players in reactive programming:

1. Observable: Source of streaming data.

2. Observer: Client that subscribes to Observable’s streams.

3. Operator: Functions like map(), filter(), take(), skip() etc. to help process the stream of data by taking an Observable as an input, and giving out another Observable as the output.

Types of Observables based on liveness of data:

1.  Hot: Hot observable start streaming data, without any knowledge of its subscribers. Any Observer that starts listening to a hot observable after the latter started emitting data, will miss the data that has already been streamed.

2.  Cold: Starts streaming data only when the client asks for it.

3.  Connectable: These are cold observables that turn into hot once they start emitting data. ie. a subscriber can choose when to start the emission, but once the data flow from the observable starts, any subscriber that joins in late would have missed some data.

From the perspective of stream processing, Observables can be also be classified as:

  1. Observable: emits a stream of data
  2. Flowable: emits a stream of data with Backpressure handling
  3. Single: Emit a single element on the stream
  4. Maybe: Emits 0 or more elements
  5. Completable: Only emits a Completion event, no data.

In reactive programming, the data is sent to the subscriber on 3 channels:

  1. Data channel: subscriber processes data from this channel
  2. Error channel: In case of any error, the latter is propagated on a different error channel. Once the error is propagated on its designated channel, the data stops flowing on the data channel. The subscriber can process the error and choose the next action like returning a value, retrying, resuming with another stream, etc.
  3. Completion channel: When the source is done sending all the data, it can send out a message on the completion channel notifying the subscriber that the data flow is finished.
No alt text provided for this image
Observables support sync, async and lazy streaming of data

Time to answer some questions to help understand reactive programming better:

Q. How are RxJava streams different from Java 8 Lambda streams?

Ans. Java 8 streams provide a single channel for the flow of data. Once the data processing starts, there is no provision to stop the subscription. Also, there is no error handling provided by Java 8 streams, whereas RxJava streams treat the error as a first-class citizen and put it on their respective channel.

Q. Angularjs HTTP module returns an Observable for async handling of the API response. How is Observable different from javascript’s Promise?

Ans. A promise is a single flow of data, whereas Observables are continuous streams of data, which the subscriber can choose when to stop listening to.

Q. What if the source is pushing data at a faster pace than at which the subscriber can consume?

Ans. This is known as Backpressure. We can choose to keep the data in buffer for the subscriber to process it later. But this can also lead to issues like out of memory. Another option is to throttle the surplus inflow of data. RxJava provides Flowable class which is basically an observable that supports various strategies for handling Backpressure.

Q. Is reactive programming same as observer design pattern?

Ans. No. The observer pattern commonly describes data-flows between whole objects/classes, whereas object-oriented reactive programming targets the members of objects/classes.

Q. Java 8 introduced CompletableFuture for async processing. How is it different from reactive programming?

Ans. A CompletableFuture represents one result of an asynchronous call, while Reactive Streams is a pattern for pushing N messages synchronously/asynchronously through a system. CompletableFuture doesn’t address the elasticity requirement of the Reactive Manifesto as it does not handle backpressure.

Concluding this post with a link to sample RxJava code snippets that will help understand above concepts.

要查看或添加评论,请登录

Ramit Sharma的更多文章

  • AWS Certifications - difficulty ranking

    AWS Certifications - difficulty ranking

    Having taken a majority of AWS Associate/Speciality/Professional certifications, I rank all AWS exams in increasing…

    12 条评论
  • What are Microfrontends?

    What are Microfrontends?

    Coming from a microservices background, the first time I heard the term ‘Micro-frontend’, I was left scratching my…

  • How to prepare for AWS certifications

    How to prepare for AWS certifications

    I keep getting messages on how to start preparations for AWS certification exams. So in this article, I will briefly…

  • Server vs Client-side load balancing

    Server vs Client-side load balancing

    This article discusses the key features of server-side and client-side load balancing. At the end of the post, there is…

    14 条评论
  • HTTP 3 is here

    HTTP 3 is here

    The demand for low latency web and mobile applications has brought significant evolution in HTTP protocol in recent…

  • Why 'REST' when you can 'gRPC'

    Why 'REST' when you can 'gRPC'

    gRPC stands for gRPC Remote Procedure Call. It's an open-source framework by Google for remote procedure calls based on…

    2 条评论

社区洞察

其他会员也浏览了