Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Reactive programming with scala and akka


Published on

Reactive programming using scala and akka

Published in: Technology
  • Be the first to comment

Reactive programming with scala and akka

  1. 1. Reactive programming wwiitthh SSccaallaa aanndd AAkkkkaa Piyush Mishra Software Consultant Knoldus Software LLP
  2. 2. AAggeennddaa ● Reactive programming and why we need it. ● Brief introduction to Scala ● Brief introduction to Akka ● Reactive applications. ● Four principles of reactive applications.
  3. 3. PPrreesseenntt DDaayy HHooww ddoo wwee wwrriittee aapppplliiccaattiioonnss ttooddaayy?? A Foo
  4. 4. PPrreesseenntt DDaayy Till now we program like this (new Foo).doSomething1(); (new Foo).doSomething2(); (new Foo).doSomething3(); (new Foo).printOutput Synchronous ? Blocking ? Sequential ?
  5. 5. Reactive programming is completely different from the sequential work flow. They react when something is done Worker A Done and react Worker B Done and react Worker C Done and react Output Reactive programming
  6. 6. Reactive pprrooggrraammmmiinngg –– WWhhyy?? Fast, faster, fastest ….. fastetstestestest ...
  7. 7. Reactive pprrooggrraammmmiinngg –– WWhhyy??
  8. 8. WWhhaatt iiss rreeaaccttiivvee??
  9. 9. What iiss rreeaaccttiivvee -- DDiisssseeccttiioonn
  10. 10. What iiss rreeaaccttiivvee -- DDiisssseeccttiioonn
  11. 11. What iiss rreeaaccttiivvee -- DDiisssseeccttiioonn
  12. 12. What iiss rreeaaccttiivvee -- DDiisssseeccttiioonn
  13. 13. What iiss rreeaaccttiivvee -- TTooggeetthheerr
  14. 14. Reactive – Parts – Event Driven Concurrent application uses shared memory to communicate. Thread A Thread B R/W R/W Shared memory Synchronization Non-determinism Hard to understand code
  15. 15. Reactive – Parts – Event Driven Event driven system avoid shared memory communication And uses message passing model throughout the Application Actor A Message Actor B Continued..
  16. 16. Reactive –– PPaarrttss –– EEvveenntt DDrriivveenn Sender Message Receiver Loosely coupled Easier to extend Evolve Receiver dormant till it receives message Large number of receivers share same hardware thread Low latency High throughput Lower operational costs
  17. 17. Reactive –– PPaarrttss –– EEvveenntt DDrriivveenn UI Web Layer Services Database Application must be reactive from top to bottom. None of the layers should stall.
  18. 18. Reactive –– PPaarrttss –– EEvveenntt DDrriivveenn We need to go in parallel. PERIOD!
  19. 19. Reactive –– PPaarrttss –– SSccaallaabbllee The word scalable is defined by Merriam- Webster as “capable of being easily expanded or upgraded on demand”
  20. 20. Reactive –– PPaarrttss –– SSccaallaabbllee Node 1 Actor A Node 2 Message Actor B Asynchronous message passing Location transparency The network is inherently unreliable
  21. 21. RReeaaccttiivvee –– PPaarrttss –– RReessiilliieenntt Isolate failures Prevent cascading failures
  22. 22. RReeaaccttiivvee –– PPaarrttss –– RReessiilliieenntt Node 1 Actor A Node 2 Message Actor B Loose coupling Isolated components Hierarchy of failure management
  23. 23. Reactive –– PPaarrttss –– RReessppoonnssiivvee Responsive is defined by Merriam- Webster as "quick to respond or react appropriately". Respond immediately Even in event of failure
  24. 24. Reactive –– PPaarrttss –– RReessppoonnssiivvee Response event is returned in O(1) or at least O(log n) time regardless of load How ? Batching when there is high traffic Queues bound with back pressure Failures handled with recovery and circuit breakers
  25. 25. RReeaaccttiivvee –– tthhee wwhhoollee
  26. 26. RReeaaccttiivvee –– HHooww??
  27. 27. RReeaaccttiivvee –– HHooww??
  28. 28. SSccaallaa BBaassiiccss
  29. 29. SSccaallaa BBaassiiccss
  30. 30. SSccaallaa BBaassiiccss 1. Scala method definitions def fun(x: Int) = { result } 2 . def fun = result 3 .Scala variable definitions var x: Int = expression val x: String = expression 1. Java method definitions Int fun(int x) { return result } 2. (no parameterless methods) 3. java variable definitions Int x = expression final String x = expression
  31. 31. Scala BBaassiiccss –– CCoonncciissee ccooddee
  32. 32. SSccaallaa BBaassiiccss pure object system operator overloading closures mixin composition with traits special treatment of interfaces existential types abstract pattern matching static members primitive types break and continue Interfaces Wildcards raw types enums
  33. 33. What is Akka Akka is the framework developed by Typesafe Right abstraction with actors for concurrent, fault-tolerant and scalable applications For Fault-Tolerance uses “Let It Crash” model Abstraction for transparent distribution of load We can Scale In and Scale Out
  34. 34. Problem Which Akka Solve Truly highly concurrent systems Truly scalable systems Self-healing, fault-tolerant systems
  35. 35. Akka Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notification etc Low level concurrency becomes Simple Workflow - we only think in terms of message flows in system We get high CPU utilization, low latency, high throughput and scalability - for free as part of this model Proven and superior model for detecting and recovering from errors
  36. 36. Akka Main part of Akka Understanding the ACTOR model
  37. 37. Actor Model Messages are in mailbox but no thread is allocated
  38. 38. Actor Model Actor read the message , a thread is allocated to it And it apply behavior on it and state change
  39. 39. Actor Model After processing thread is deallocated
  40. 40. DDeeffiinnee aaccttoorr Define message on which actor will react. case object Hello Define actor Class HelloActor extends Actor { def receive = { case Hello => println(“Hello”) case _=> println(“I did not understand”) } }
  41. 41. Tying it back – Event Driven Message Passing ! ?
  42. 42. Tying it back – Event Driven Code
  43. 43. Tying it back – Resilience
  44. 44. RReessiilliieenntt Applications which are fault tolerant, detect failure and recover from failure are resilient. Such system are self healed and unstoppable
  45. 45. Actor System Guardian actor Actor A Actor B Actor C Actor D Actor System can be think of as home for actors
  46. 46. Actors form parent child relationship Guardian actor Actor A Actor B /A Actor C Actor D /A/C /B /B/D
  47. 47. OOnnee ffoorr oonnee SSttrraatteeggyy Guardian System actor X Y z
  48. 48. OOnnee ffoorr oonnee SSttrraatteeggyy Guardian System actor X Y z Only x will be restated
  49. 49. OOnnee ffoorr aallll SSttrraatteeggyy Guardian System actor X Y z
  50. 50. OOnnee ffoorr aallll SSttrraatteeggyy Guardian System actor X Y z All children will be restated
  51. 51. Tying it back – Resilience Code
  52. 52. Tying it back – Scalability
  53. 53. SSccaallaabbllee Applications which are able to scale up/down as the load Increases or decreases are scalable. Scaling is of two types. 1. Scaling the application on all cpu's core is called scale up. 2. Scaling the application to multiple machine is called scale out
  54. 54. AAkkkkaa SSccaallaabbiilliittyy CCooddee
  55. 55. RReessppoonnssiivvee Application which are able to respond even if failure occurs or load increases are responsive.
  56. 56. RReessppoonnssiivvee Bringing these three traits (event driven, resilient, and Scalable) together make to the applications responsive and all these four qualities together make the application reactive.
  57. 57. Reactive ppllaattffoorrmm –– ccoommppoonneennttss Play (Reactive we application framework built on top of akka) Akka Clustering Akka persistence Adaptive load balancing Software Transnational memory
  58. 58. TThhaannkkss