Akka Futures In Scala With A Simple Example

Table of contents
Reading Time: 3 minutes

We use Akka Futures extensively for a product built to handle huge load of streaming data. We have been early adopters of Akka and been using it right from its Akka 1.1.X days. We found Futures as simple yet powerful tool to add concurrent behavior in our application.

This post emphasizes importance of Akka Futures with a short and easy to understand example application. Please read excellent documentation on Akka Futures for more details.

Ok, now lets look at a simple example. Lets say we have an identity function. We sleep in it for a while to replicate long running nature. Here is the Scala code for the method.

Suppose, we have an application in which we call timeTakingIdentityFunction method three times, gather their return in three variables and execute sum on them. Here is the code listing.

When we run this we will get around nine seconds to execute it. But what will happen when we do not block on timeTakingIdentityFunction?

Using Akka Futures we can do exactly that. When we do not block on timeTakingIdentityFunction and continue processing, we get performance boost. Now lets look at the Akka Futures version.

We can use Future directly, we wrap method call in a Future. In our example, we can do this in code.

Since we are making three calls on timeTakingIdentityFunction. We can collect Future[Int] in three variables i.e. future1, future2, future3

Futures are monadic. We can compose them using for expressions. The future obtained by composing them can be used for calculating sum.

We will use onSuccess callback for this. Here is the code snippet.

We composed a new Future out of three. The new composed Future is of type Future[Int]. We issued a callback to gather sum of three numbers. Here is the complete example.

Only thing we have added here is implicit ActorSystem. Future requires an execution context. If ActorSystem is in implicit scope we are fine.

When we run Future version of the application it runs in about three seconds!!

Akka Futures can add benefit by adding concurrency in our applications. For example we might need to make three blocking Rest API calls to achieve a functionality. Depending on our use case we can avoid issuing blocking calls, but rather use Futures.

We used Futures directly in our example application. Since Futures are monadic we composed them using for expressions. We also used callback method to collect the result. And using them we got three times improvement in our code. Most importantly code is simple, concise and expressive.

There is more concise way to do this. We can construct List of Future[Int] and call Future.sequence to create or compose a single Future of List[Int]. Code is left as an exercise 🙂

Latest version of Akka 2.1.0 for Scala 2.10 has unified version of Future in Scala. This post pertains to Akka 2.0.5 version, concepts are still the same.

13 thoughts on “Akka Futures In Scala With A Simple Example4 min read

  1. As suggested, this example can be conciser. Applying hint I gave, we can do the following:
    // we construct List[Future[Int]]
    val futureList = List(1, 2, 3) map { case number => Future(timeTakingIdentityFunction(number)) }
    // we compose from List[Future[Int]] to Future[List[Int]]
    val future = Future.sequence(futureList)
    // we wait on the combined Future[List[Int]] and print the result
    future onSuccess {
    case results =>
    val elapsedTime = ((System.currentTimeMillis – startTime) / 1000.0)
    println(“Sum of 1, 2 and 3 is ” + results.sum + ” calculated in ” + elapsedTime + ” seconds”)
    }

  2. Meetu,

    Thanks for putting this together.

    I’d like to mention that Akka Futures have been moved (after unification with some other Future implementations and standardization under the umbrella of SIP 14) to the Scala standard library in 2.10 and therefore there are no more Akka Futures in Akka 2.1.

    Another aspect I’d like to point out: If you compose Futures monadically, i.e. with a for expression, you will *sequence* them. Therefore the running time will be the sum of the individual running times. In other words, you don’t go parallel.

    If parallelism is what you want, you have to compose the individual Futures into one like shown in your earlier comment. Fun fact: Although the composite Future will execute all parts in parallel (real parallel only when there are enough threads/cores), the name of the composing operation is `sequence`.

    Heiko

    1. Thanks a lot Heiko,

      Yes we are going to use latest Akka version soon, we are still on Akka 2.0.5. I will bump it to the latest in our upcoming release. Thanks for pointing out that from 2.1 both scala and Akka has been combined. I will mention that in my post.

      For this simple example number of threads are available and we do get the three time benefit :). The major point is that if need be, we need not make blocking calls.

      As for composing futures we do get the benefit because we compose Future after we create them. If we had composed them without creating them then they would have run sequentially. For example the above code and in gist https://gist.github.com/4561789 and the one mentioned in blog https://gist.github.com/4562364 runs in three seconds instead of nine.

      Regards,
      Meetu

      1. Thanks a lot Heiko,
        We all are learning, and Akka is such a great toolkit. It did wonders to many applications we built. Thanks to you and your organization Typesafe for building this great toolkit and Scala.
        Kind regards,
        Meetu

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading