• Linas Naginionis
Software Craftsmanship

Simple reactive microservices using rxJava Observables

Simple reactive microservices using rxJava Observables

Functional Reactive Programming is what interest me the most nowadays. RxJava is a great library which implements reactive extensions for the JVM. There are many reasons why functional reactive programming using RxJava is beneficial:

  • It fully embraces immutability. Instead of mutating state in place, you just pass it through the chain.
  • It encourages you to write pure functions
  • It’s very flexible
  • It’s highly composable
  • Easy to recover from possible errors
  • It uses push instead of pull
  • Same code can be sync or async
  • It’s easily testable
  • It supports backpressure
  • It’s polyglot library. Same concepts can be applied in different languages
  • It helps you to separate your business logic into commands and events.
  • It’s fun 🙂

As you probably now, akka is a framework which also can be used to write reactive applications. One of the akka selling points are actors and their ability to communicate with each other through the network. This makes akka very useful when writing microservices, especially when microservices now are on the rise.

I like the concept of RxJava more though because it provides you with more functional approach to build reactive applications. So recently I started thinking,- why can’t Observables be easily used in distributed environment?

And then I decided to write a library which would allow me to call commands (which are implemented using Observables) from different applications. This way you could easily decouple your services. They could work independently without even knowing about each other. You would compose them in your client applications. So the code now looks like this:

//Suppose you have some business logic in the first web app.
//Here you must register your exposable commands
CommandRegistry commandRegistry = CommandRegistry.of("addOrder", order -> 
               Observable.just("Order added: " + order)) //just emit text in this example
      .and("anotherCommand", arg -> Observable.just("foo"));

new Server(config, commandRegistry).start(); //starts server which exposes all registered commands

//Here you just execute your commands. This can be done in another application
  .execute(new Order(id), String.class) //returns Observable<String>
  .subscribe(msg -> System.out.println(msg)); //execute addOrder command and receive events from it. 
//It prints: Order added: {Order = ...}.

There are some more configuration needed to be done but I just wanted to show the main concept of the library.

All the commands will be wrapped into hystrix commands implementing circuit breaker pattern and allowing to have some fallback mechanisms. So if the main server is busy or not responding, fallback server will be used to execute the same command. Also hystrix monitoring dashboard can be used because all the metrics will be sent over the network:

Hystrix Dashboard

In summary, I wanted to create a thin library for building simple reactive microservices. I think the first results are very promising. What’s your opinion about this?


You can find my library on the github already, it is called reacto.


2 thoughts on “Simple reactive microservices using rxJava Observables

  1. Madhab Mahakud

    Hi Linas, its a very good article . I just started love to rxjava now a days which I also found great fun.

    I was searching for the use of CommandRegistry and found this which clear me a lot.
    Thanks for this post.

Leave a Reply

Your email address will not be published. Required fields are marked *