0
Suraj Atreya
Data Engineer
Glassbeam

Introduction
to
What we do
●

Machine log analytics company

●

Scala shop

●

Akka is used extensively

●

We are a bunch of passionate h...
Akka
●

Pronounced as 'Ahkka', part of Typesafe stack

●

Framework for:
–

Distributed

–

Asynchronous

–

Concurrent

–...
Actor Model
Based on Carl Hewitt 1973 paper
Actor Model
●

3 axioms – When an Actor receives a message
it can:
–

Create new Actors

–

Send messages to other Actors
...
Akka Actors
●
●

High level abstraction
No need to worry about thread locking and
synchronization

●

Fault tolerant

●

L...
Create Actor
case class HelloWorld(who: String)
class HelloWorldActor extends Actor with ActorLogging {
def receive = {
ca...
Create Actor
case class HelloWorld(who: String)
class HelloWorldActor extends Actor with ActorLogging {
def receive = {
ca...
Create Actor
case class HelloWorld(who: String)
class HelloWorldActor extends Actor with ActorLogging {
def receive = {
ca...
Create Actor
case class HelloWorld(who: String)
class HelloWorldActor extends Actor with ActorLogging {
def receive = {
ca...
Actor hierarchy
Guardian System Actor
Actor hierarchy
Guardian System Actor

HelloWorldActor

system.actorOf(Props[HelloWorldActor], name = "hello")
Actor hierarchy
Guardian System Actor

HelloWorldActor

system.actorOf(Props[HelloWorldActor], name = "hello")
File system like name resolution
Guardian System Actor

/HelloWorldActor
HelloWorldActor

system.actorOf(Props[HelloWorldA...
Under the hood

Actor mailbox

Actor 1

...

Actor 2

.......
m2 m1
Time elapsed
Message delivery
●

At most once semantics

●

Ordering of messages sender-receiver pair
Actors != Threads
●

General confusion that actors are threads. They
are not!

●

Actors are perceived as processes

●

Ac...
Routers
●

●

Router is an actor that first receives messages and
routes them to other actors
Can be configured to be any ...
Create a router
val router1 =
system.actorOf(Props[ExampleActor1]
.withRouter(RoundRobinRouter(nrOfInstances = 5)))
Sending messages to router
router1 ! MyMsg
RoundRobin router
Routee 1

Routee 2
Router

router1 ! MyMsg

Routee 3

Routee 4

Routee 5
RoundRobin router
Routee 1

Routee 2
Router

router1 ! MyMsg

Routee 3

Routee 4

Routee 5
SmallestMailBox router
Routee 1

Routee 2
Router

router1 ! MyMsg

Routee 3

Routee 4

Routee 5
Pinned dispatcher demo
Pinned dispatcher
.......................
.......................
.......................
.......................
...........
Best practices
●

●

●

●

Do not block inside an Actor Eg: a big while
loop, network socket etc
Prefer immutable messages...
EOF
Upcoming SlideShare
Loading in...5
×

Meetup slides

207

Published on

Akka meetup slides @ Glassbeam

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
207
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Meetup slides"

  1. 1. Suraj Atreya Data Engineer Glassbeam Introduction to
  2. 2. What we do ● Machine log analytics company ● Scala shop ● Akka is used extensively ● We are a bunch of passionate hackers who solve concurrency, race conditions, memory overcommit issues ● We process 1 TB worth of logs everyday ● Akka, Cassandra, Solr, H2
  3. 3. Akka ● Pronounced as 'Ahkka', part of Typesafe stack ● Framework for: – Distributed – Asynchronous – Concurrent – Non-blocking – Event driven – Message passing
  4. 4. Actor Model Based on Carl Hewitt 1973 paper
  5. 5. Actor Model ● 3 axioms – When an Actor receives a message it can: – Create new Actors – Send messages to other Actors – Decide how it should handle the next message it receives
  6. 6. Akka Actors ● ● High level abstraction No need to worry about thread locking and synchronization ● Fault tolerant ● Location transparent ● Support for Java and Scala APIs
  7. 7. Create Actor case class HelloWorld(who: String) class HelloWorldActor extends Actor with ActorLogging { def receive = { case HelloWorld(person) ⇒ log.info("Hello " + person) } } object TestHelloWorld { def main(args: Array[String]): Unit = { val system = ActorSystem("MySystem") val helloWorldActorRef = system.actorOf(Props[HelloWorldActor], name = "hello") helloWorldActorRef ! HelloWorld("Mr.X") } }
  8. 8. Create Actor case class HelloWorld(who: String) class HelloWorldActor extends Actor with ActorLogging { def receive = { case HelloWorld(person) ⇒ log.info("Hello " + person) } } Create an object TestHelloWorld {Actor System def main(args: Array[String]): Unit = { val system = ActorSystem("MySystem") val helloWorldActorRef = system.actorOf(Props[HelloWorldActor], name = "hello") helloWorldActorRef ! HelloWorld("Mr.X") } }
  9. 9. Create Actor case class HelloWorld(who: String) class HelloWorldActor extends Actor with ActorLogging { def receive = { case HelloWorld(person) ⇒ log.info("Hello " + person) } } Create an object TestHelloWorld {Actor System Create an Actor def main(args: Array[String]): Unit = { val system = ActorSystem("MySystem") val helloWorldActorRef = system.actorOf(Props[HelloWorldActor], name = "hello") helloWorldActorRef ! HelloWorld("Mr.X") } }
  10. 10. Create Actor case class HelloWorld(who: String) class HelloWorldActor extends Actor with ActorLogging { def receive = { case HelloWorld(person) ⇒ log.info("Hello " + person) } } Create an object TestHelloWorld {Actor System Create an Actor def main(args: Array[String]): Unit = { Send message val system = ActorSystem("MySystem") val helloWorldActorRef = system.actorOf(Props[HelloWorldActor], name = "hello") helloWorldActorRef ! HelloWorld("Mr.X") } }
  11. 11. Actor hierarchy Guardian System Actor
  12. 12. Actor hierarchy Guardian System Actor HelloWorldActor system.actorOf(Props[HelloWorldActor], name = "hello")
  13. 13. Actor hierarchy Guardian System Actor HelloWorldActor system.actorOf(Props[HelloWorldActor], name = "hello")
  14. 14. File system like name resolution Guardian System Actor /HelloWorldActor HelloWorldActor system.actorOf(Props[HelloWorldActor], name = "hello") B /HelloWorldActor/A A /HelloWorldActor/B
  15. 15. Under the hood Actor mailbox Actor 1 ... Actor 2 ....... m2 m1 Time elapsed
  16. 16. Message delivery ● At most once semantics ● Ordering of messages sender-receiver pair
  17. 17. Actors != Threads ● General confusion that actors are threads. They are not! ● Actors are perceived as processes ● Actors are assigned to threads by 'Dispatcher' ● Many different kinds of dispatchers: – Default dispatcher – Pinned dispatcher – Balancing dispatcher (not covered in this talk) – CallingThreadDispatcher (not covered in this talk)
  18. 18. Routers ● ● Router is an actor that first receives messages and routes them to other actors Can be configured to be any one of the routers: – Round Robin – Random Router – Smallest Mailbox – Broadcast Router – Scatter Gather First Completed – Consistent Hashing
  19. 19. Create a router val router1 = system.actorOf(Props[ExampleActor1] .withRouter(RoundRobinRouter(nrOfInstances = 5)))
  20. 20. Sending messages to router router1 ! MyMsg
  21. 21. RoundRobin router Routee 1 Routee 2 Router router1 ! MyMsg Routee 3 Routee 4 Routee 5
  22. 22. RoundRobin router Routee 1 Routee 2 Router router1 ! MyMsg Routee 3 Routee 4 Routee 5
  23. 23. SmallestMailBox router Routee 1 Routee 2 Router router1 ! MyMsg Routee 3 Routee 4 Routee 5
  24. 24. Pinned dispatcher demo
  25. 25. Pinned dispatcher ....................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... ...................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... ....................... Log file Chuncked file Chunk 1 Chunk 2 Chunk 3 Pinned dispatcher Chunk 4
  26. 26. Best practices ● ● ● ● Do not block inside an Actor Eg: a big while loop, network socket etc Prefer immutable messages between actors instead of mutable objects Do not create too many “ActorSystem”s. Instead create as many actors as you want. Do not send behaviour as messages. Might accidently share state.
  27. 27. EOF
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×