Top Banner
Reactive Software Systems Behrad Zari [email protected] Fall 2014
62

Reactive Software Systems

Jul 02, 2015

Download

Software

Behrad Zari

My presentation of reactive software adoption in Atieh Dadeh Pardaz
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Reactive Software Systems

Reactive Software Systems

Behrad [email protected]

Fall 2014

Page 2: Reactive Software Systems

Today Software

● Cluster of multi-core machines● Fast networks● Lots of concurrent users● Large data sets● Latency in milliseconds

Page 3: Reactive Software Systems

Our Needs

● Cope to Enterprise Software Complexity

● Scalability / Performance

Page 4: Reactive Software Systems

Reactive Manifesto

Responsive

Resilient

Message Driven

Elastic

http://www.reactivemanifesto.org/

Page 5: Reactive Software Systems

Code Complexity

Page 6: Reactive Software Systems

Complexity

● Programming paradigms

– Imperative → OOP, Statements, …

– Functional → HOF, Immutability, Expressions,...

Page 7: Reactive Software Systems

Programming Paradigms

● Imperative: how to solve– procedural, object-oriented, statements

● Declarative: what to solve– functional, immutability, expressions

Page 8: Reactive Software Systems

Imperative

● Sequence of statements● Contents of memory as state● Statements update variables (mutation)● Assignment, control structures

Page 9: Reactive Software Systems

Example

Page 10: Reactive Software Systems

OOP

● Encapsulation

● Inheritance– Reuse → Composition

– Extension

● Polymorphism– Ad-hoc

– Parametric

– Dynamic

Page 11: Reactive Software Systems

Functional

● Based on Lambda calculus● Functions form programs● Same input → same output● Avoids mutation● Higher order functions

Page 12: Reactive Software Systems

Example

Page 13: Reactive Software Systems

Side Effects

● Global Variables

Page 14: Reactive Software Systems

Side Effects

● Function State, Object State

Page 15: Reactive Software Systems

Side Effects Matter

● Harder to reason about program● Harder to test● Harder to extend, parallelize, …

How to avoid them?

Page 16: Reactive Software Systems

Statements vs Expressions

Page 17: Reactive Software Systems

Functions as 1st-class Members

Functions as values

Page 18: Reactive Software Systems

Lambda Expressions

● block of code with parameters executed later

Page 19: Reactive Software Systems

Clojures

Page 20: Reactive Software Systems

Immutability

Page 21: Reactive Software Systems

Statelessness

● should perform every task as if for the first time– ignorant of the past

Page 22: Reactive Software Systems

Thinking in Functions

● All of your functions must accept at least one argument

● All of your functions must return data or another function

● No loops!

Page 23: Reactive Software Systems

Multi paradigm Implementations

● Imperative with functional features– Java, C#, Python, Ruby

● Functional with OO features– Scala

– Ocaml, f#

What makes difference?– Higher order functions

– Immutable data structures

– Recommended functional style idioms

Page 24: Reactive Software Systems

Scala :D

Page 25: Reactive Software Systems

www.toptal.com

www.toptal.comwww.toptal.comwww.toptal.com

Page 26: Reactive Software Systems

Why Scala

● Nice functional support– Basically functional + immutable data structures + ...

● Type inference system– Less program code, more type checks

● Designed for scalability

● Stick with JVM echo system

Page 27: Reactive Software Systems

Scala Features

● Lambda expressions, closures and currying naturally

● Pattern matching

● Multiple inheritance through Traits

● Comprehensive collections library

Page 28: Reactive Software Systems

Collections

Page 29: Reactive Software Systems

Immutability

● Variables– val VS var

● Collections

● Value objects (case classes)

Page 30: Reactive Software Systems

Case classes

Page 31: Reactive Software Systems

Pattern Matching

Page 32: Reactive Software Systems

Functional Programing Issues

● Input/output difficult to deal with– Monadic operations

● Requires garbage collection

● Sometimes slower (but usually not by much)

● Stateful operations are cumbersome

Page 33: Reactive Software Systems

Learn more

● http://www.scala-lang.org/old/node/8610

Page 34: Reactive Software Systems

Concurrency Models

Page 35: Reactive Software Systems

Thread based Model

● Shared mutable state● Threads concurrently execute code sections

– Synchronization

– Locking mechanism– Not scalable

– Edward A. Lee, The Problem with Threads

Page 36: Reactive Software Systems

Always Blocking

● Latency– CPU → 1ns (=> 1 second)

– RAM → 250,000ns (=> 3 days)

– TCP →150,000,000ns (=> 5 years)

Page 37: Reactive Software Systems

Non blocking Code

Page 38: Reactive Software Systems

Future

Page 39: Reactive Software Systems

Future

Page 40: Reactive Software Systems

Futures in Scala

Page 41: Reactive Software Systems

Event Loop Model

● Implemented in Node.js

Page 42: Reactive Software Systems

Message Passing Model

● 1973, paper by Carl Hewitt● Avoid the problems caused by threading and

locking ● Implemented in Erlang, Scala

Page 43: Reactive Software Systems

Actor Model

● Actors instead of objects

● No shared state between actors

● Asynchronous message-passing

– Immutable messages

● Mailboxes

Page 44: Reactive Software Systems

Actor Benefits

● Higher abstraction level– Easier to reason about

● Easier to avoid– Race conditions– Deadlocks– Starvation– Live locks

● Distributed computing

Page 45: Reactive Software Systems

Example in Erlang

Page 46: Reactive Software Systems

Erlang Actors in Real

● Ericson AXD 301 switch– millions of calls per ,99.9999999 percent uptime

● Facebook chat application– 70 million concurrent users

● RabbitMQ– high-performance AMQP, 400,000 messages per second.

● Apache CouchDB– distributed, fault-tolerant document-oriented database

● Ejabberd XMPP server – jabber.org

Page 47: Reactive Software Systems

JVM implementations

● Java– Kilim, Jetlang, ActorFoundry, Actors Guild

● Scala (FP + actor model)– Akka

● Groovy– GParallelizer

Page 48: Reactive Software Systems

Akka :x

Page 49: Reactive Software Systems

Akka

● Concurrency– mailboxes

– dispatchers

● Scalability– Remote actors, clustering

● Fault tolerance– Actor supervision

Page 50: Reactive Software Systems

Akka Actors

● Define messages● Create new actors● Send messages to others

– Fire & forget

– Send & receive eventually

– Send & receive with future

● Become● Supervise

Page 51: Reactive Software Systems

Akka Actor in Java

Page 52: Reactive Software Systems

Akka actor in Scala

Page 53: Reactive Software Systems

Talk to Actors

Page 54: Reactive Software Systems

Actor Hierarchies

● Name resolution: file system like

Page 55: Reactive Software Systems

Supervision & Resilience

● OneForOneStrategy VS AllForOneStrategy

● Monitor through Death Watch

● Every single actor has a default supervisor strategy. Which is usually sufficient. But it can be overridden

Page 56: Reactive Software Systems

Scalability & Elasticity

● Scale Up VS Scale Out– Essentially are same thing

● Minimize Contention● Maximize Locality of Reference

– Share Nothing Design

Page 57: Reactive Software Systems

Akka Routers

● RoundRobinRoutingLogic

● RandomRoutingLogic

● SmallestMailboxRoutingLogic

● BroadcastRoutingLogic

● ScatterGatherFirstCompleted

● TailChoppingRoutingLogic

● ConsistentHashingRoutingLogic

Page 58: Reactive Software Systems

Akka Clustering

Page 59: Reactive Software Systems

Simple Bulk Aggregator

Page 60: Reactive Software Systems

Better Bulk Aggregator

Page 61: Reactive Software Systems

Akka Adoption

Page 62: Reactive Software Systems

.End()

bacheHa thank { qList =>  qList forEach (_.answer)}