Building reactive applications with akka in java

50 %
50 %
Information about Building reactive applications with akka in java

Published on November 27, 2016

Author: phamphuongtu

Source: slideshare.net

1. Building Reactive Systems with Akka Tu Pham CTO @ Dyno.me Google Developer Expert on Cloud Technology

2. The rules of thegame have changed

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. Reactive applications share four traits ReactiveApplications 5

5. Reactive applications enrich the user experience with low latencyresponse.

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. Reactive applications react to changes in the world around them.

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. Introducing the ActorModel

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. Theessence of an actor from Akka’sperspective 12 1. DEFINE 2. CREATE 3. SEND 4. BECOME 5. SUPERVISE

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. 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. 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. 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. Guardian SystemActor Actors can formhierarchies

17. Guardian SystemActor system.actorOf(Props.create(Foo.class), “Foo”); Actors can formhierarchies

18. Foo Guardian SystemActor system.actorOf(Props.create(Foo.class), “Foo”); Actors can formhierarchies

19. A Foo Guardian SystemActor Actors can formhierarchies

20. A Foo Guardian SystemActor context().actorOf(Props.create(A.class), “A”); Actors can formhierarchies

21. A B BarFoo C B E A D C Guardian SystemActor Actors can formhierarchies

22. A B BarFoo C B E A C D Guardian SystemActor Actors can formhierarchies

23. A B BarFoo C B E A C D Guardian SystemActor Name resolution—like a file-system

24. A B BarFoo C B E A C /Foo D Guardian SystemActor Name resolution—like a file-system

25. A B BarFoo C B E A C /Foo /Foo/A D Guardian SystemActor Name resolution—like a file-system

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. 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. 2.SEND X simpleRef.tell(new Event(new UUID().toString(), null, jsonObject), null); Send the message asynchronously Passin the senderActorRef

29. Reactive applications are architected to handle failure at all levels.

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. Think Vending Machine

32. Coffee Machine Programmer Think VendingMachine

33. Coffee Machine Programmer Inserts coins Think VendingMachine

34. Coffee Machine Programmer Inserts coins Add morecoins Think VendingMachine

35. Coffee Machine Programmer Inserts coins Gets coffee Add morecoins Think VendingMachine

36. Coffee Machine Programmer Think VendingMachine

37. Coffee Machine Programmer Inserts coins Think VendingMachine

38. Coffee Machine Programmer Inserts coins Think VendingMachine Out of coffee beans error

39. Coffee Machine Programmer Inserts coins Think VendingMachine Out of coffee beans error Wrong

40. Coffee Machine Programmer Inserts coins Think VendingMachine

41. Coffee Machine Programmer Inserts coins Out of coffee beans error Think VendingMachine

42. Coffee Machine Programmer Service Guy Inserts coins Out of coffee beans error Think VendingMachine

43. Coffee Machine Programmer Service Guy Inserts coins Out of coffee beans error Adds more beans Think VendingMachine

44. Coffee Machine Programmer Service Guy Inserts coins Gets coffee Out of coffee beans error Adds more beans Think VendingMachine

45. The RightWay ServiceClient

46. The RightWay ServiceClient Request

47. The RightWay ServiceClient Request Response

48. The RightWay ServiceClient Request ValidationError Response

49. The RightWay ServiceClient Request ValidationError Application Response Error

50. The RightWay ServiceClient Supervisor Request ValidationError Application Response Error

51. The RightWay ServiceClient Supervisor Request ValidationError Application Error Manages Failure Response

52. • Isolate the failure • Compartmentalize • Manage failure locally • Avoid cascading failures Use Bulkheads

53. • Isolate thefailure • Compartmentalize • Manage failurelocally • Avoid cascadingfailures Use Bulkheads

54. EnterSupervision

55. A B BarFoo C B E A D C Supervisor hierarchies Automatic and mandatory supervision

56. Reactive applications scale up and down to meet demand.

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. Scale UP Scale OUT 34

59. Essentially the samething 34

60. 1.Minimize Contention 2.Maximize Locality ofReference 35 Weneedto

61. Share NOTHING Design 36

62. Fully event-drivenapps are a necessity Amdahl’s Law will hunt you down X

63. Typesafe Activator http://typesafe.com/platform/getstarted

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. Reactive is being adopted across a wide range of industries.

66. Finance Internet/Social Media Mfg/Hardware Government Retail

67. DEMO TIMEAsimple game of ping pong

68. 3.BECOME X

69. Questions? Email: tu@dyno.me

Add a comment