3. Agenda
• What is Akka?
• Concurrency paradigms overview.
• Actors and actor model.
• Live demo #1.
• Akka remoting and clustering.
• Live demo #2.
• CRDTs and Akka Distributed Data.
• Live demo #3.
• Summary.
• Q&A.
4. What is Akka?
http://akka.io
Akka is a toolkit and runtime for building highly
concurrent, distributed, and fault tolerant event-driven
applications on the JVM.
11. Actors
• Originate in a 1973 paper by Carl Hewitt
• Implemented in Erlang
• Encapsulate state and behavior
• Closer to the definition of OO than classes
16. Building a web crawler
1. Fetch a page
2. Parse the page to get links
3. Check if max crawl depth has been reached and if
yes, finish
4. Go to 1 for all parsed links
17. Building a web crawler
Parser
CrawlMasteruser Fetcherpass urls
pass page content
pass parsed urls
18. Building a web crawler
Router
pass urls
CrawlMasterinitial url UrlHandlercreate UrlHandler
Fetcher Fetcher
Router
Parser Parser
19. Building a web crawler
class FetcherActor(val parser: ActorRef) extends Actor with ActorLogging {
import context.dispatcher
val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
override def receive: Receive = {
case Url(link, depth) => pipeline(Get(link)).map(…).pipeTo(parser)
}
}
20. Building a web crawler
class ParserActor extends Actor with ActorLogging with HtmlParser {
override def receive: Receive = {
case UrlContents(Url(link, depth), resp, requester) =>
val links = parseHtml(resp)
.map(l => if (l.matches("^[/#].*")) link + l else l)
.filter(l => Try(new Url(l)).isSuccess
links.foreach(requester ! Url(_, depth + 1))
}
}
21. Building a web crawler
akka {
actor.deployment {
/parsers {
router = round-robin-pool nr-of-instances = 5
}
/fetchers {
router = round-robin-pool nr-of-instances = 5
}
}
}
23. Going remote
• Everything works using asynchronous message
passing which is good for remoting
• Akka-remoting allows working with remote actors
just as if they were in the same JVM
• Still need to handle additional issues like
serialization and handling potential networking
problems
24. Akka cluster – pool routing
akka.tcp://localhost:2551/user
/crawler
Routees
akka.tcp://localhost:2552/user
/crawler
Cluster Pool
Router
localhost:2550
5. Gossip:
localhost:2551
is Up
2. Gossip:
localhost:2552
is Up
6. Deploy
routee
3. Deploy
routee
localhost:2551
localhost:2552
1. Joins cluster
4. Joins cluster
/remote/…/
(routee)
/remote/…/
(routee)
25. 1. Joins cluster
Akka cluster – group routing
akka.tcp://localhost:2551/user
/crawler
Routees
akka.tcp://localhost:2552/user
/crawler
Cluster Group
Router
localhost:2550
5. Gossip:
localhost:2551
is Up
2. Gossip:
localhost:2552
is Up
6. Routes
messages
3. Routes
messages
/user/crawler
localhost:2551
4. Joins cluster
/user/crawler
localhost:2552
37. Summary
• Actor model provides a concurrency paradigm that
is easier to reason about than traditional Java
concurrency
• Designing actor systems is a lot like OO
• You can easily make your actor systems distributed
and have referential transparency… to an extent
• Akka has many useful modules, like Akka
distributed data, which allows to manage
distributed state
• Try to build your own application and see how it
works for you