+ All Categories
Home > Documents > Akka (1)

Akka (1)

Date post: 22-Jan-2018
Category:
Upload: rahul-shukla
View: 326 times
Download: 0 times
Share this document with a friend
50
Akka Rahul Shukla [email protected]
Transcript
Page 1: Akka (1)

Akka Rahul [email protected]

Page 2: Akka (1)

Why ?

Page 3: Akka (1)

Application requirements have changed

Page 4: Akka (1)

New requirements

Page 5: Akka (1)

Apps in the 60s-90s were written for Apps today are written for

Single machines Clusters of machines

Single core processors Multicore processors

Expensive RAM Cheap RAM

Expensive disk Cheap disk

Slow networks Fast networks

Few concurrent users Lots of concurrent users

Small data sets Large data sets

Latency in seconds Latency in milliseconds

Page 6: Akka (1)

As a matter of necessity,businesses are going Reactive

Page 7: Akka (1)
Page 8: Akka (1)

Responsive● Real-time, engaging, rich and collaborative

○ Create an open and ongoing dialog with users○ More efficient workflow; inspires a feeling of connectedness○ Fully Reactive enabling push instead of pull

Page 9: Akka (1)

Elastic

● Scalability and elasticity to embrace the Cloud○ Leverage all cores via asynchronous programming○ Clustered servers support joining and leaving of nodes○ More cost-efficient utilization of hardware

Page 10: Akka (1)

Resilient

● Failure is embraced as a natural state in the app lifecycle○ Resilience is a first-class construct○ Failure is detected, isolated, and managed○ Applications self heal

Page 11: Akka (1)

Message-Driven

● Loosely coupled architecture, easier to extend, maintain, evolve○ Asynchronous and non-blocking○ Concurrent by design, immutable state○ Lower latency and higher throughput

Page 12: Akka (1)

What?

Introducing Akka

Page 13: Akka (1)

The name comes from the goddess in the Sami (native swedes) mythology that represented all the wisdom and beauty in the world.

It is also the name of a beautiful mountain in Laponia in the north part of Sweden

Page 14: Akka (1)

● Message - Driven● Concurrency● Scalability (Elastic)● Fault Tolerance (Resilient)

Vision

Page 15: Akka (1)

Manage System Overload

Page 16: Akka (1)

Scale Up & Scale Out

Page 17: Akka (1)

Program at higher level

Page 18: Akka (1)

● Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc.

● Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system

● You get high CPU utilization, low latency, high throughput and scalability - FOR FREE as part of the model

● Proven and superior model for detecting and recovering from errors

Page 19: Akka (1)

Distributable by Design

Page 20: Akka (1)

● Actors are location transparent & distributable by design● Scale UP and OUT for free as part of the model ● You get the PERFECT FABRIC for the CLOUD

○ elastic & dynamic○ fault-tolerant & self-healing○ adaptive load-balancing, cluster rebalancing & actor

migration ○ build extremely loosely coupled and dynamic systems

that can change and adapt at runtime

Page 21: Akka (1)

How

Can we achieve this ?

Page 22: Akka (1)

Actor

What is an Actor ?

Page 23: Akka (1)

● Akka's unit of code organization is called an Actor● Actors helps you create concurrent, scalable and fault-

tolerant applications● Like Java EE servlets and session beans, Actors is a model

for organizing your code that keeps many “policy decisions” separate from the business logic

● Actors may be new to many in the Java community, but they are a tried-and-true concept (Hewitt 1973) used for many years in telecom systems with 9 nines uptime

Page 24: Akka (1)

What can I use Actors for ?

Page 25: Akka (1)

● In different scenarios, an Actor may be an alternative to: - ○ a thread○ an object instance or component ○ a callback or listener ○ a singleton or service ○ a router, load-balancer or pool ○ a Java EE Session Bean or Message-Driven Bean

Page 26: Akka (1)

Carl Hewitt’s definition

Page 27: Akka (1)

- The fundamental unit of computation that embodies: - Processing - Storage - Communication

- 3 axioms - When an Actor receives a message it can: - Create new Actors - Send messages to Actors it knows - Designate how it should handle the next message it receives

Page 28: Akka (1)

Let’s Code

Page 29: Akka (1)

Define an actor

Scala : import akka.actor._import bank.Bank.Balance

object Bank { case class Balance(number: String) def props(name: String): Props = { return Props(Bank(name)) }}

case class Bank(name: String) extends AbstractLoggingActor { def balance(number: String) = { //Do Something }

override def receive = { case Balance(number) => balance(number) }

}

Page 30: Akka (1)

Define an actor

Java : public class Bank extends AbstractLoggingActor {

public static Props props(String name) { return Props.create(Bank.class, () -> new Bank(name)); }

@Override public PartialFunction<Object, BoxedUnit> receive() { return ReceiveBuilder .match(Balance.class, b -> balance(b.number)) .build();}

private void balance(String number) { //DO Something }}

Page 31: Akka (1)

Create an actor• CREATE - creates a new instance of an Actor• Extremely lightweight (2.7 Million per Gb RAM)• Very strong encapsulation - encapsulates:- state- behavior- message queue

• State & behavior is indistinguishable from each other• Only way to observe state is by sending an actor amessage and see how it reacts

Page 32: Akka (1)

Create an actor

Scala : val system = ActorSystem("Bank")

val bank = system.actorOf(Bank.props("Central Bank"),”CBNK”)

Java : ActorSystem system = ActorSystem.create("Bank");

ActorRef bank = system.actorOf(Bank.props("Central Bank"),”CBNK”);

Page 33: Akka (1)

Actors hierarchies

Bank

User System

/Bank

CBNK /Bank/user/CBNK

/Bank/system/Bank/user

Page 34: Akka (1)

SEND• SEND - sends a message to an Actor• Asynchronous and Non-blocking - Fire-forget• Everything happens Reactively- An Actor is passive until a message is sent to it,which

triggers something within the Actor- Messages is the Kinetic Energy in an Actor system- Actors can have lots of buffered Potential Energy but

can't do anything with it until it is triggered by a message

• EVERYTHING is asynchronous and lockless

Page 35: Akka (1)

SEND

Scala : bank ! Bank.Balance(“12345”)

Java : bank.tell(new Bank.Balance(“12345”), ActorRef.noSender());

Page 36: Akka (1)

SEND

Page 37: Akka (1)

Anatomy of an Actor● Each actor is represented by an ActorRef● Each actor is has a mailbox and a dispatcher● Only one message at a time is passed to the actor

Page 38: Akka (1)

REPLY

Scala : override def receive = { case Balance(number) => sender ! "Message"}

Java : @Override public PartialFunction<Object, BoxedUnit> receive() { return ReceiveBuilder .match(Balance.class, b -> sender().tell(“Message”,self())) .build();}

Page 39: Akka (1)

SUPERVISE (Fault Tolerance)SUPERVISE - manage another Actor’s failures● Error handling in actors is handle by letting Actors

monitor (supervise) each other for failure● This means that if an Actor crashes, a notification will

be sent to his supervisor, who can react upon the failure

● This provides clean separation of processing and error handling

Page 40: Akka (1)

Supervise

Scala : override def supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: Account.OverdrawException => Resume case _: Exception => Restart}

Java : @Overridepublic SupervisorStrategy supervisorStrategy() { return new OneForOneStrategy(DeciderBuilder .match(Account.OverdrawException.class, c -> SupervisorStrategy.resume()) .match(Exception.class, c -> SupervisorStrategy.escalate()) .build());}

Page 41: Akka (1)

Manage Failure

Scala : @throws[Exception](classOf[Exception])override def preStart(): Unit = ???

@throws[Exception](classOf[Exception])override def postRestart(reason: Throwable): Unit = ???

@throws[Exception](classOf[Exception])override def postStop(): Unit = ???

Page 42: Akka (1)

Manage Failure

Java : @Overridepublic void preRestart(Throwable reason, Option<Object> message) throws Exception {// Handle here}

@Overridepublic void postRestart(Throwable reason) throws Exception { // Handle here}

@Overridepublic void postStop() throws Exception { // Handle here}

Page 43: Akka (1)

Supervisor Strategies

● Akka ships with two highly configurable supervisor strategies:

○ OneForOneStrategy: Only the faulty child is affected when it fails

○ AllForOneStrategy: All children are affected when one child fails

Page 44: Akka (1)

Supervisor Strategy Directives

● Resume: Simply resume message processing● Restart:

○ Transparently replace affected actor(s) with new instance(s)

○ Then resume message processing● Stop: Stop affected actor(s)● Escalate: Delegate the decision to the supervisor's

parent

Page 45: Akka (1)

Load Balancing (Elastic)

Page 46: Akka (1)

Concurrency vs. Parallelism● Two or more tasks are concurrent, if the order in which

they get executed in time is not predetermined● In other words, concurrency introduces non-determinism● Concurrent tasks may or may not get executed in parallel● Concurrent programming is primarily concerned with the

complexity that arises due to non-deterministic control flow

● Parallel programming aims at improving throughput and making control flow deterministic

Page 47: Akka (1)
Page 48: Akka (1)

Routers

Scala : context.actorOf(RoundRobinPool(5).props(SomeActor.props()),"some-actor")

Java : getContext().actorOf(new RoundRobinPool(5).props(SomeActor.props()),"some-actor");

Page 49: Akka (1)

Routing Strategies provided by Akka● RandomRoutingLogic● RoundRobinRoutingLogic● SmallestMailboxRoutingLogic● ConsistentHashingRoutingLogic● BroadcastRoutingLogic● ScatterGatherFirstCompletedRoutingLogic● To write your own routing strategy, extend RoutingLogic:● Attention: The implementation must be thread-safe!

Page 50: Akka (1)

Thanks


Recommended