SlideShare a Scribd company logo
1 of 29
Download to read offline
MILAN november 28th, 2014 - Roberto Bentivoglio 
1 
Roberto Bentivoglio 
Recipes to develop a reactive 
and cloud-ready application 
using Scala and Akka 
roberto.bentivoglio@databiz.it 
Follow me on Twitter! @robbenti
MILAN november 28th, 2014 - Roberto Bentivoglio 
2 
Outline 
•The reactive manifesto 
•Introduction to Akka 
•Akka advanced features: clustering and sharding 
•Example: Akka chat!
MILAN november 28th, 2014 - Roberto Bentivoglio 
3 
Reactive Manifesto 
“Today's demands are simply not met by 
yesterday’s software architectures”
MILAN november 28th, 2014 - Roberto Bentivoglio 
“We believe that a coherent approach to 
systems architecture is needed, and we 
believe that all necessary aspects are already 
recognised individually: we want systems that 
are Responsive, Resilient, Elastic and Message 
Driven. We call these Reactive Systems.” 
4 
Reactive Manifesto
MILAN november 28th, 2014 - Roberto Bentivoglio 
5 
Reactive Manifesto
MILAN november 28th, 2014 - Roberto Bentivoglio 
6 
Reactive Programming 
“Akka is a toolkit and runtime for building 
highly concurrent, distributed, and resilient 
message-driven applications on the JVM.”
MILAN november 28th, 2014 - Roberto Bentivoglio 
7 
Akka 
•Event-driven 
•Elastic / Scalable 
•Scale out 
•abstraction for transparent distribution 
•Resilient 
•Supervisor hierarchies with “let-it-crash” semantics 
•Fault-tolerant and self-healing systems 
•Responsive 
•Asynchronous, non-blocking and highly performant event-driven 
programming mode
MILAN november 28th, 2014 - Roberto Bentivoglio 
8 
Actor Model 
“The actor model in computer science is a mathematical 
model of concurrent computation that treats "actors" as 
the universal primitives of concurrent computation: in 
response to a message that it receives, an actor can 
make local decisions, create more actors, send more 
messages, and determine how to respond to the next 
message received” (Wikipedia)
MILAN november 28th, 2014 - Roberto Bentivoglio 
9 
Actor System 
/ 
user system 
root 
parent1 parent2 parentN 
child1 child2 child2 
An Actor System is a container of actors 
Actors are arranged in a hierarchy (a tree) 
root 
user 
actors 
top-level 
user 
actors 
root 
system 
actors 
children
MILAN november 28th, 2014 - Roberto Bentivoglio 
10 
Akka - ActorSystem 
// create a new actor system 
val system = ActorSystem("reactive-chat-actor-system") 
// shutdown the actor system 
system.shutdown() 
// creating a new top-level actor 
val user: ActorRef = system.actorOf(Props(new User), "user")
MILAN november 28th, 2014 - Roberto Bentivoglio 
Akka - Actors 
An actor is a container for: 
•State 
•Behavior 
•Mailbox 
•Children 
•Supervisor Strategy. 
All of this is encapsulated behind an Actor 
11 
Reference (ActorRef)
MILAN november 28th, 2014 - Roberto Bentivoglio 
12 
Akka - Actor 
class User extends Actor with ActorLogging { 
override def receive: Receive = { 
case message: String => 
// do something here... 
log.info("Received the message {}", message) 
} 
} 
The receive method defines the behaviour 
type Receive = PartialFunction[Any, Unit]
MILAN november 28th, 2014 - Roberto Bentivoglio 
13 
Akka - Actor 
•Process events and generate responses (or more 
requests) in an event-driven manner 
•Do not block! 
•Do not pass mutable objects between actors! 
•Do not share internal mutable state!
MILAN november 28th, 2014 - Roberto Bentivoglio 
14 
Akka - fire-and-forget semantics 
user ! "Hi, wazzup?" 
// short form of 
user.tell("Hi, wazzup?", context.self) 
final def tell(msg: Any, sender: ActorRef): Unit = 
this.!(msg)(sender)
MILAN november 28th, 2014 - Roberto Bentivoglio 
15 
Akka - ask pattern 
import akka.actor._ 
import akka.util.Timeout 
import scala.concurrent.duration._ 
implicit val timeout: Timeout = 5 seconds 
user ? "Hi, wazzup?" 
def ask(message: Any) 
(implicit timeout: Timeout): Future[Any]
MILAN november 28th, 2014 - Roberto Bentivoglio 
•An anchor (to identify the actor system) 
•A concatenation of path elements (from root guardian to the 
designed actor) 
•The path elements are the names of the traversed actors and 
are separated by slashes 
16 
Akka - Actor Path 
An actor path consists of: 
// local 
“akka://reactive-chat-actor-system/user/service-a/worker1" 
// remote 
"akka.tcp://reactive-chat-actor-system@host.example.com: 
5678/user/service-b"
MILAN november 28th, 2014 - Roberto Bentivoglio 
17 
Akka - Location transparency 
Everything in Akka is designed to work in a 
distributed setting: all interactions of actors use 
purely message passing and everything is 
asynchronous
MILAN november 28th, 2014 - Roberto Bentivoglio 
18 
Akka - Remoting 
Akka Remoting is designed for communication in a peer-to-peer 
fashion and it has limitations for client-server setups 
context.actorSelection("akka.tcp://rchat-actor- 
system@10.10.10.2:2552/user/actor1")
MILAN november 28th, 2014 - Roberto Bentivoglio 
19 
Akka - Cluster 
Akka Cluster provides a: 
•Fault-tolerant, decentralised, peer-to-peer based cluster 
membership service 
•With no single point of failure or single point of bottleneck 
•It uses gossip protocols and an automatic failure detector
MILAN november 28th, 2014 - Roberto Bentivoglio 
Akka - Cluster 
•Node: A logical member of a cluster. There could be 
multiple nodes on a physical machine. Defined by a 
hostname:port:uid tuple. 
•Cluster: A set of nodes joined together through the 
membership service. 
•Leader: A single node in the cluster that acts as the leader. 
It is simply the first node in sorted order that is able to take 
the leadership role 
•Seed nodes: configured contact points for new nodes joining 
the cluster 
20
MILAN november 28th, 2014 - Roberto Bentivoglio 
21 
Akka - Cluster 
Cluster 
Node1 Node2 
Node3 Node4 
NodeN 
Node5 
Leader 
Seeds
MILAN november 28th, 2014 - Roberto Bentivoglio 
22 
Akka - Cluster 
akka { 
actor.provider = "akka.cluster.ClusterActorRefProvider" 
extensions = 
["akka.contrib.pattern.DistributedPubSubExtension"] 
remote.netty.tcp { 
hostname = "localhost" 
port = 2552 
} 
cluster { 
seed-nodes=["akka.tcp://reactive-chat-server@localhost: 
2552","akka.tcp://application@localhost:2553"] 
roles = [backend] 
} 
} 
// create a new cluster 
val cluster = Cluster(system)
MILAN november 28th, 2014 - Roberto Bentivoglio 
Akka - Cluster, contrib package (1) 
•Cluster singleton: one actor of a certain type running 
somewhere in the cluster 
•Cluster sharding: distribute actors across several nodes in 
the cluster and want to be able to interact with them using 
their logical identifier, but without having to care about their 
physical location in the cluster, which might also change 
over time 
23
MILAN november 28th, 2014 - Roberto Bentivoglio 
Akka - Cluster, contrib package (2) 
•Distributed Publish Subscribe (topic): This pattern provides a 
mediator actor, 
akka.contrib.pattern.DistributedPubSubMediato 
r, that manages a registry of actor references and 
replicates the entries to peer actors among all cluster nodes 
or a group of nodes tagged with a specific role. 
•Cluster Client: An actor system that is not part of the cluster 
can communicate with actors somewhere in the cluster via 
this ClusterClient 
24
MILAN november 28th, 2014 - Roberto Bentivoglio 
25 
Akka - Example! 
An Akka reactive (clustered) chat!
MILAN november 28th, 2014 - Roberto Bentivoglio 
26 
Akka - Example! 
Browser 
user1 
Cluster 
Node1 
User1 User2 
User3 
User5 User4 Node2 
Topic / 
mediator 
Browser 
user5
MILAN november 28th, 2014 - Roberto Bentivoglio 
27 
Biblio & Links 
• Reactive Manifesto - http://www.reactivemanifesto.org/ 
• Scala & Akka - http://www.typesafe.com/ 
• Scala - Programming in Scala 2nd edition - http://www.artima.com/shop/ 
programming_in_scala_2ed 
• Akka documentation - http://akka.io/docs/ 
• Akka team blog - http://letitcrash.com/ 
• Akka concurrency - http://www.artima.com/shop/akka_concurrency 
• Akka in Action - http://www.manning.com/roestenburg/ 
• Activator example on mediator pattern - http://typesafe.com/activator/ 
template/reactive-maps
MILAN november 28th, 2014 - Roberto Bentivoglio 
28 
We’re hiring! 
info@databiz.it
MILAN november 28th, 2014 - Roberto Bentivoglio 
29 
Akka - Example! 
Thanks! 
Q&A 
roberto.bentivoglio@databiz.it 
Follow me on Twitter! @robbenti

More Related Content

What's hot

Inside neutron 2
Inside neutron 2Inside neutron 2
Inside neutron 2
Robin Gong
 

What's hot (20)

Icinga Camp Antwerp - Icinga2 Cluster
Icinga Camp Antwerp - Icinga2 ClusterIcinga Camp Antwerp - Icinga2 Cluster
Icinga Camp Antwerp - Icinga2 Cluster
 
Akka Fundamentals
Akka FundamentalsAkka Fundamentals
Akka Fundamentals
 
FunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang EcosystemFunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang Ecosystem
 
Icinga @OSMC 2013
Icinga @OSMC 2013Icinga @OSMC 2013
Icinga @OSMC 2013
 
Open Source Monitoring with Icinga at Fossasia 2015
Open Source Monitoring with Icinga at Fossasia 2015Open Source Monitoring with Icinga at Fossasia 2015
Open Source Monitoring with Icinga at Fossasia 2015
 
Combining the strength of erlang and Ruby
Combining the strength of erlang and RubyCombining the strength of erlang and Ruby
Combining the strength of erlang and Ruby
 
Inside neutron 2
Inside neutron 2Inside neutron 2
Inside neutron 2
 
Icinga @CLT 2013
Icinga @CLT 2013Icinga @CLT 2013
Icinga @CLT 2013
 
Icinga Camp Barcelona - Current State of Icinga
Icinga Camp Barcelona - Current State of IcingaIcinga Camp Barcelona - Current State of Icinga
Icinga Camp Barcelona - Current State of Icinga
 
Security workflow with ansible
Security  workflow with ansibleSecurity  workflow with ansible
Security workflow with ansible
 
Icinga 2011 at Nagios Workshop
Icinga 2011 at Nagios WorkshopIcinga 2011 at Nagios Workshop
Icinga 2011 at Nagios Workshop
 
Welcome Icinga Camp San Francisco 2014
Welcome Icinga Camp San Francisco 2014Welcome Icinga Camp San Francisco 2014
Welcome Icinga Camp San Francisco 2014
 
The Software Developers Guide to Prototyping Wearable Devices
The Software Developers Guide to Prototyping Wearable DevicesThe Software Developers Guide to Prototyping Wearable Devices
The Software Developers Guide to Prototyping Wearable Devices
 
Icinga 2011 at Chemnitzer Linuxtage
Icinga 2011 at Chemnitzer LinuxtageIcinga 2011 at Chemnitzer Linuxtage
Icinga 2011 at Chemnitzer Linuxtage
 
Icinga 2012 Development at 6th TF-NOC Meeting
Icinga 2012 Development at 6th TF-NOC MeetingIcinga 2012 Development at 6th TF-NOC Meeting
Icinga 2012 Development at 6th TF-NOC Meeting
 
OSMC 2021 | Robotmk: You don’t run IT – you deliver services!
OSMC 2021 | Robotmk: You don’t run IT – you deliver services!OSMC 2021 | Robotmk: You don’t run IT – you deliver services!
OSMC 2021 | Robotmk: You don’t run IT – you deliver services!
 
Docker {at,with} SignalFx
Docker {at,with} SignalFxDocker {at,with} SignalFx
Docker {at,with} SignalFx
 
Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"
 
Splunk Developer Platform
Splunk Developer PlatformSplunk Developer Platform
Splunk Developer Platform
 
Microservices are ‘easy’ dependencies are hard
Microservices are ‘easy’ dependencies are hardMicroservices are ‘easy’ dependencies are hard
Microservices are ‘easy’ dependencies are hard
 

Similar to Recipes to develop a reactive and cloud-ready application using Scala and Akka

Openstack Neutron and SDN
Openstack Neutron and SDNOpenstack Neutron and SDN
Openstack Neutron and SDN
inakipascual
 

Similar to Recipes to develop a reactive and cloud-ready application using Scala and Akka (20)

neutron_icehouse_update
neutron_icehouse_updateneutron_icehouse_update
neutron_icehouse_update
 
We all need friends and Akka just found Kubernetes
We all need friends and Akka just found KubernetesWe all need friends and Akka just found Kubernetes
We all need friends and Akka just found Kubernetes
 
Akka and Kubernetes: Reactive From Code To Cloud
Akka and Kubernetes: Reactive From Code To CloudAkka and Kubernetes: Reactive From Code To Cloud
Akka and Kubernetes: Reactive From Code To Cloud
 
End to End Akka Streams / Reactive Streams - from Business to Socket
End to End Akka Streams / Reactive Streams - from Business to SocketEnd to End Akka Streams / Reactive Streams - from Business to Socket
End to End Akka Streams / Reactive Streams - from Business to Socket
 
Reactive Streams, j.u.concurrent, & Beyond!
Reactive Streams, j.u.concurrent, & Beyond!Reactive Streams, j.u.concurrent, & Beyond!
Reactive Streams, j.u.concurrent, & Beyond!
 
muCon 2014 "Building Java Microservices for the Cloud"
muCon 2014 "Building Java Microservices for the Cloud"muCon 2014 "Building Java Microservices for the Cloud"
muCon 2014 "Building Java Microservices for the Cloud"
 
Kubernetes fundamentals
Kubernetes fundamentalsKubernetes fundamentals
Kubernetes fundamentals
 
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
 
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
CAPS: What's best for deploying and managing OpenStack? Chef vs. Ansible vs. ...
 
Reactive Streams, j.u.concurrent & Beyond!
Reactive Streams, j.u.concurrent & Beyond!Reactive Streams, j.u.concurrent & Beyond!
Reactive Streams, j.u.concurrent & Beyond!
 
Openstack – An introduction
Openstack – An introductionOpenstack – An introduction
Openstack – An introduction
 
Openstack Neutron and SDN
Openstack Neutron and SDNOpenstack Neutron and SDN
Openstack Neutron and SDN
 
Akka Streams in Action @ ScalaDays Berlin 2016
Akka Streams in Action @ ScalaDays Berlin 2016Akka Streams in Action @ ScalaDays Berlin 2016
Akka Streams in Action @ ScalaDays Berlin 2016
 
OpenStack: Networking Roadmap, Collaboration and Contribution
OpenStack: Networking Roadmap, Collaboration and ContributionOpenStack: Networking Roadmap, Collaboration and Contribution
OpenStack: Networking Roadmap, Collaboration and Contribution
 
Open stack networking_101_update_2014
Open stack networking_101_update_2014Open stack networking_101_update_2014
Open stack networking_101_update_2014
 
Virtual Networking (1) (1).pptx
Virtual Networking (1) (1).pptxVirtual Networking (1) (1).pptx
Virtual Networking (1) (1).pptx
 
Patterns of-streaming-applications-qcon-2018-monal-daxini
Patterns of-streaming-applications-qcon-2018-monal-daxiniPatterns of-streaming-applications-qcon-2018-monal-daxini
Patterns of-streaming-applications-qcon-2018-monal-daxini
 
Kubernetes Operability Tooling (Minnebar 2019)
Kubernetes Operability Tooling (Minnebar 2019)Kubernetes Operability Tooling (Minnebar 2019)
Kubernetes Operability Tooling (Minnebar 2019)
 
Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC
Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYCBuilding a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC
Building a Reactive System with Akka - Workshop @ O'Reilly SAConf NYC
 
How Reactive Streams & Akka Streams change the JVM Ecosystem
How Reactive Streams & Akka Streams change the JVM EcosystemHow Reactive Streams & Akka Streams change the JVM Ecosystem
How Reactive Streams & Akka Streams change the JVM Ecosystem
 

More from Codemotion

More from Codemotion (20)

Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
 
Pompili - From hero to_zero: The FatalNoise neverending story
Pompili - From hero to_zero: The FatalNoise neverending storyPompili - From hero to_zero: The FatalNoise neverending story
Pompili - From hero to_zero: The FatalNoise neverending story
 
Pastore - Commodore 65 - La storia
Pastore - Commodore 65 - La storiaPastore - Commodore 65 - La storia
Pastore - Commodore 65 - La storia
 
Pennisi - Essere Richard Altwasser
Pennisi - Essere Richard AltwasserPennisi - Essere Richard Altwasser
Pennisi - Essere Richard Altwasser
 
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
 
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
 
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
 
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 - Francesco Baldassarri  - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
 
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
 
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
 
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
 
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
 
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
 
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
 
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
 
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
 
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
 
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
 
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
 
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
 

Recipes to develop a reactive and cloud-ready application using Scala and Akka

  • 1. MILAN november 28th, 2014 - Roberto Bentivoglio 1 Roberto Bentivoglio Recipes to develop a reactive and cloud-ready application using Scala and Akka roberto.bentivoglio@databiz.it Follow me on Twitter! @robbenti
  • 2. MILAN november 28th, 2014 - Roberto Bentivoglio 2 Outline •The reactive manifesto •Introduction to Akka •Akka advanced features: clustering and sharding •Example: Akka chat!
  • 3. MILAN november 28th, 2014 - Roberto Bentivoglio 3 Reactive Manifesto “Today's demands are simply not met by yesterday’s software architectures”
  • 4. MILAN november 28th, 2014 - Roberto Bentivoglio “We believe that a coherent approach to systems architecture is needed, and we believe that all necessary aspects are already recognised individually: we want systems that are Responsive, Resilient, Elastic and Message Driven. We call these Reactive Systems.” 4 Reactive Manifesto
  • 5. MILAN november 28th, 2014 - Roberto Bentivoglio 5 Reactive Manifesto
  • 6. MILAN november 28th, 2014 - Roberto Bentivoglio 6 Reactive Programming “Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM.”
  • 7. MILAN november 28th, 2014 - Roberto Bentivoglio 7 Akka •Event-driven •Elastic / Scalable •Scale out •abstraction for transparent distribution •Resilient •Supervisor hierarchies with “let-it-crash” semantics •Fault-tolerant and self-healing systems •Responsive •Asynchronous, non-blocking and highly performant event-driven programming mode
  • 8. MILAN november 28th, 2014 - Roberto Bentivoglio 8 Actor Model “The actor model in computer science is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received” (Wikipedia)
  • 9. MILAN november 28th, 2014 - Roberto Bentivoglio 9 Actor System / user system root parent1 parent2 parentN child1 child2 child2 An Actor System is a container of actors Actors are arranged in a hierarchy (a tree) root user actors top-level user actors root system actors children
  • 10. MILAN november 28th, 2014 - Roberto Bentivoglio 10 Akka - ActorSystem // create a new actor system val system = ActorSystem("reactive-chat-actor-system") // shutdown the actor system system.shutdown() // creating a new top-level actor val user: ActorRef = system.actorOf(Props(new User), "user")
  • 11. MILAN november 28th, 2014 - Roberto Bentivoglio Akka - Actors An actor is a container for: •State •Behavior •Mailbox •Children •Supervisor Strategy. All of this is encapsulated behind an Actor 11 Reference (ActorRef)
  • 12. MILAN november 28th, 2014 - Roberto Bentivoglio 12 Akka - Actor class User extends Actor with ActorLogging { override def receive: Receive = { case message: String => // do something here... log.info("Received the message {}", message) } } The receive method defines the behaviour type Receive = PartialFunction[Any, Unit]
  • 13. MILAN november 28th, 2014 - Roberto Bentivoglio 13 Akka - Actor •Process events and generate responses (or more requests) in an event-driven manner •Do not block! •Do not pass mutable objects between actors! •Do not share internal mutable state!
  • 14. MILAN november 28th, 2014 - Roberto Bentivoglio 14 Akka - fire-and-forget semantics user ! "Hi, wazzup?" // short form of user.tell("Hi, wazzup?", context.self) final def tell(msg: Any, sender: ActorRef): Unit = this.!(msg)(sender)
  • 15. MILAN november 28th, 2014 - Roberto Bentivoglio 15 Akka - ask pattern import akka.actor._ import akka.util.Timeout import scala.concurrent.duration._ implicit val timeout: Timeout = 5 seconds user ? "Hi, wazzup?" def ask(message: Any) (implicit timeout: Timeout): Future[Any]
  • 16. MILAN november 28th, 2014 - Roberto Bentivoglio •An anchor (to identify the actor system) •A concatenation of path elements (from root guardian to the designed actor) •The path elements are the names of the traversed actors and are separated by slashes 16 Akka - Actor Path An actor path consists of: // local “akka://reactive-chat-actor-system/user/service-a/worker1" // remote "akka.tcp://reactive-chat-actor-system@host.example.com: 5678/user/service-b"
  • 17. MILAN november 28th, 2014 - Roberto Bentivoglio 17 Akka - Location transparency Everything in Akka is designed to work in a distributed setting: all interactions of actors use purely message passing and everything is asynchronous
  • 18. MILAN november 28th, 2014 - Roberto Bentivoglio 18 Akka - Remoting Akka Remoting is designed for communication in a peer-to-peer fashion and it has limitations for client-server setups context.actorSelection("akka.tcp://rchat-actor- system@10.10.10.2:2552/user/actor1")
  • 19. MILAN november 28th, 2014 - Roberto Bentivoglio 19 Akka - Cluster Akka Cluster provides a: •Fault-tolerant, decentralised, peer-to-peer based cluster membership service •With no single point of failure or single point of bottleneck •It uses gossip protocols and an automatic failure detector
  • 20. MILAN november 28th, 2014 - Roberto Bentivoglio Akka - Cluster •Node: A logical member of a cluster. There could be multiple nodes on a physical machine. Defined by a hostname:port:uid tuple. •Cluster: A set of nodes joined together through the membership service. •Leader: A single node in the cluster that acts as the leader. It is simply the first node in sorted order that is able to take the leadership role •Seed nodes: configured contact points for new nodes joining the cluster 20
  • 21. MILAN november 28th, 2014 - Roberto Bentivoglio 21 Akka - Cluster Cluster Node1 Node2 Node3 Node4 NodeN Node5 Leader Seeds
  • 22. MILAN november 28th, 2014 - Roberto Bentivoglio 22 Akka - Cluster akka { actor.provider = "akka.cluster.ClusterActorRefProvider" extensions = ["akka.contrib.pattern.DistributedPubSubExtension"] remote.netty.tcp { hostname = "localhost" port = 2552 } cluster { seed-nodes=["akka.tcp://reactive-chat-server@localhost: 2552","akka.tcp://application@localhost:2553"] roles = [backend] } } // create a new cluster val cluster = Cluster(system)
  • 23. MILAN november 28th, 2014 - Roberto Bentivoglio Akka - Cluster, contrib package (1) •Cluster singleton: one actor of a certain type running somewhere in the cluster •Cluster sharding: distribute actors across several nodes in the cluster and want to be able to interact with them using their logical identifier, but without having to care about their physical location in the cluster, which might also change over time 23
  • 24. MILAN november 28th, 2014 - Roberto Bentivoglio Akka - Cluster, contrib package (2) •Distributed Publish Subscribe (topic): This pattern provides a mediator actor, akka.contrib.pattern.DistributedPubSubMediato r, that manages a registry of actor references and replicates the entries to peer actors among all cluster nodes or a group of nodes tagged with a specific role. •Cluster Client: An actor system that is not part of the cluster can communicate with actors somewhere in the cluster via this ClusterClient 24
  • 25. MILAN november 28th, 2014 - Roberto Bentivoglio 25 Akka - Example! An Akka reactive (clustered) chat!
  • 26. MILAN november 28th, 2014 - Roberto Bentivoglio 26 Akka - Example! Browser user1 Cluster Node1 User1 User2 User3 User5 User4 Node2 Topic / mediator Browser user5
  • 27. MILAN november 28th, 2014 - Roberto Bentivoglio 27 Biblio & Links • Reactive Manifesto - http://www.reactivemanifesto.org/ • Scala & Akka - http://www.typesafe.com/ • Scala - Programming in Scala 2nd edition - http://www.artima.com/shop/ programming_in_scala_2ed • Akka documentation - http://akka.io/docs/ • Akka team blog - http://letitcrash.com/ • Akka concurrency - http://www.artima.com/shop/akka_concurrency • Akka in Action - http://www.manning.com/roestenburg/ • Activator example on mediator pattern - http://typesafe.com/activator/ template/reactive-maps
  • 28. MILAN november 28th, 2014 - Roberto Bentivoglio 28 We’re hiring! info@databiz.it
  • 29. MILAN november 28th, 2014 - Roberto Bentivoglio 29 Akka - Example! Thanks! Q&A roberto.bentivoglio@databiz.it Follow me on Twitter! @robbenti