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.

Building Reactive Applications With Akka And Java

607 views

Published on

Building Reactive Applications With Akka And Java

Published in: Technology
  • Be the first to comment

Building Reactive Applications With Akka And Java

  1. 1. Building Reactive Systems with Akka Tu Pham CTO @ Dyno.me Google Developer Expert on Cloud Technology
  2. 2. The rules of thegame have changed
  3. 3. Apps inthe 60s-90s were writtenfor Apps today are writtenfor Single machines Single coreprocessors Expensive RAM Expensive disk Slow networks Few concurrent users Small data sets Latency in seconds Clusters ofmachines Multicore processors Cheap RAM Cheap disk Fast networks Lots of concurrentusers Large data sets Latency in milliseconds 3
  4. 4. Reactive applications share four traits ReactiveApplications 5
  5. 5. Reactive applications enrich the user experience with low latencyresponse.
  6. 6. Responsive • Real-time, engaging, rich andcollaborative • Create an open and ongoing dialog with users • More efficient workflow; inspires a feeling of connectedness • Fully Reactive enabling push instead of pull “The move to these technologies is already paying off. Response times are down for processor intensive code–such as image andPDF generation–by around 75%.” Brian Pugh, VP of Engineering, Lucid Software 7
  7. 7. Reactive applications react to changes in the world around them.
  8. 8. Message-Driven • Loosely coupled architecture, easier to extend, maintain, evolve • Asynchronous and non-blocking • Concurrent by design, immutablestate • Lower latency and higher throughput “Clearly, the goal is to do these operations concurrently and non-blocking, so that entire blocks of seats or sections are not locked. We’re able to find and allocate seats under load in less than 20ms without trying very hard to achieve it.” Andrew Headrick, Platform Architect,Ticketfly 9
  9. 9. Introducing the ActorModel
  10. 10. Acomputational model thatembodies: 11 ✓Processing ✓Storage ✓Communication Supports 3 axioms—when an Actor receives a message it can: 1.Create newActors 2.Send messages to Actors it knows 3.Designate how it should handle the next message it receives The ActorModel
  11. 11. Theessence of an actor from Akka’sperspective 12 1. DEFINE 2. CREATE 3. SEND 4. BECOME 5. SUPERVISE
  12. 12. public abstract class BaseEvent implements Serializable { private final String id; private final JsonElement data; // Can be Json Primitive, Object, Array private int created; public BaseEvent(String id, JsonElement data) { this.id = id; this.data = data; this.created = DateTimeUtil.getCurrentUnixTime(""); } // getter & setter here } X 0.DEFINE
  13. 13. public class Event extends BaseEvent { private EventType eventType; public Event(String id, JsonElement data){ super(id, data); } public Event(String id, EventType eventType, JsonElement data) { super(id, data); this.eventType = eventType; } } X 0.DEFINE
  14. 14. public class SimpleActor extends UntypedActor { @Override public void onReceive(Object msg) throws Exception { if (msg instanceof Event) { // Say hello System.out.println(“Hello ” + msg.getId()) } } } X 0.DEFINE
  15. 15. Give it a name 1.CREATE Yougetan ActorRef back Create theActor Create an Actor system Actorconfiguration ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(Props.create(SimpleActor.class), “greeter");
  16. 16. Guardian SystemActor Actors can formhierarchies
  17. 17. Guardian SystemActor system.actorOf(Props.create(Foo.class), “Foo”); Actors can formhierarchies
  18. 18. Foo Guardian SystemActor system.actorOf(Props.create(Foo.class), “Foo”); Actors can formhierarchies
  19. 19. A Foo Guardian SystemActor Actors can formhierarchies
  20. 20. A Foo Guardian SystemActor context().actorOf(Props.create(A.class), “A”); Actors can formhierarchies
  21. 21. A B BarFoo C B E A D C Guardian SystemActor Actors can formhierarchies
  22. 22. A B BarFoo C B E A C D Guardian SystemActor Actors can formhierarchies
  23. 23. A B BarFoo C B E A C D Guardian SystemActor Name resolution—like a file-system
  24. 24. A B BarFoo C B E A C /Foo D Guardian SystemActor Name resolution—like a file-system
  25. 25. A B BarFoo C B E A C /Foo /Foo/A D Guardian SystemActor Name resolution—like a file-system
  26. 26. A B BarFoo C B E A C /Foo /Foo/A /Foo/A/B D Guardian SystemActor Name resolution—like a file-system
  27. 27. A B BarFoo C B E A C /Foo /Foo/A /Foo/A/B Guardian SystemActor /Foo/A/D D Name resolution—like a file-system
  28. 28. 2.SEND X simpleRef.tell(new Event(new UUID().toString(), null, jsonObject), null); Send the message asynchronously Passin the senderActorRef
  29. 29. Reactive applications are architected to handle failure at all levels.
  30. 30. Resilient • Failure is embraced as a natural state in the app lifecycle • Resilience is a first-class construct • Failure is detected, isolated, and managed • Applications selfheal “The Typesafe Reactive Platform helps us maintain a very aggressive development and deployment cycle, all in a fail-forward manner. It’s now the default choice for developing all new services.” Peter Hausel, VP Engineering, Gawker Media 21
  31. 31. Think Vending Machine
  32. 32. Coffee Machine Programmer Think VendingMachine
  33. 33. Coffee Machine Programmer Inserts coins Think VendingMachine
  34. 34. Coffee Machine Programmer Inserts coins Add morecoins Think VendingMachine
  35. 35. Coffee Machine Programmer Inserts coins Gets coffee Add morecoins Think VendingMachine
  36. 36. Coffee Machine Programmer Think VendingMachine
  37. 37. Coffee Machine Programmer Inserts coins Think VendingMachine
  38. 38. Coffee Machine Programmer Inserts coins Think VendingMachine Out of coffee beans error
  39. 39. Coffee Machine Programmer Inserts coins Think VendingMachine Out of coffee beans error Wrong
  40. 40. Coffee Machine Programmer Inserts coins Think VendingMachine
  41. 41. Coffee Machine Programmer Inserts coins Out of coffee beans error Think VendingMachine
  42. 42. Coffee Machine Programmer Service Guy Inserts coins Out of coffee beans error Think VendingMachine
  43. 43. Coffee Machine Programmer Service Guy Inserts coins Out of coffee beans error Adds more beans Think VendingMachine
  44. 44. Coffee Machine Programmer Service Guy Inserts coins Gets coffee Out of coffee beans error Adds more beans Think VendingMachine
  45. 45. The RightWay ServiceClient
  46. 46. The RightWay ServiceClient Request
  47. 47. The RightWay ServiceClient Request Response
  48. 48. The RightWay ServiceClient Request ValidationError Response
  49. 49. The RightWay ServiceClient Request ValidationError Application Response Error
  50. 50. The RightWay ServiceClient Supervisor Request ValidationError Application Response Error
  51. 51. The RightWay ServiceClient Supervisor Request ValidationError Application Error Manages Failure Response
  52. 52. • Isolate the failure • Compartmentalize • Manage failure locally • Avoid cascading failures Use Bulkheads
  53. 53. • Isolate thefailure • Compartmentalize • Manage failurelocally • Avoid cascadingfailures Use Bulkheads
  54. 54. EnterSupervision
  55. 55. A B BarFoo C B E A D C Supervisor hierarchies Automatic and mandatory supervision
  56. 56. Reactive applications scale up and down to meet demand.
  57. 57. Elastic • Elasticity and Scalability to embrace the Cloud • Adaptive Scale on Demand • Clustered servers support joining and leaving of nodes • More cost-efficient utilization of hardware “Our traffic can increase by as much as 100x for 15 minutes each day. Until a couple of years ago, noon was a stressful time. Nowadays, it’s usually a non-event.” Eric Bowman, VP Architecture, Gilt Groupe 33
  58. 58. Scale UP Scale OUT 34
  59. 59. Essentially the samething 34
  60. 60. 1.Minimize Contention 2.Maximize Locality ofReference 35 Weneedto
  61. 61. Share NOTHING Design 36
  62. 62. Fully event-drivenapps are a necessity Amdahl’s Law will hunt you down X
  63. 63. Typesafe Activator http://typesafe.com/platform/getstarted
  64. 64. Typesafe ReactivePlatform • • Actors are asynchronous and communicate via message passing Supervision and clustering in support of fault tolerance • • Purely asynchronous and non-blocking webframeworks No container required, no inherent bottlenecks in session management • • Asynchronous and immutable programmingconstructs Composable abstractions enabling simpler concurrency and parallelism 48
  65. 65. Reactive is being adopted across a wide range of industries.
  66. 66. Finance Internet/Social Media Mfg/Hardware Government Retail
  67. 67. DEMO TIMEAsimple game of ping pong
  68. 68. 3.BECOME X
  69. 69. Questions? Email: tu@dyno.me

×