Reactive applications Linux Day 2013

1,990 views

Published on

Reactive Applications, Reactive Manifesto, Scala, Akka

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,990
On SlideShare
0
From Embeds
0
Number of Embeds
591
Actions
Shares
0
Downloads
16
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Reactive applications Linux Day 2013

  1. 1. Reactive Applications di Massimiliano Dessì
  2. 2. Reactive applications Speaker Massimiliano Dessì has more than 13 years of experience in programming. He’s a proud father of three. @desmax74 Manager of GDG Sardegna, Founder of S SpringFramework IT, co-founder of Jug Sardegna. Author of Spring 2.5 AOP. He works and lives in Cagliari, Italy. 2
  3. 3. Reactive applications 3
  4. 4. Reactive applications Software requirements nowadays Highly Responsive, Real Time Scalable Resilient Petabytes 4
  5. 5. Reactive applications New problems We need better tools 5 http://www.pacificrimmovie.net/
  6. 6. Reactive applications Reactive “readily responsive to a stimulus” Component active and ready to respond to event Event Driven 6
  7. 7. Reactive applications Reactive React to events → Event Driven React to failure → Resilient React through a UI → Interactive React to load → Scalable 7
  8. 8. Reactive applications React to event - Event driven Asyncronous and loosely coupled + Non blocking = lower latency and higher throughput 8
  9. 9. Reactive applications React to event - Event driven Productors push asyncronously data towards consumers (message passing) better resource usage instead of having consumers continually ask for data 9
  10. 10. Reactive applications React to event - Event driven Non blocking operation mean have all the time the application responsive even under failure 10
  11. 11. Reactive applications Event driven Actors , No shared mutable state Promise , Composable Message passing asyncronous, non Blocking Lock free concurrency 11
  12. 12. Reactive applications Scalable “Capable of being easily expanded or upgraded on demand” Event driven and message passing are the foundations 12
  13. 13. Reactive applications Scalable “loose coupling and location independence between components and subsystems make it possible to scale out the system onto multiple node” Location transparency != transparent distributed computing 13
  14. 14. Reactive applications Resilient “the capacity to recover quickly from difficulties” In a reactive application, resilience is part of the design from the beginning 14
  15. 15. Reactive applications Resilient Bulkheads and Circuit Breaker patterns Isolate failure Manage Failure locally Avoid cascading failure 15
  16. 16. Reactive applications Resilient Bulkheads Failure is modeled explicitly in order to be compartmentalized, observed, managed and configured in a declarative way, and where the system can heal itself and recover automatically 16
  17. 17. Reactive applications Resilient Actor lightweight isolated process (400 bytes of heap space) Each process has its own supervisor In case of failure the supervisor receive as async msg the error The supervisor can choose the recovery strategy kill, restart, suspend/resume 17
  18. 18. Reactive applications Resilient Actor decoupling business logic from handling error Mailbox Guaranteed Delivery my-dispatcher { mailbox-type = akka.actor.mailbox.filebased.FileBasedMailboxType } 18
  19. 19. Reactive applications Resilient override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: java.sql.SQLException => Resume case _: NullPointerException => Restart case _: Exception => Escalate } 19
  20. 20. Reactive applications Resilient A circuit breaker is used to provide stability and prevent cascading failures in distributed systems. val breaker = new CircuitBreaker( context.system.scheduler, maxFailures = 5, callTimeout = 10.seconds, resetTimeout = 1.minute).onOpen(notifyMeOnOpen()) doc.akka.io/docs/akka/current/common/circuitbreaker.html 20
  21. 21. Reactive applications Responsive quick to respond or react appropriately Reactive applications use observable models, event streams and stateful clients. 21
  22. 22. Reactive applications Tools Actors Agent Future Functional Reactive Programming 22
  23. 23. Reactive applications Tools Actors Share nothing Each actor has a Mailbox (message queue) Comunicates through async and non blocking message passing Location transaparent 23
  24. 24. Reactive applications Tools Agents Reactive memory cells Send an update function to the agent which: 1) add to an ordered queue, to be 2)applied to the agent async and non blocking Reads are “free” Composes http://clojure.org/agents 24
  25. 25. Reactive applications Agent import scala.concurrent.ExecutionContext.Implicits.global import akka.agent.Agent val agent = Agent(5) agent.send(7); //Update (atomically and asyncronously) val result = agent.get //Reading an Agent's current value happens immediately If an Agent is used within an enclosing Scala STM transaction, then it will participate in that transaction 25
  26. 26. Reactive applications Tools Futures Span concurrency with not yet computed result Write once, read many Freely sharable Allows non blocking composition Monadic Built in model for managing failure 26
  27. 27. Reactive applications Future Future Read-only placeholder val f1 = Future { "Hello" + "World"} val f2 = f1 map { x ⇒ x.length } val result = Await.result(f2, 1 second) 27
  28. 28. Reactive applications Promise Writeable, single-assignment container, which completes a Future import scala.concurrent.{ future, promise } val p = promise[T] val f = p.future val producer = future { val r = produceSomething() p success r ContinueDoingSomethingUnrelated()} val consumer = future { startDoingSomething() f onSuccess { case r => doSomethingWithResult() } 28 }
  29. 29. Reactive applications Tools Functional reactive programming Extends futures with concept of stream Functional variation of the observer pattern A signal attached to a stream of events The signal is reevaluated for each event Model events on a linear timeline deterministic Compose nicely Rx, RXJava, Scala.RX, Reactive.js, Knockout.js 29
  30. 30. Reactive applications References ● http://www.reactivemanifesto.org/ ● http://akka.io/ (JAVA and SCALA API) ● Deprecating the observer pattern http://lampwww.epfl.ch/~imaier/pub/DeprecatingObserversTR 2010.pdf 30
  31. 31. Reactive applications Q &A 31
  32. 32. Reactive applications Thanks for your attention 32

×