The typesafe console

Akka - The most important middleware you’ve never heard of.

Stop me if you’ve heard this one. You just got off the phone with your boss, who is getting frantic phone calls about your application throwing errors all over the place, running extremely slowly, and in general causing massive headaches. You check your pool of app servers, and find that all of the threads are blocked on I/O. It’s 6am, and you haven’t even fired up the coffee maker, and already existential dread is filling your brain.

Java web app developers know all about this scenario— Java web frameworks have, historically, been extremely poor at concurrency. The number one cry of Struts or JSF developers is “just turn on sticky sessions”, and then only a percentage of your users will get hosed when things start going wrong at the controller level. Want to write a controller that connects to a third-party REST service which happens to reject a connection from time to time? Beati pauperes spiritu.

In 1973, Carl Hewitt, Peter Bishop, and Richard Steiger wrote “A Universal Modular Actor Formalism for Artificial Intelligence”, in which the Actor model was born. An actor, in 21st-century-web parlance, is a thread, or process, or worker, which receives messages and performs a piece of work based upon that message, including being able to pass the message along to another actor, respond to the original message, or even create new actors to deal with the message. The model lends itself extremely well to modern system architecture, where we have highly distributed processing.

Enter Akka, a piece of middleware that brings the Actor model to your Java or Scala web applications. Akka is extremely fast, extremely concurrent, and it takes care of a lot of low-level IO operations and code that have historically caused web app developers to throw up their hands and say “just throw more boxes at it”. Akka is made by Typesafe, the same guys who are pushing Scala and the Play framework (which itself is built upon Akka). If you know about Twitter’s finagle library, Akka is like that, only much, much easier to use.

Want to asynchronously log some action to a database? Send a message to Akka and forget about it. Want to fail gracefully when that REST service refuses your connection? Have your Akka actor schedule the original message to be placed back into the mailbox, and you’re done. Want to share sessions between application servers? Ask an Akka actor to retrieve a key from a cache, and you’re done. Want to recreate the Quartz functionality of JBoss? Schedule an Akka actor to run every so often with one line of code. Want to resize an image as a background task? Send an Akka actor a message containing the bytes of the image, return a Future containing the bytes of the resized image, and you’re done.

Akka is designed to be bulletproof. Ever have an exception that you’d like to immediately retry? Akka makes that easy with its Supervisor “strategies”. You can configure the system to take action in the case of an actor crashing or throwing an exception. Want to restart the actor? No problem. Want to just stop the actor? No problem. Want to escalate the exception up the stack? No problem. Either way, the main actor system won’t stop just because of an exception. It’s actually very hard to write a system using Akka that isn’t fault tolerant.

Clustering in Akka is still marked as “experimental” as of right now, but you can go ahead and use clustered actors in production with no problems. Actor systems can start up, discover, and automatically join a cluster with only a few lines of configuration. You can use remote Akka actor systems right now, again with only a few lines of configuration, and your code won’t change.

At Heluna, we use Akka exclusively for all e-mail message processing. When an e-mail enters the system, we send the metadata and content of that e-mail out to various actor systems in parallel. In the past, all of these operations were serial, and they all blocked waiting for each other (sound familiar, Java app devs?). Now, with Akka, e-mail is processed much, much faster, and the act of adding capacity to our overall system means spinning up additional instances of actor systems at whichever tier we need.

So, Java developers, Scala developers, if you haven’t looked at Akka, dig into it today. The amount of functionality and architectural sanity that Akka brings to the table will eliminate those 6am phone calls from your boss, and you’ll be able to grow your application very quickly and very easily. It’s the most important middleware that you’ve never heard of.

Like what you read? Give uosəəq ʞɹɐɯ a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.