Dustin's Pages

Thursday, October 4, 2012

JavaOne 2012: Up, Up, and Out: Scaling Software with Akka

After the late-ending Community Keynote, I headed to Hilton Golden Gate 3/4/5 to see Viktor Klang's (Typesafe) "Up, Up and Out: Akka" presentation. Klang is the technical lead on Akka. Akka is a "beautiful mountain in northern Sweden," is a Goddess, and is a Scala-based "toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM." Akka is not exclusive to Scala only and can be used "from Java today." Using Akka with Scala does allow you to do some things you cannot do with Akka when used with Java.

Akka is used by many large companies to solve real problems. Akka is built to scare in many directions and to provide extreme flexibility. One of Akka's goals is to "manage system overload."

Akka uses Actors: "Akka's unit of code organization." According to Klang, "Actors help you create concurrent, scalable, and fault-tolerant applications." Actors "keep many 'policy decisions' separate from the business logic." Actors originated in 1970s ("like all cool stuff in computer science") and Erlang has been using actors with "great success" for several years.

Klang warned to avoid "thinking in terms of shared state, a leaky abstraction." He added that threads and locks are "a means of execution" rather than structural. He said this mixes execution with business logic. Concurrent collections are good optimizations for local uses.

Actors are "distributable by design" and Klang had a slide listing several bullets explaining this statement. He stated that an actor can be an alternative to "a thread," "an object instance," "a callback or listener," "a singleton or service," "a router, load-balancer, or pool," "Java EE session bean or Message-Driven Bean," "out of process service," and "finite state machines."

Klang showed code examples in my preferred format in a presentation: embedded in his slides with color syntax highlighting. He showed step 0 ("DEFINE") in which his code defined the Actor's class and the Actor's behavior.

Once defined, the first operation (I - "CREATE") "creates a new instance of an Actor." The created Actor is extremely lightweight and its "state and behavior are indistinguishable from each other." He drove this last point home: "The only way to observe state is by sending an actor a message and seeing how the actor reacts." The Actor is "very strong encapsulation" of state, behavior, and message queue.

Akka provides an ActorSystem for creating Akka Actor instances. An instance of Props is provided to the Actor because actors need props.

Step 2 ("SEND") involves "sending a message to an Actor" and "everything happens reactively" and "everything is asynchronous and lockless." Akka supports a "fire and forget" mode with the actor's tell method. However, Akka provides guaranteed order of delivery. The reply is implemented in Akka with getSender().tell().

Step 3 ("BECOME") "redefines the Actor's behavior" and is "triggered reactively by receipt of message." The reasons one might want to change the behavior of an actor at runtime include supporting highly contended actor transforming to an actor pool or to implement graceful degradation.

Actors can supervise other Actors, leading to Step 4 ("SUPERVISE"). A "supevisor detects and responds to the failures of the Actor(s) it supervises" and Klang stated that this translates to "a clean separation and processing and error handling."

Klang talked about "failure management in Java, C, and C#" where you are "given a single thread of control." He put it this way in a bullet: "If this thread blows up, you are screwed." The implication of this is that all "explicit error handling" is done "within the single thread" and "tangled up" with the business code.

Klang said the way to deal with error handling is to push the error handling out away from the business logic. He then referenced the onion-layer error kernel. Klang talked about callbacks (preRestart and postRestart) provided for Actors to handle failures.

A Router is a special case of Actor. Klang showed a slide with code using a RoundRobinRouter. He also showed being able to define the deployment scenario outside of the code in configuration file and referencing that from the code with a path. He took this example even further to show code for "remote deployment" specifying a URL with the "akka" protocol, a host name, and a port.

Everything that Klang presented to this point is available today as Akka 2.0. Klang said that there will be Akka Cluster in the to-be-released-soon Akka 2.1. He asked for feedback to ensure that the correct APIs and correct functionality are available for clustering in Akka 2.2. More information on Akka clustering is available in the specification, the user guide, and the code itself.