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

29
MILAN november 28 th , 2014 - Roberto Bentivoglio 1 Recipes to develop a reactive and cloud-ready application using Scala and Akka Roberto Bentivoglio [email protected] Follow me on Twitter! @robbenti

description

by Roberto Bentivoglio - Non-functional requirements have become increasingly challenging during the last years. Nowadays we expect at least that every application provide fast response time and almost 100% uptime. Reactive Applications is a set of principles and architectural patterns allowing to build event-driven, scalable, resilient and responsive systems. One of the most interesting technologies to create Reactive Applications is the Typesafe stack and, in particular, the Akka toolkit. During this talk I will introduce it, showing how develop a reactive and cloud-ready Scala web application.

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

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

MILAN november 28th, 2014 - Roberto Bentivoglio

1

Recipes to develop a reactive and cloud-ready application

using Scala and Akka

Roberto Bentivoglio

[email protected] Follow me on Twitter! @robbenti

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

MILAN november 28th, 2014 - Roberto Bentivoglio

2

•The reactive manifesto

•Introduction to Akka

•Akka advanced features: clustering and sharding

•Example: Akka chat!

Outline

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

MILAN november 28th, 2014 - Roberto Bentivoglio

3

“Today's demands are simply not met by yesterday’s software architectures”

Reactive Manifesto

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

MILAN november 28th, 2014 - Roberto Bentivoglio

4

“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.”

Reactive Manifesto

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

MILAN november 28th, 2014 - Roberto Bentivoglio

5

Reactive Manifesto

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

MILAN november 28th, 2014 - Roberto Bentivoglio

6

“Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM.”

Reactive Programming

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

MILAN november 28th, 2014 - Roberto Bentivoglio

7

•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

Akka

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

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)

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

MILAN november 28th, 2014 - Roberto Bentivoglio

9

Actor System

An Actor System is a container of actors

Actors are arranged in a hierarchy (a tree)

/

user system

parent2 parentNparent1

child2 child2child1

rootroot user actors

top-level user actors

root system actors

children

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

MILAN november 28th, 2014 - Roberto Bentivoglio

10

Akka - ActorSystem// create a new actor systemval system = ActorSystem("reactive-chat-actor-system")

// shutdown the actor systemsystem.shutdown()

// creating a new top-level actorval user: ActorRef = system.actorOf(Props(new User), "user")

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

MILAN november 28th, 2014 - Roberto Bentivoglio

11

An actor is a container for:

•State

•Behavior

•Mailbox

•Children

•Supervisor Strategy.

All of this is encapsulated behind an Actor Reference (ActorRef)

Akka - Actors

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

MILAN november 28th, 2014 - Roberto Bentivoglio

12

Akka - Actorclass 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]

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

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!

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

MILAN november 28th, 2014 - Roberto Bentivoglio

14

Akka - fire-and-forget semantics

user ! "Hi, wazzup?"// short form ofuser.tell("Hi, wazzup?", context.self)

final def tell(msg: Any, sender: ActorRef): Unit = this.!(msg)(sender)

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

MILAN november 28th, 2014 - Roberto Bentivoglio

15

Akka - ask pattern

import akka.actor._import akka.util.Timeoutimport scala.concurrent.duration._

implicit val timeout: Timeout = 5 secondsuser ? "Hi, wazzup?"

def ask(message: Any)(implicit timeout: Timeout): Future[Any]

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

MILAN november 28th, 2014 - Roberto Bentivoglio

16

Akka - Actor Path

// local“akka://reactive-chat-actor-system/user/service-a/worker1"

// remote"akka.tcp://[email protected]:5678/user/service-b"

An actor path consists of:

•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

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

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

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

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://[email protected]:2552/user/actor1")

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

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

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

MILAN november 28th, 2014 - Roberto Bentivoglio

20

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

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

MILAN november 28th, 2014 - Roberto Bentivoglio

21

Akka - Cluster

Cluster

Node1 Node2

Node3 Node4

NodeN

Node5

Leader

Seeds

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

MILAN november 28th, 2014 - Roberto Bentivoglio

22

Akka - Cluster

// create a new clusterval cluster = Cluster(system)

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] }}

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

MILAN november 28th, 2014 - Roberto Bentivoglio

23

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

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

MILAN november 28th, 2014 - Roberto Bentivoglio

24

Akka - Cluster, contrib package (2)•Distributed Publish Subscribe (topic): This pattern provides a

mediator actor, akka.contrib.pattern.DistributedPubSubMediator, 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

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

MILAN november 28th, 2014 - Roberto Bentivoglio

25

Akka - Example!

An Akka reactive (clustered) chat!

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

MILAN november 28th, 2014 - Roberto Bentivoglio

26

Akka - Example!

Cluster

Node1

Node2User5 User4

User1 User2

User3

Topic / mediator

Browser user1

Browser user5

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

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

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

MILAN november 28th, 2014 - Roberto Bentivoglio

28

We’re hiring! [email protected]

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

MILAN november 28th, 2014 - Roberto Bentivoglio

29

Akka - Example!

Thanks!

Q&A

[email protected] Follow me on Twitter! @robbenti