Building scalable and resilient web application using Akka

When designing computer applications, the programming model needs to consider scalability, resilience, fault tolerant and most importantly high performance. The system should be able to cope with network latency, memory usage, and high computation required by the system. To overcome these situations, Akka’s actor model allows you to create actors to cope with the modern computer architectures.

Akka Actors

An actor is the smallest unit of your Akka-based application. An Akka is a container for several components. The components include actors state, behavior, mailbox, child actors and Supervisor Strategy. Akka ensures that the communication is through asynchronous messaging and its the only way to interact. This means that you can send a message to an actor and forget about it. When a sender sends a message to an actor, the message ends up in the mailbox, which is dispatched to the actor asynchronously by the dispatcher. Dispatcher’s job is to enqueue and dequeue messages from the mailbox and send it to the actor to execute the business logic, one at a time. An actor encapsulates state and behavior. The state and behavior, represent attributes and business logic of the actor. Akka takes care of all the necessary synchronization to make sure the state of any actor is visible to any other actor. Child actors are basically new actors created by an actor to do new subtasks. Fault handling is done transparently by Akka applying different strategies in Akka System.

Akka Actor Components

Akka Example

In this blog post, we will create a Soccer Dashboard for English Premier League. The dashboard is created by web crawling the website. The example will have three parts:

  • Data Collection: We will be only interested in 3 sections of the site Results, Fixtures, and Team Standings. Out of many links in the web page, we will filter on these 3, scrape that page, create a JSON store it in IBM Cloudant NoSQL database. You can use any NoSQL database.
  • Akka-HTTP REST-API: We will be creating REST based API which has actors to basically GET the data from Cloudant database and send JSON response to the UI
  • Soccer Dashboard: This is not the main part of the blog but we will be using ReactJS to create a UI to display dashboard UI. The dashboard will contain 4 pages, Results, Fixtures, Team Table and Dashboard.


Defining Actors

We have identified five actors where CrawlingApp is the main class which creates an ActorSystem is a container where WebCrawler, CloudantReaderWriter, URLValidator and HTMLParser run. The WebCrawler is a mediator that calls other actors. WebCrawler creates instances of CloudantReaderWriter, URLValidator and HTMLParser. Below shows the asynchronous communication between the actors:

With this messaging flow, the JSON after scraping the web will be stored into the IBM Cloudant database. There will be 3 JSON document stored in the database, results, fixtures, and team standing. The next step is to create REST-ful APIs to serve these as JSON so that the ReactJS front end can consume it.

Creating Actors

In Akka an actor is a class that implements a traitActor and you can mix inActorLogging which gives you an implicitlog object to start logging. And you will be needing to override receivemethod which returnsReceivewhich is the initial behavior of an actor.

class WebCrawler extends Actor with ActorLogging{
override def receive: Receive = ???

In order to create these actors first, we need to createActorSystem which is a singleton object per application.Basically, ActorSystem is a factory method that allows you to get an instance of actors. There is only one ActorSystem per application. You cannot directly create actors using the constructor because it throws an exception and violated the asynchronous principle used in Akka.

val system = ActorSystem("Crawler")
val webCrawler = system.actorOf(Props[WebCrawler], "WebCrawler")

Tell (!) and Ask (?) Operators in Akka

After you have created the instance of actor you need to pass messages to the actors to execute the logic. ! is used for sending messages asynchronously to the actor. In Akka terminology, ! means fire and forget also known as tell? sends a message asynchronously and returns a Future representing a possible reply. Also known as ask

REST API using Akka HTTP

The Akka HTTP modules implement client-side and server-side HTTP stack on top of akka-actor and akka-stream. Akka HTTP can be used to create REST-based API. In this example I have created 3 GET API to return JSON results for fixtures, results, and team standing.

GET /results
GET /fixtures
GET /teamtable

We have a controller class calledSoccerController which exposes 3 GET APIs for the ReactJS application to interact and receive JSON response. Akka HTTP comes in a separate JAR and you can get started by adding following dependencies into your project

val akkaVersion = 2.5.4
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,

The next step is to create DSL routes so that frontend can interact. According to Akka HTTP documentation “The high-level, routing API of Akka HTTP provides a DSL to describe HTTP “routes” and how they should be handled. Each route is composed of one or more level of Directive s that narrows down to handling one specific type of request."

For instance, the route will define a path called "/results" and then narrow it down to the request type GET, POST, PUT, UPDATE, DELETE and then onComplete,which will be sent back a an HTTP OK with the JSON as the response body. Instead of sending just string or default HTML you can also use JSON/XML marshallers to send JSON/XML as a response.

val route: Route =
path("fixtures") {
get {
val fixtureJson:Future[String] = (cloudantReader ? GetDocument(DocumentType.Fixtures)).mapTo[String]
} ~
path("teamtable") {
get {
val teamTableJson:Future[String] = (cloudantReader ? GetDocument(DocumentType.TeamTable)).mapTo[String]
} ~
path("results") {
get {
val resultsJson:Future[String] = (cloudantReader ? GetDocument(DocumentType.Results)).mapTo[String]

The Route created using the Route DSL is then “bound” to a port to start serving HTTP requests

val bindingFuture = Http().bindAndHandle(corsHandler(route),

The REST API reads documents from IBM Cloudant database and returns you the result to create UI. The ReactJS tutorial is not part of this blog so I will only show you output results in the UI. The source code is available on GitHub.

Deploying to Cloud Foundry

In order to deploy to Cloud Foundry, make sure you have an IBM Bluemix account. And you have to install the following to get started.

  • Install Cloud Foundry CLI
  • Login to CF using: cf login --sso and use one-time password from a given URL to login
  • Create a fat jar using: sbt assembly after going to directory: /akka-epl
  • You need to have manifest.yml file as in the code repository to push it to the cloud foundry app
  • You can push the app using command: cf push
  • For Debugging you can see the logs to make sure your app is successfully pushed or not using cf logs akka-react-cloudant --recent
  • you can also ssh to the application machine using command: cf enable-ssh <app_name> and cf ssh <app_name>


The components are hosted in IBM Cloudfoundry as apps and the data are stored in IBM cloudant database.


The GitHub source links can be found here. It contains 3 components.

  • Web Crawler for data collection to IBM Cloudant database
  • Akka HTTP REST based API using Routes DSL
  • ReactJS Frontend

Reference Links