What is Akka? – A Five-ish Minute Introduction – Who is this guy?



What is Akka? – A Five-ish Minute Introduction – Who is this guy?

0 0


what-is-akka

A 5-ish minute introduction to Akka. A lightning talk I gave at a Chicago Java Users Group meeting.

On Github tginsberg / what-is-akka

What is Akka?

A Five-ish Minute Introduction

Created by Todd Ginsberg for CJUG

Who is this guy?

I just moved here from Austin, TX

So come say hi after this!

I've worked with Java for 20 years.

Caveats!

  • This is a quick five minute introduction, so I'm leaving a lot out!
  • All of my experience with Akka is on personal projects.

OK, so what IS Akka?

“Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM.”

...and it does this with the Actor Concurrency model.

OK, so how does that happen? First let's look at what we're trying to avoid.

Thread-based Concurrency

  • Tricky to write/debug.
  • Lends itself to shared state for communication.
  • Errors can lead to states that are unanticipated.
  • Can be tightly coupled and hard to test.

Not to say it's all bad, plenty of great systems written this way.

Just not the only way to solve the problem.

Actor Concurrency

Only way to talk to an actor is to send or receive a message.

Small, atomic units of functionality and state.

Each actor has a parent - heirarchy.

This is what Erlang uses. Ericsson's AXD301 switch had 9 9's (< 1s in 20 years)

How does this help?

  • State is mutated in one place.
  • Within an actor, there are no race conditions.
  • Easy to reason about what an Actor is doing.
  • Loosely coupled and location independent.
  • Easy to test.
  • Actors take up almost no space - millions in a VM.

Basic Example

Actor:

class CounterActor extends Actor with ActorLogging {
  var counter = 0

  def receive = {
    case Increment =>
      counter += 1
      sender ! CounterState(counter)
    case Decrement =>
      counter -= 1
      sender ! CounterState(counter)
  }
}

Sending a Message:

val actor = system.actorOf(CounterActor.props, "myCounter")
actor ! Increment

Point out var.

! == send

Props - not shown here but used to create actors with properties and settings

Well what about errors?

Supervision

Let it crash!

  • Each actor has a supervisor (its parent).
  • When an actor fails to handle a message properly, the supevisor can...
    • Resume processing keeping current state
    • Restart the actor from scratch
    • Stop the actor
    • Escalate the failure to its parent

These decions can be complicated (fail at most 3 times in 10 seconds, and then restart up to 4 times before escalating).

Akka provides code to help with these

Example Supervisor

override val supervisorStrategy =
  OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
    case _: ArithmeticException      => Resume
    case _: NullPointerException     => Restart
    case _: IllegalArgumentException => Stop
    case _: Exception                => Escalate
  }
(Borrowed from the Akka documentation!)

So What?

  • Separation of actor code from failure handling.
  • Separation of decision making from doing the handling.

Separation of concerns - deciding vs. doing. Akka does the restart/stop/etc

Contrast this with traditional exception handling.

Restart/Stop apply to all children.

Loose coupling between the strategy and actually carrying out the work of recovery.

Out-of-the-box Patterns

Ask

Used when you want to wait for the answer

val futureAnswer = actor ? Increment
val result = Await.result(futureAnswer, 10 seconds)
println(result)

Routers

Many different strategies supported.

Or write your own!

And Much More!

  • Finite State Machines
  • Event Sourcing (Persistence)
  • Circuit Breaker
  • Clustering and Failover
  • Many options for most components

What Now?

Visit http://akka.io for very comprehensive documentation and examples.

FIN

Thanks!

What is Akka? A Five-ish Minute Introduction Created by Todd Ginsberg for CJUG