reactive software systems
DESCRIPTION
My presentation of reactive software adoption in Atieh Dadeh PardazTRANSCRIPT
![Page 2: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/2.jpg)
Today Software
● Cluster of multi-core machines● Fast networks● Lots of concurrent users● Large data sets● Latency in milliseconds
![Page 3: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/3.jpg)
Our Needs
● Cope to Enterprise Software Complexity
● Scalability / Performance
![Page 4: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/4.jpg)
Reactive Manifesto
Responsive
Resilient
Message Driven
Elastic
http://www.reactivemanifesto.org/
![Page 5: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/5.jpg)
Code Complexity
![Page 6: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/6.jpg)
Complexity
● Programming paradigms
– Imperative → OOP, Statements, …
– Functional → HOF, Immutability, Expressions,...
![Page 7: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/7.jpg)
Programming Paradigms
● Imperative: how to solve– procedural, object-oriented, statements
● Declarative: what to solve– functional, immutability, expressions
![Page 8: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/8.jpg)
Imperative
● Sequence of statements● Contents of memory as state● Statements update variables (mutation)● Assignment, control structures
![Page 9: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/9.jpg)
Example
![Page 10: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/10.jpg)
OOP
● Encapsulation
● Inheritance– Reuse → Composition
– Extension
● Polymorphism– Ad-hoc
– Parametric
– Dynamic
![Page 11: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/11.jpg)
Functional
● Based on Lambda calculus● Functions form programs● Same input → same output● Avoids mutation● Higher order functions
![Page 12: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/12.jpg)
Example
![Page 13: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/13.jpg)
Side Effects
● Global Variables
![Page 14: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/14.jpg)
Side Effects
● Function State, Object State
![Page 15: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/15.jpg)
Side Effects Matter
● Harder to reason about program● Harder to test● Harder to extend, parallelize, …
How to avoid them?
![Page 16: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/16.jpg)
Statements vs Expressions
→
![Page 17: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/17.jpg)
Functions as 1st-class Members
Functions as values
![Page 18: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/18.jpg)
Lambda Expressions
● block of code with parameters executed later
![Page 19: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/19.jpg)
Clojures
![Page 20: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/20.jpg)
Immutability
![Page 21: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/21.jpg)
Statelessness
● should perform every task as if for the first time– ignorant of the past
![Page 22: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/24.jpg)
Scala :D
![Page 25: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/25.jpg)
www.toptal.com
www.toptal.comwww.toptal.comwww.toptal.com
![Page 26: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/27.jpg)
Scala Features
● Lambda expressions, closures and currying naturally
● Pattern matching
● Multiple inheritance through Traits
● Comprehensive collections library
![Page 28: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/28.jpg)
Collections
![Page 29: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/29.jpg)
Immutability
● Variables– val VS var
● Collections
● Value objects (case classes)
![Page 30: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/30.jpg)
Case classes
![Page 31: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/31.jpg)
Pattern Matching
![Page 32: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/33.jpg)
Learn more
● http://www.scala-lang.org/old/node/8610
![Page 34: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/34.jpg)
Concurrency Models
![Page 35: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/36.jpg)
Always Blocking
● Latency– CPU → 1ns (=> 1 second)
– RAM → 250,000ns (=> 3 days)
– TCP →150,000,000ns (=> 5 years)
![Page 37: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/37.jpg)
Non blocking Code
![Page 38: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/38.jpg)
Future
![Page 39: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/39.jpg)
Future
![Page 40: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/40.jpg)
Futures in Scala
![Page 41: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/41.jpg)
Event Loop Model
● Implemented in Node.js
![Page 42: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/42.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/43.jpg)
Actor Model
● Actors instead of objects
● No shared state between actors
● Asynchronous message-passing
– Immutable messages
● Mailboxes
![Page 44: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/44.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/45.jpg)
Example in Erlang
![Page 46: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/46.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/47.jpg)
JVM implementations
● Java– Kilim, Jetlang, ActorFoundry, Actors Guild
● Scala (FP + actor model)– Akka
● Groovy– GParallelizer
![Page 48: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/48.jpg)
Akka :x
![Page 49: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/49.jpg)
Akka
● Concurrency– mailboxes
– dispatchers
● Scalability– Remote actors, clustering
● Fault tolerance– Actor supervision
![Page 50: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/50.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/51.jpg)
Akka Actor in Java
![Page 52: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/52.jpg)
Akka actor in Scala
![Page 53: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/53.jpg)
Talk to Actors
![Page 54: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/54.jpg)
Actor Hierarchies
● Name resolution: file system like
![Page 55: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/55.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/56.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/57.jpg)
Akka Routers
● RoundRobinRoutingLogic
● RandomRoutingLogic
● SmallestMailboxRoutingLogic
● BroadcastRoutingLogic
● ScatterGatherFirstCompleted
● TailChoppingRoutingLogic
● ConsistentHashingRoutingLogic
![Page 58: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/58.jpg)
Akka Clustering
![Page 59: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/59.jpg)
Simple Bulk Aggregator
![Page 60: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/60.jpg)
Better Bulk Aggregator
![Page 61: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/61.jpg)
Akka Adoption
![Page 62: Reactive Software Systems](https://reader033.vdocuments.net/reader033/viewer/2022060121/559444711a28abfa2f8b4744/html5/thumbnails/62.jpg)
.End()
bacheHa thank { qList => qList forEach (_.answer)}