Share
Embed
Transcript
62 slides
-
manning.com/roestenburg 12mp25 for 42% Off! Early Akka adopter (v0.7, 2010)
Akka Committer (akka-camel in 2.x) Co-author of Akka in Action -
“The free lunch is over” / TANSTAAFL Herb Sutter 2005
http://www.gotw.ca/publications/concurrency-ddj.htm Number of transistors Clock Speed -
Can’t I just.. Parallelize that Sh*t™? Put that in a
box! Something like this? Parallel Version 9 Hey Oracle don’t sue me, of course it’s fake -
Nope. Parallelize that Sh*t™ Not every algorithm can be parallelized
Dynamic coordination (concurrent) Amdahl’s Law Sequential fraction determines max speedup -
Threads? Shared (mutable) state + locks Require synchronization Diminishing returns**
Which primitives to use? Difficult to predict & maintain Deadlock Livelock Thread starvation Race condition *Using threads directly is meant here **Performance improvement degrades with more threads than cores. -
“Life is too short for Blocking code”* *Mario Fusco just
the other day on twitter -
Why Async? Less latency. Ser Service A Service B Service
C 4 sec 6 sec 3 sec Sync takes 13 seconds Async takes max 6 seconds Asynchronous ‘first completed response’ is easy to do -
1. The world is concurrent 2. Things in the world
don't share data 3. Things communicate with messages 4. Things fail Joe Armstrong (creator of Erlang) -
We need better tools to build.. Reactive* Applications * The
latest need-to-know buzz word! -
Scale Up and Out Concurrent Distributed Many Nodes Many Threads
Akka A uniform model for both up and out, embraces concurrency and failure Maximize the use of cores _and_ nodes. Concurrent (more Cores) Distributed (more Nodes) -
Scale Up Processes (even less**) Actors (2.7 million / 1GB)
Threads (4096 / 1GB) Actors are small* * Disclaimer: text is not shown to scale, actors are way smaller.. ** Especially JVM processes -
Mailbox Actor Instance ActorRef Mailbox Processes Messages asynchronously* Invokes Actor
Instance with message *The Mailbox runs on a Dispatcher that abstracts threading -
Actor Instance Actor Instance ActorRef Mailbox Receives messages one at
a time Your code runs here. -
Actor Instance Crashed Actor Instance Mailbox An Actor Instance crashes
instead of handling Exceptions. A Supervisor decides it’s fate. ActorRef Restart / Resume / Stop / Escalate New Actor Instance -
ActorRef Actor Instance Mailbox Immutable. Only the ActorRef is accessible
From the outside. ActorRef -
ActorRef Actor Instance Mailbox The ActorRef is used for both
local and remote actors. ActorRef -
ActorRef Actor Instance Mailbox The ActorRef always points 1 on
1 to the ‘same’ Actor instance. (Alive or Restarted) ActorRef Actor Instance -
ActorRef Dead Actor Instance Mailbox The ActorRef points to deadletters
ActorRef when the actor is dead (terminated). ActorRef deadletters -
msg msg Immutable messages msg Location Transparency msg Serialized messages
Actor System Actor System Actor System Actor systems Event -
Immutable messages msg msg app@node2 app@node1 app@node3 “app” clustered actor
system On three nodes Clustered actor system msg msg -
“GoTicks.com” Build a REST API PUT /events { “event” :
“RHCP”, “nrOfTickets”: 300 } GET /ticket/RHCP Response: { “event”: “RHCP”, “nr”: 1 } GET /events Response: [ { “event”: “RHCP”, “nrOfTickets”: 299 } ] -
RestApi Ticket Seller Ticket Seller BoxOffice GoTicks.com Spray & Akka
“goticks” ActorSystem REST Spray-can -
GoTicks.com REST Interface Handles HTTP calls Translates to and from
JSON RestApi REST Spray-can -
GoTicks.com BoxOffice REST Spray-can BoxOffice Creates Events Creates TicketSeller One
for every Event Partition / shard per event Many sharding strategies possible -
GoTicks.com Ticket Seller Ticket Seller REST Spray-can TicketSeller Contains list
of Tickets Sells Tickets for a Event until SoldOut -
RestApi Ticket Seller Ticket Seller BoxOffice Clustered GoTicks.com Ticket Seller
Ticket Seller BoxOffice Consistent Hashing Consistent Hashing -
Inside our Main class… val system = new ActorSystem(“goticks”) Create
The system ActorSystem(“goticks”) -
Inside Main… val system = new ActorSystem(“goticks”) val api =
system.actorOf( Props[RestInterface], “httpInterface”) … CREATE RestInterface ActorSystem(“goticks”) Top Level Actor ActorSystem(“goticks”) REST Interface -
Inside Main… val system = new ActorSystem(“goticks”) val api =
system.actorOf( Props[RestInterface], “httpInterface”) … CREATE RestInterface ActorSystem(“goticks”) Factory/Creator/ Immutable Configuration object ActorSystem(“goticks”) REST Interface -
Inside Main… val system = new ActorSystem(“goticks”) val api =
system.actorOf( Props[RestInterface], “httpInterface”) … CREATE RestInterface ActorSystem(“goticks”) ActorRef to the Actor ActorSystem(“goticks”) REST Interface -
ActorSystem(“goticks”) ActorSystem(“goticks”) Inside RestInterface.. val master = context.actorOf( Props[BoxOffice],”boxOffice” )
1. CREATE BoxOffice REST Interface BoxOffice Child of REST Interface Actor -
ActorSystem(“goticks”) ActorSystem(“goticks”) Inside RestInterface.. val master = context.actorOf( Props[BoxOffice],”boxOffice” )
1. CREATE BoxOffice REST Interface BoxOffice An Actor has a name, part of an ActorPath -
Inside BoxOffice… val child = context.actorOf( Props[TicketSeller], eventName ) 1.
CREATE TicketSeller ActorSystem(“goticks”) ActorSystem(“goticks”) REST Interface BoxOffice TicketSeller Child of BoxOffice Actor -
Inside BoxOffice… val child = context.actorOf( Props[TicketSeller], eventName ) 1.
CREATE TicketSeller ActorSystem(“goticks”) ActorSystem(“goticks”) REST Interface BoxOffice TicketSeller Ticket sellers are named by event -
Receiving messages class BoxOffice extends Actor { def receive =
{ case TicketRequest(name) => … case GetEvents => … case event: Event => … } } Pattern Matching On Messages -
Receiving messages class TicketSeller extends Actor { def receive =
soldOut def soldOut:Receive = { case Tickets(newTickets) => .. //store new tickets become(selling) } def selling:Receive = { case BuyTicket(name) => … } } Partial Function 3. BECOME: You can change th behavior at every msg -
Create Event ActorSystem(“goticks”) ActorSystem(“goticks”) REST Interface BoxOffice Event(“RHCP”, 250) {
event: “RHCP” nrOfTickets: 250} Inside RestInterface… (receives JSON, unmarshall to event) boxOffice ! event -
Create Event ActorSystem(“goticks”) ActorSystem(“goticks”) REST Interface BoxOffice Event(“RHCP”, 250) {
event: “RHCP” nrOfTickets: 250} Inside RestInterface… (receives JSON, unmarshall to event) boxOffice ! event Bang Operator ‘Tell’ ‘One-way’ -
ActorSystem(“goticks”) ActorSystem(“goticks”) REST Interface BoxOffice Event(“RHCP”, 250) { event: “RHCP”
nrOfTickets: 250} Inside RestInterface… boxOffice ! event Receiver can respond to ‘sender’ Send Event To BoxOffice -
ActorSystem(“goticks”) ActorSystem(“goticks”) REST Interface BoxOffice EventCreated Inside BoxOffice… sender !
EventCreated Respond with EventCreated, results in OK HTTP OK -
ActorSystem(“goticks”) ActorSystem(“goticks”) REST Interface BoxOffice { event: “RHCP” nrOfTickets: 250}
BuyTicket TicketSeller Inside BoxOffice… ticketSeller forward BuyTicket Get a Ticket Forward keeps sender (RestInterface) TicketRequest(“RHCP”) -
ActorSystem(“goticks”) ActorSystem(“goticks”) REST Interface BoxOffice { event: “RHCP”, nr :1}
TicketSeller Inside TicketSeller… sender ! ticket Respond to original sender BuyTicket Ticket(“RHCP”, 1) -
“Async sending subscription data” Send Thread Consumer Consumer Consumer Thread
Thread Thread Service A -
Send Thread Consumer Consumer Consumer Thread Thread Thread Service A
Thread 2 fails Handle every possible error inside Thread 2, never fail? Not completely under your control. -
Send Thread Consumer Consumer Consumer Thread Thread Thread Service A
Thread 2 fails Restart / Stop? Monitor? Handle errors across all subscriptions? -
4. Supervise* Untangle behavior and errors ‘Just Reboot’ Let It
Crash ™ Error Kernel -
4. Supervise* val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 5, withinTimeRange =
1 minute) { case _: Exception => Restart } -
4. Supervise* val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 5, withinTimeRange =
1 minute) { case _: Exception => Restart } Restart, Resume, Escalate, Stop,