Above the Clouds:
 Introducing Akka
       Jonas Bonér
         CTO Typesafe
        Twitter: @jboner

presented by Martin   Odersky
The problem

It is way too hard to build:
1. correct highly concurrent systems
2. truly scalable systems
3. fault-tolerant systems that self-heals
...using “state-of-the-art” tools
akka
Introducing
Vision

Simpler
     Concurrency
     Scalability
     Fault-tolerance
Vision

...with a single unified
     Programming model
     Runtime service
Manage system overload
Scale up & Scale out
Replicate and distribute
  for fault-tolerance
Transparent load balancing
ARCHITECTURE



           CORE
          SERVICES
ARCHITECTURE



         ADD-ON
         MODULES
ARCHITECTURE



          CLOUDY
           AKKA
WHERE IS AKKA USED?
                      SOME EXAMPLES:

FINANCE                                 TELECOM
•   Stock trend Analysis & Simulation
                                        •   Streaming media network gateways

•   Event-driven messaging systems
                                        SIMULATION
BETTING & GAMING                        •   3D simulation engines

•   Massive multiplayer online gaming
                                        E-COMMERCE
•   High throughput and transactional
                                        •   Social media community sites
    betting
What is an Actor?
Actor

Behavior



 State
Event-
 driven

Actor

Behavior



 State
Event-
 driven

Actor

Behavior



 State
Event-
 driven

Actor

Behavior



 State
Event-
 driven

Actor

Behavior



 State
Event-
 driven

Actor

Behavior



 State
Actor

Behavior



 State
Event-
 driven

Actor

Behavior



 State
Akka Actors
one tool in the toolbox
Actors
case object Tick

class Counter extends Actor {
 var counter = 0

    def receive = {
      case Tick =>
       counter += 1
       println(counter)
    }
}
Create Actors

val counter = actorOf[Counter]




  counter is an ActorRef
Start actors

val counter = actorOf[Counter].start
Stop actors

val counter = actorOf[Counter].start
counter.stop
Send: !

counter ! Tick




   fire-forget
Send: !!!
// returns a future
val future = actor !!! Message
future.await
val result = future.result




    returns the Future directly
Future
val future1, future2, future3 =
 Future.empty[String]

future1.await
future2.onComplete(f => ...)

future1.completeWithResult(...)
future2.completeWithException(...)
future3.completeWith(future2)
Future

val f1 = Futures.future(callable)

val f2 = Futures.firstCompletedOf(futures)
val f3 = Futures.reduce(futures)((x, y) => ..)
val f4 = Futures.fold(zero)(futures)((x, y) => ...)
Future
val future1 = for {
  a: Int <- actor !!! "Hello" // returns 5
  b: String <- actor !!! a    // returns "10"
  c: String <- actor !!! 7    // returns "14"
} yield b + "-" + c

val future2 = for {
  a <- actor !!! Req("Hello") collect { case Res(x: Int) => x }
  b <- actor !!! Req(a)     collect { case Res(x: String) => x }
  c <- actor !!! Req(7)     collect { case Res(x: String) => x }
} yield b + "-" + c
Dataflow
import Future.flow

val x, y, z = Promise[Int]()

flow {
  z << x() + y()
  println("z = " + z())
}
flow { x << 40 }
flow { y << 2 }
Send: !!

val result = (actor !! Message).as[String]




 uses Future under the hood and blocks until
           timeout or completion
Reply
class SomeActor extends Actor {
  def receive = {
    case User(name) =>
     // use reply
     self.reply(“Hi ” + name)
  }
}
HotSwap
self become {
  // new body
  case NewMessage =>
   ...
}
HotSwap
actor ! HotSwap {
  // new body
  case NewMessage =>
   ...
}
HotSwap

self.unbecome()
Set dispatcher
class MyActor extends Actor {
 self.dispatcher = Dispatchers
   .newThreadBasedDispatcher(self)

    ...
}

actor.dispatcher = dispatcher // before started
Remote Actors
Remote Server
// use host & port in config
Actor.remote.start()

Actor.remote.start("localhost", 2552)




 Scalable implementation based on
      NIO (Netty) & Protobuf
Two types of
remote actors
Client initiated & managed
Server initiated & managed
Client-managed
    supervision works across nodes


import Actor._

val service = remote.actorOf[MyActor](host, port)

service ! message
Server-managed
register and manage actor on server
  client gets “dumb” proxy handle

 import Actor._

 remote.register(“service:id”, actorOf[MyService])




                    server part
Server-managed
 val service = remote.actorFor(
  “service:id”,
  “darkstar”,
  9999)

 service ! message



           client part
Server-managed
 import Actor._

 remote.register(actorOf[MyService])
 remote.registerByUuid(actorOf[MyService])
 remote.registerPerSession(
  “service:id”, actorOf[MyService])

 remote.unregister(“service:id”)
 remote.unregister(actorRef)




            server part
Remoting in Akka 1.1
              Remote Actors
               Client-managed
               Server-managed

                  Problem
Deployment (local vs remote) is a dev decision
  We get a fixed and hard-coded topology
  Can’t change it dynamically and adaptively

           Needs to be a
    deployment & runtime decision
Clustered Actors
(in development for upcoming Akka 2.0)
Address

val actor = actorOf[MyActor](“my-service”)




     Bind the actor to a virtual address
Deployment
•Actor address is virtual and decoupled from how it
 is deployed
•If no deployment configuration exists then actor is
 deployed as local
•The same system can be configured as distributed
 without code change (even change at runtime)

•Write as local but deploy as distributed in the
 cloud without code change
•Allows runtime to dynamically and adaptively
 change topology
Deployment configuration
   akka {
     actor {
       deployment {
         my-service {
           router = "least-cpu"
           clustered {
             home      = "node:test-node-1"
             replicas = 3
             stateless = on
           }
         }
       }
     }
   }
Deployment configuration
   akka {                  Address
     actor {
       deployment {
         my-service {
           router = "least-cpu"
           clustered {
             home      = "node:test-node-1"
             replicas = 3
             stateless = on
           }
         }
       }
     }
   }
Deployment configuration
   akka {                  Address
     actor {                             Type of load-
       deployment {
         my-service {
           router = "least-cpu"
           clustered {
             home      = "node:test-node-1"
             replicas = 3
             stateless = on
           }
         }
       }
     }
   }
Deployment configuration
       akka {                  Address
         actor {                             Type of load-
           deployment {
             my-service {
               router = "least-cpu"
               clustered {
                 home      = "node:test-node-1"
Clustered replicas = 3
 or Local        stateless = on
               }
             }
           }
         }
       }
Deployment configuration
       akka {                  Address
         actor {                             Type of load-
           deployment {
             my-service {
               router = "least-cpu"
               clustered {
                 home      = "node:test-node-1"
Clustered replicas = 3
 or Local        stateless = on
               }                                Home node
             }
           }
         }
       }
Deployment configuration
       akka {                  Address
         actor {                             Type of load-
           deployment {
             my-service {
               router = "least-cpu"
               clustered {
                 home      = "node:test-node-1"
Clustered replicas = 3
 or Local        stateless = on
               }                                  Home node
             }
           }
         }                                   Nr of replicas
       }                                        in cluster
Deployment configuration
       akka {                  Address
         actor {                             Type of load-
           deployment {
             my-service {
               router = "least-cpu"
               clustered {
                 home      = "node:test-node-1"
Clustered replicas = 3
 or Local        stateless = on
               }                                  Home node
             }
           }
         }
      Stateful or                            Nr of replicas
       }                                        in cluster
       Stateless
The runtime provides
•Subscription-based cluster membership service
•Highly available cluster registry for actors
•Automatic cluster-wide deployment
•Highly available centralized configuration service
•Automatic replication with automatic fail-over upon
 node crash
•Transparent and user-configurable load-balancing
•Transparent adaptive cluster rebalancing
•Leader election
•Durable mailboxes - guaranteed delivery
Upcoming features

•Publish/Subscribe (broker and broker-less)
•Compute Grid (MapReduce)
•Data Grid (querying etc)
•Distributed STM (Transactional Memory)
•Event Sourcing
Akka Node
Akka Node
val ping = actorOf[Ping](“ping”)
val pong = actorOf[Pong](“pong”)

ping ! Ball(pong)
Akka Node
 val ping = actorOf[Ping](“ping”)
 val pong = actorOf[Pong](“pong”)

 ping ! Ball(pong)




Ping                                Pong
Akka Node

    Akka
Cluster Node




               Ping   Pong
Akka
                               Cluster Node



   Akka Node

    Akka                                                        Akka
Cluster Node                                                Cluster Node




                        Ping                  Pong




             Akka                                    Akka
         Cluster Node                            Cluster Node
Akka
                           Cluster Node




    Akka                                                   Akka
Cluster Node                                           Cluster Node




                    Ping                  Pong




         Akka                                   Akka
     Cluster Node                           Cluster Node
Akka
                                 Cluster Node


                      akka {
    Akka                actor {                                        Akka
                          deployment {
Cluster Node               ping {}                                 Cluster Node
                           pong {
                             router = "round-robin"
                             clustered {
                               replicas = 3
                               stateless = on
                             }
                           }
                    Ping}                             Pong
                        }
                      }




         Akka                                               Akka
     Cluster Node                                       Cluster Node
Akka
                               Cluster Node


                    akka {
    Akka              actor {                                         Akka
    Ping                deployment {
Cluster Node              ping {}                                 Cluster Node
                          pong {
                            router = "round-robin"
                            clustered {
                              replicas = 3
                              stateless = on
                            }
                          }
                        }                            Pong
                      }
                    }




         Akka                                              Akka
     Cluster Node                                      Cluster Node
Akka
                                   Pong
                               Cluster Node


                    akka {
    Akka              actor {                                       Akka
    Ping                deployment {                                Pong
Cluster Node              ping {}                               Cluster Node
                          pong {
                            router = "round-robin"
                            clustered {
                              replicas = 3
                              stateless = on
                            }
                          }
                        }
                      }
                    }




         Akka                                            Akka
         Pong
     Cluster Node                                    Cluster Node
Akka
                                   Pong
                               Cluster Node


                    akka {
    Akka              actor {                                       Akka
    Ping                deployment {                                Pong
Cluster Node              ping {}                               Cluster Node
                          pong {
                            router = "round-robin"
                            clustered {
                                 ZooKeeper
                                    ZooKeeper
                              replicas = 3
                                  ZooKeeper
                              stateless = on
                                     Ensemble
                            }
                          }
                        }
                      }
                    }




         Akka                                            Akka
         Pong
     Cluster Node                                    Cluster Node
Let it crash
fault-tolerance
The

Erlang
 model
9   nines
...let’s take a
standard OO
 application
Which components have
critically important state
            and
 explicit error handling?
Classification of State
• Scratch data
• Static data
 • Supplied at boot time
 • Supplied by other components
• Dynamic data
 • Data possible to recompute

 • Input from other sources; data
 that is impossible to recompute
 •
Classification of State
• Scratch data
• Static data
 • Supplied at boot time
 • Supplied by other components
• Dynamic data
 • Data possible to recompute

 • Input from other sources; data
 that is impossible to recompute
 •
Classification of State
• Scratch data
• Static data
 • Supplied Must time
              at boot be
 • Supplied by other components
            protected
• Dynamic data
 • Databy anyto recompute
          possible means


 • Input from other sources; data
 that is impossible to recompute
 •
Fault-tolerant
onion-layered
 Error Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Error
Kernel
Node 1   Node 2
Linking
link(actor)
unlink(actor)

startLink(actor)
spawnLink[MyActor]
Fault handlers
 AllForOneStrategy(
  errors,
  maxNrOfRetries,
  withinTimeRange)

 OneForOneStrategy(
  errors,
  maxNrOfRetries,
  withinTimeRange)
Supervision
class
Supervisor
extends
Actor
{


faultHandler
=
AllForOneStrategy(




List(classOf[IllegalStateException])





5,
5000))



def
receive
=
{




case
Register(actor)
=>
link(actor)


}
}
Manage failure
class FaultTolerantService extends Actor {
  ...
  override def preRestart(reason: Throwable) = {
    ... // clean up before restart
  }
  override def postRestart(reason: Throwable) = {
    ... // init after restart
  }
}
...and much much more
          STM
                             FSM
HTTP                Camel
         Microkernel        Guice
OSGi
           Dataflow
                         AMQP
scalaz     Spring      Security
Get it and learn more
       http://akka.io
EOF

Above the clouds: introducing Akka

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 \n
  • #5 \n
  • #6 \n
  • #7 \n
  • #8 \n
  • #9 \n
  • #10 \n
  • #11 \n
  • #12 \n
  • #13 \n
  • #14 \n
  • #15 \n
  • #16 \n
  • #17 \n
  • #18 \n
  • #19 \n
  • #20 \n
  • #21 \n
  • #22 \n
  • #23 \n
  • #24 \n
  • #25 Scalable: Scala stands for SCALAble language which means that the language is designed to scale with the usage. Concepts and abstractions scale from small to larges\nComponent-Oriented: scala was initially designed as a language to do component oriented programming, with its requirements on reusability, maintainability, orthogonality and extensibility.\nCoherent: Scala has managed to build a coherent whole while merging the FP and OO paradigms.\nHigh level: Scala allows you to program it to very high level utilizing its powerful abstractions both in OO and FP\nExtensible: It is great for building Domain Specific Languages (DSLs). Since Scala has a good type inference, a very flexible syntax which gives it a dynamic feel similar to Ruby and Python.\n
  • #26 \n
  • #27 \n
  • #28 \n
  • #29 \n
  • #30 \n
  • #31 \n
  • #32 \n
  • #33 \n
  • #34 \n
  • #35 \n
  • #36 \n
  • #37 \n
  • #38 \n
  • #39 \n
  • #40 \n
  • #41 \n
  • #42 Scalable: Scala stands for SCALAble language which means that the language is designed to scale with the usage. Concepts and abstractions scale from small to larges\nComponent-Oriented: scala was initially designed as a language to do component oriented programming, with its requirements on reusability, maintainability, orthogonality and extensibility.\nCoherent: Scala has managed to build a coherent whole while merging the FP and OO paradigms.\nHigh level: Scala allows you to program it to very high level utilizing its powerful abstractions both in OO and FP\nExtensible: It is great for building Domain Specific Languages (DSLs). Since Scala has a good type inference, a very flexible syntax which gives it a dynamic feel similar to Ruby and Python.\n
  • #43 \n
  • #44 \n
  • #45 \n
  • #46 \n
  • #47 \n
  • #48 \n
  • #49 \n
  • #50 Scalable: Scala stands for SCALAble language which means that the language is designed to scale with the usage. Concepts and abstractions scale from small to larges\nComponent-Oriented: scala was initially designed as a language to do component oriented programming, with its requirements on reusability, maintainability, orthogonality and extensibility.\nCoherent: Scala has managed to build a coherent whole while merging the FP and OO paradigms.\nHigh level: Scala allows you to program it to very high level utilizing its powerful abstractions both in OO and FP\nExtensible: It is great for building Domain Specific Languages (DSLs). Since Scala has a good type inference, a very flexible syntax which gives it a dynamic feel similar to Ruby and Python.\n
  • #51 \n
  • #52 \n
  • #53 \n
  • #54 \n
  • #55 \n
  • #56 \n
  • #57 \n
  • #58 \n
  • #59 \n
  • #60 \n
  • #61 \n
  • #62 \n
  • #63 \n
  • #64 \n
  • #65 \n
  • #66 \n
  • #67 \n
  • #68 \n
  • #69 \n
  • #70 \n
  • #71 \n
  • #72 \n
  • #73 \n
  • #74 \n
  • #75 \n
  • #76 \n
  • #77 \n
  • #78 \n
  • #79 \n
  • #80 \n
  • #81 \n
  • #82 \n
  • #83 \n
  • #84 \n
  • #85 \n
  • #86 \n
  • #87 Scalable: Scala stands for SCALAble language which means that the language is designed to scale with the usage. Concepts and abstractions scale from small to larges\nComponent-Oriented: scala was initially designed as a language to do component oriented programming, with its requirements on reusability, maintainability, orthogonality and extensibility.\nCoherent: Scala has managed to build a coherent whole while merging the FP and OO paradigms.\nHigh level: Scala allows you to program it to very high level utilizing its powerful abstractions both in OO and FP\nExtensible: It is great for building Domain Specific Languages (DSLs). Since Scala has a good type inference, a very flexible syntax which gives it a dynamic feel similar to Ruby and Python.\n
  • #88 Scalable: Scala stands for SCALAble language which means that the language is designed to scale with the usage. Concepts and abstractions scale from small to larges\nComponent-Oriented: scala was initially designed as a language to do component oriented programming, with its requirements on reusability, maintainability, orthogonality and extensibility.\nCoherent: Scala has managed to build a coherent whole while merging the FP and OO paradigms.\nHigh level: Scala allows you to program it to very high level utilizing its powerful abstractions both in OO and FP\nExtensible: It is great for building Domain Specific Languages (DSLs). Since Scala has a good type inference, a very flexible syntax which gives it a dynamic feel similar to Ruby and Python.\n
  • #89 Scalable: Scala stands for SCALAble language which means that the language is designed to scale with the usage. Concepts and abstractions scale from small to larges\nComponent-Oriented: scala was initially designed as a language to do component oriented programming, with its requirements on reusability, maintainability, orthogonality and extensibility.\nCoherent: Scala has managed to build a coherent whole while merging the FP and OO paradigms.\nHigh level: Scala allows you to program it to very high level utilizing its powerful abstractions both in OO and FP\nExtensible: It is great for building Domain Specific Languages (DSLs). Since Scala has a good type inference, a very flexible syntax which gives it a dynamic feel similar to Ruby and Python.\n
  • #90 \n
  • #91 \n
  • #92 \n
  • #93 \n
  • #94 \n
  • #95 \n
  • #96 \n
  • #97 \n
  • #98 \n
  • #99 \n
  • #100 \n
  • #101 \n
  • #102 \n
  • #103 \n
  • #104 \n
  • #105 \n
  • #106 \n
  • #107 \n
  • #108 \n
  • #109 \n
  • #110 \n
  • #111 \n
  • #112 \n
  • #113 \n
  • #114 \n
  • #115 \n
  • #116 \n
  • #117 \n
  • #118 \n
  • #119 \n
  • #120 \n
  • #121 \n
  • #122 \n
  • #123 \n
  • #124 \n
  • #125 \n
  • #126 \n
  • #127 \n
  • #128 \n
  • #129 \n
  • #130 \n
  • #131 \n
  • #132 \n
  • #133 \n
  • #134 \n
  • #135 \n
  • #136 \n
  • #137 \n
  • #138 \n
  • #139 \n
  • #140 \n
  • #141 \n
  • #142 \n
  • #143 \n
  • #144 \n
  • #145 \n
  • #146 \n
  • #147 \n
  • #148 \n
  • #149 \n
  • #150 \n
  • #151 \n
  • #152 \n
  • #153 \n
  • #154 \n
  • #155 \n
  • #156 \n
  • #157 \n
  • #158 \n
  • #159 \n
  • #160 \n
  • #161 \n
  • #162 \n
  • #163 \n
  • #164 \n
  • #165 \n
  • #166 \n
  • #167 \n
  • #168 \n
  • #169 \n
  • #170 \n
  • #171 \n
  • #172 \n
  • #173 \n
  • #174 \n
  • #175 \n
  • #176 \n
  • #177 \n
  • #178 \n
  • #179 \n
  • #180 \n
  • #181 \n
  • #182 \n
  • #183 \n
  • #184 \n
  • #185 \n
  • #186 \n
  • #187 \n
  • #188 \n
  • #189 \n
  • #190 \n
  • #191 \n
  • #192 \n
  • #193 \n
  • #194 \n
  • #195 \n
  • #196 \n
  • #197 Scalable: Scala stands for SCALAble language which means that the language is designed to scale with the usage. Concepts and abstractions scale from small to larges\nComponent-Oriented: scala was initially designed as a language to do component oriented programming, with its requirements on reusability, maintainability, orthogonality and extensibility.\nCoherent: Scala has managed to build a coherent whole while merging the FP and OO paradigms.\nHigh level: Scala allows you to program it to very high level utilizing its powerful abstractions both in OO and FP\nExtensible: It is great for building Domain Specific Languages (DSLs). Since Scala has a good type inference, a very flexible syntax which gives it a dynamic feel similar to Ruby and Python.\n
  • #198 \n
  • #199 Scalable: Scala stands for SCALAble language which means that the language is designed to scale with the usage. Concepts and abstractions scale from small to larges\nComponent-Oriented: scala was initially designed as a language to do component oriented programming, with its requirements on reusability, maintainability, orthogonality and extensibility.\nCoherent: Scala has managed to build a coherent whole while merging the FP and OO paradigms.\nHigh level: Scala allows you to program it to very high level utilizing its powerful abstractions both in OO and FP\nExtensible: It is great for building Domain Specific Languages (DSLs). Since Scala has a good type inference, a very flexible syntax which gives it a dynamic feel similar to Ruby and Python.\n