Reactive programming in software development

What exactly is a reactive system? To keep it simple, it is a system that utilizes system resources on demand, it can “shrink” or “expand” depending on current load. But why is it cool?

Reactive systems

To illustrate the benefits of reactive systems, it’s best to refer to negative examples – situations when system’s inability to adapt causes deny of service and loss of potential profit.

The best negative examples are:

  1. Digital release of a hyped game. Nobody can get a decent download speed (though it can be partially avoided by utilizing P2P download mechanisms in game store client);

  2. Initial launch of an MMO (Massive Multiplayer Online) game like World of Warcraft or GTA V Online or a large update to it. Users overrun game servers and nobody gets to play. Players either can’t log in, wait in large queues or enjoy first-class lags;

  3. Online store or a SaaS service during a specific time-frame (Black Friday, Christmas, etc) or during a popular promo campaign. It’s just being overrun by clients and becomes very unstable – one second it works, and another it doesn’t. Imagine a checkout process in that situation;

Things we described occur over and over. So you would ask, why is it so hard to just rent enough servers already? Why not calculate resources needed and not make your clients frustrated? Short answer for this question is “money”. In many cases renting, setting up, integrating and supporting additional servers for a relatively small time-frame (like a couple of days, a week or even two) will cost ridiculous amount in comparison with earnings additional users will bring. So resources are calculated and rented only for expected average load with some local peaks in mind, and quality of service for extreme situations is compromised for the sake of product profitability.

But if someone could bring revenue from users usually left behind, without spending too much?

That person would make a nice profit and improve their reputation. It’s killing two birds with one stone. So how would you do that? How to make everyone happy and make more money?

That’s where Reactive Systems are coming to save the day.

So what makes something a reactive system?

Types of requirements

First of all, reactiveness is not some mambo-jumbo, it is a software architecture approach.

And like all architecture approaches it has specific purposes. The main purpose is to give your software means to avoid deny of service all by itself, without human planning, hiring teams, etc, so your product will always respond to user requests.

This is achieved by grasping a set of lesser goals:

  • More intensive and more intelligent usage of system resources;
  • Ability to seamlessly expand beyond its physical servers and shrink back;

Of course this alone wouldn’t do any good without a way to seamlessly get more machine time and pay only for what you actually use. So it is not possible to build a truly reactive system without modern cloud solutions, that’s why reactiveness is becoming a trend only now, it wasn’t possible earlier due to lack of adequate infrastructure (See our article on cloud solutions).

But what rules over all of this, what triggers and regulates all these actions is users.

Without any requests from users system will use a minimum set of resources and won’t burn your money, and when need arises, it will only take the absolute necessary amount of both. System reacts to amount of requests like a living organism. Hence the name – Reactive Systems.

Why should you adopt reactive approach?

So what should bring you to make a reactive system out of your project from the start?

Let’s see by example of a typical development story:

You have an idea for a startup. So you hire a bunch of developers to implement and support it.

With startups time to market is everything, so you go by Agile methodology and focus more on functionality and fast delivery rather than on growth plans. That usually means that you try to make use of maximum amount of existing mainstream solutions.

Everything goes fine, your product gains popularity, and user base grows. Everyone is happy. But because you didn’t think ahead about what will happen when your product becomes popular, you come to the point when product becomes unstable due to lack of system resources. You can get into the situation when on the one hand, it will cost too much money to re-implement and optimize your code, and from the other hand — your revenue will decrease due to dropping of quality of service.

Reactive programming in Agile methodology

No one likes to use an unstable product. First time you face this problem you can ignore it by getting more machine power, going extensive. But soon you will discover that maintenance footprint makes it difficult to do that too, revenue won’t cover usage & maintenance costs. Then you start to profile your product and discover that in fact there are time-frames when you don’t even need machine power you currently rent, and you spend money ineffectively as is.

Then you think that it would be good if you could pay only for what you use, and discover that it is possible. And only then you discover that your software wasn’t built to work like that, and regret that you didn’t think about building a reactive system from starters.

Guiding principles of reactive systems

To make reactive systems more popular, it’s only natural to spread the word and highlight guiding principles that will make a difference in your products life. These principles were combined together into Reactive Manifesto, of which we’ll give you a short summary. Reactive systems must:

  1. React to increase of load, be scalable — this is a quality that allows application to shrink or expand (add and remove nodes) on demand. This also allows to make your application work like a cluster and distribute load between its nodes.

  2. React to failures, be resilient — the system is fail-proof and has recovery mechanism for any stage of processing user request. If some component of your application fails, the system will make its best to compensate the loss (just like a living organism), recover the component or completely replace it.

  3. React to users, be responsive — the system provides guaranteed fast response disregarding current load.

  4. React to events, be event-driven — the system makes use of asynchronous message transfer, which allows components to work independently from one another, and every component communicates with another by means of messages.

These are the guiding principles of reactive systems. They also help to illustrate main benefits of this approach.

Mainstream reactive technologies

We’ve mentioned earlier, that startups try to exploit maximum amount of existing technologies. So to make reactive systems popular, they should be something trendy and easy receivable. Thanks to community, reactiveness is a trend now, so you don’t have to do everything by yourself if you want to build a reactive system. There is a bunch of frameworks to help you.

Redwerk’s current stack of choice is a Typesafe Reactive Platform which provides instruments like:

Typesafe Activator

All in one instrument that will help you setup a project with technologies stack best suiting your needs, or just to fiddle with new technologies and see what they’re good for without spending hours on a tedious setup.

Akka reactive middle-ware

Akka brings an event-driven actor model that is easy to use to the mainstream world of programming. It allows developer to easily grasp core aspects of a reactive system: event-driveness, resilience, responsiveness and scalability (thanks to Akka Cluster).

Scala programming language

Scala is a functional programming language which runs on top of runtime environment very popular with business – Java Runtime Environment. Functional approach really suit best otherwise demanding job of implementing a reactive system and makes it easy and pleasant. Running on top of Java runtime environment and its full Java interoperability really help its popularity.

It’s always a good thing to have a handy framework to build a project on. So which framework will allow you to go reactive? There are two major choices:

Both these frameworks allow you to make a reactive application without breaking a sweat. But while Play Framework stands strong on a principle “write less, do more” to free developers from completing routine tasks over and over, Spray was literally built on top of Akka Actors model to be as reactive as possible out of the box (which comes at some price for developer, but hey).

Examples of currently working reactive systems

  1. Lichess take a look at thousands of chess players having matches in real time on this website. Without reactive approach and technologies it wouldn’t be possible. It’s a good example of what level of interactivity and responsiveness you can achieve by going reactive.

  2. Really popular social networks like LinkedIn and Foursquare are making good use of reactive approaches to ensure not a single feature will fail you due to servers overload. It’s valid not only for social networks, but for any resource with a huge auditory, like Coursera, where users can apply for thousands of educational courses and complete related tasks to receive a certificate.

  3. Another good example of such a service would be Retail.net – a cloud platform which allows customers to interact with retail brands in any spot, and for retailers – to have a centralized access and management of their sales, not to speak about stats. Again, if it wasn’t built on top of reactive principles it just wouldn’t work.

  4. Speaking about sales and retailers, and having online stores as an example of an appropriate project to incorporate reactive approach in this article, we should present examples of actual online stores going reactive. Good examples are Walmart, Amazon, and Gilt, these stores want to make sure that they won’t miss customers on any huge sale due to deny of service.

  5. Another good place for reactive systems are mass-media services, and The Guardian makes sure hundreds of thousands around the globe can have their daily fix of information by utilizing reactive approaches for their website.

Summary

Reactive systems are not only making user experience better, they also allow you to increase your product’s revenue and profitability. Reactive approach is the way to go in modern software development.

About Redwerk

For almost 10 years now Redwerk develops various solutions for e-government, broadcasting, business processes and workflow automation. By years we’ve polished our workflow to cover software development process from A to Z, so our customers will always get predictable, on-time, and awesome quality result. Redwerk has its representations in Ukraine, Germany and USA.

Projects we have done

Hear from our customer

«We hired Redwerk to design and develop a backend for our website. We asked them to do it in Scala using Play framework and although they did not have lots of expertise in exactly these technologies the work they delivered was top notch and on time with virtually no involvement from us. I highly recommend Redwerk for all kinds of software development! I am sure we will be working with them again.» — Dmitry Skavish, Co-founder and CEO at Animatron

Exploring Alternative Architecture Approaches: Reactive Programming 5.00/5 (100.00%) 6 votes