Getting Started with Akka

An absolute beginners guide to Akka: Step 1

Garrett
4 min readJan 4, 2014

The word Actor has been thrown around a lot recently. Everyone I hear is telling me how powerful they are, and how well they scale, etc. I loved what I was hearing, so I decided to investigate. I listened to a few talks on YouTube but they were more advanced and conceptual. I’m writing this post to create the explanation and example that I would have wanted to see.

Akka is a TypeSafe Toolkit that implements Actors in a very clean way. There is almost no boilerplate when using Akka and Actors. Plus, the TypeSafe utilities are amazing powerful and easy to use.

Why Concurrency?

Before we get into what an Actor is/ does, it’s important to understand why concurrency is so important right now. CPU Manufacturers have always sought to create bigger and faster chips, and they’ve been very good at it. But in the last few years they’ve had to change their technique. Instead of large 7GHz single core processors, they’ve focussed on taking an older 2.2GHz core and fit as many as they can onto a single chip. This means that single threaded systems under load are maxing out a single core, while the others remain un-utilized.

One solution to this problem is to write single threaded software, and then have a server run multiple instance on threads and simply load balanced any incoming requests. There’s a problem though—if you begin to get consistent traffic, and have a single 1 second delay somewhere in your infrastructure, you could potentially retain queue of requests that will all be delayed by 1 second indefinitely.

So what should you do? Write asynchronous, non-blocking software, also known as reactive software. By never blocking, you can safely share threads in your thread pool, and you won’t cause traffic jams.

So how can you achieve reactive software without killing yourself? One approach, is by using Actors.

Akka and Actors

The Actor model was invented by Carl Hewitt in 1973 as a model for concurrent computation. An Actor is generic and flexible. It’s capable of creating Actors, responding to messages (from other Actors), etc. Actors are also thread-safe and can remove a lot of concurrency boilerplate which is one reason why they’re so popular right now.

An Akka Actor System is like a Pub/Sub server, and an Akka Actor is like a Pub/Sub Channel — akin to what you might find in Faye, Socket.IO or RabbitMQ. Only, Akka isn’t a separate server/service that you must run and connect to before executing your program. Akka is a self-contained light-weight toolkit that provides the power of messaging services in your runtime. Because of this, Akka is used in Android phones, massively scaled transaction systems, and in many other ways. Akka is also very easy to extend and scale from a single machine to a whole cluster.

Akka is a self-contained light-weight toolkit that provides the power of messaging services in your runtime.

Hello World

Let’s jump into a Hello World example. Let’s create an Actor that counts down from any given number, and then prints “done!” when it reaches 0.

import akka.actor._val system = ActorSystem("count-acting-system")class CountingActor extends Actor {
def receive = {
case n: Int if n > 0 => self ! (n - 1)
case 0 => println("done!")
}
}
val countingActor = system.actorOf(Props[CountingActor])countingActor ! 10

Note: If you want to get this example working, checkout the TypeSafe activator (Web UI/ Compiler) and the Hello-Akka template.

What we just did, was:

  1. Created what’s called an ActorSystem, which we use to coordinate and create all Actors
  2. Created a CountingActor which only has a receive method, and case statement. If n is greater than 0, we use self to send another message with (n-1). Otherwise we print “done!”
  3. We used system to create our instance of CountingActor called countingActor
  4. We used the ! operator (also know as the tell method) to send a message to countingActor with the value 10. Note: countingActor ! 10 is the same thing as writing countingActor.!(10). This is true for all Scala methods and operators.

What will happen after (4) is executed? The statement will return while a CountingActor on a different thread will asynchronously handle the message. Once the first message is processed, it will send a message to CountingActor with the value 9, and so on until it receives a 0. Once CountingActor receives a 0, it will print “done!” and send no further messages.

Speed

Actors are extremely lightweight, they are only constrained by memory of which they consume only a few hundred bytes each—this means you can easily create millions of concurrent Actors in a single application. —Hello Akka Tutorial

Using CountingActor above, let’s check how long it takes with some larger numbers.

countingActor ! 1000             // Time: 1 MillisecondscountingActor ! 10000            // Time: 3 MillisecondscountingActor ! 100000           // Time: 26 MillisecondscountingActor ! 1000000          // Time: 277 Milliseconds

One million messages passed through Akka in 277 Milliseconds, that’s extremely fast!

Conclusion

Akka is a very powerful toolkit that allows you to create complex reactive systems without worrying about thread-safeness, or message passing, or failures, etc. Akka provides a clean and simple API to write modern software. Remember, Akka is not a golden hammer, and should not be used to solve every problem. But, if you’re fortunate enough to be working on a problem that can utilize Akka, I highly recommend that you do.

--

--