Reactive API development

Customers’ demands are ever increasing: software has to look nice, feel intuitive to use, be fast, and work seamlessly on any device. I experience this every day: if I want to order a new pair of sneakers and I browse through some online shops, the sites that don’t feel right after five seconds get a ruthless click on the X.

In this high-demanding world we have to continuously look at new aspects of software development. Off course the user interface with all its shiny CSS, HTML, and JavaScript frameworks is very important. However, in this article I focus on the API layer that connects the front-end with the back-end systems.
APIs have to be reactive: responsive, resilient, and elastic. In the reactive manifesto , the fourth aspect is message-driven, although I consider that to be an implementation choice (a good one). Let’s look at two ways to accomplish this.

Akka

Akka is a popular Scala framework that is based on the Actor Pattern invented and published in 1973 by Carl Hewitt . In modern form, this model surfaced in Russell & Norvig’s book “Artificial Intelligence: A Modern Approach” as autonomous agents . Most noticeable in IT, Ericsson used actors in the Erlang language to build reliable telecom systems . An actor is an independent, message-driven container with state (data) and behaviour (methods). Actors are not replacements of objects; they cannot call each other directly. Each actor is supervised by a parent, who can create new actors or replace non-functioning ones. Using Akka actors gives developers a high level of concurrency, without having to worry about threads or keep-alive mechanisms. The value of Akka is eminent; for example, Spark relies heavily on Akka actors to make in-memory, parallel processing of big data possible. The downside of Akka is that developers sometimes find the framework too low-level, and since the messages are not typesafe the code of an Akka system must be tested extensively.


 
class MyActor extends Actor {
  val log = Logging(context.system, this)
  def receive = {
    // tell something (fire & forget) to originating actor
    case “check”   => sender ! “check OK”  
    // absorb incoming message and ignore further
    case _         => log.info(“received unknown message”)
  }
}

Listing 1: Sample Akka actor

Futures

A Future in Scala or Java is a value that may not exist yet. It gives developers a way to build in parallelism and non-blocking code. Futures simply rely on callbacks, but the concept of a Future can be difficult to grasp; a Future of something means that a computation may not be complete yet; so what is its value or meaning? There’s no way to say; we have to wait for the Future to complete successfully or have failed. The advantage we get is that in this period of uncertainty, the server can continue with other tasks and remains responsive. This is a very powerful concept, but also adds some complexity in programs: you have to think more about the order of processing, and there are more moments when things can go wrong, so error handling gets even more important.
Conclusion
This article only scratches the surface in creating reactive APIs, highlighting two popular options. There certainly are more: Java 8 Streams, RxScala and RxJava to name a few. So, which one should you take? Each framework has its own pros and cons, and a lot depends on the environment. If you’re happy to use Scala, go for Akka or Scala Futures. If you want to stick with Java, Java Futures (java.util.concurrent.future) or the Java 8 Streaming framework will also be a good option. At J-Fall, we will describe an Akka API that ING uses for predictive analytics in the ‘Kijk vooruit’ feature of our mobile app. Join our session to learn more about Akka and see an example in production!

Tekstvak: val f: Future[List[String]] = Future {
  session.getLargeDataset(myQuery)
}

f onComplete {
  case Success(data) => for (record <- data) println(record)
  case Failure(t) => println("Error while getting data: " + t.getMessage)
}

 

 

 

 

Listing 2: Sample Scala Future

Conclusion

This article only scratches the surface in creating reactive APIs, highlighting two popular options. There certainly are more: Java 8 Streams, RxScala and RxJava to name a few. So, which one should you take? Each framework has its own pros and cons, and a lot depends on the environment. If you’re happy to use Scala, go for Akka or Scala Futures. If you want to stick with Java, Java Futures (java.util.concurrent.future) or the Java 8 Streaming framework will also be a good option. At J-Fall, we will describe an Akka API that ING uses for predictive analytics in the ‘Kijk vooruit’ feature of our mobile app. Join our session to learn more about Akka and see an example in production!