Your SlideShare is downloading. ×
Real World Akka Actor Recipes JavaOne 2013
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Real World Akka Actor Recipes JavaOne 2013

3,757
views

Published on

Patterns for actor based programs presented at JavaOne2013.

Patterns for actor based programs presented at JavaOne2013.

Published in: Technology, Business

0 Comments
15 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,757
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
90
Comments
0
Likes
15
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Real -World Akka Actor Recipes Björn Antonsson @bantonsson co-authored with Jamie Allen and Patrik Nordwall Tuesday, 1 October 13
  • 2. @bantonsson@bantonsson Overview • What is Akka? • What are actors? • Getting into the flow • Getting your message across • Tying it all together 2 Tuesday, 1 October 13
  • 3. What is Akka? Tuesday, 1 October 13
  • 4. @bantonsson@bantonsson What is Akka? • Toolkit and runtime for reactive applications • Write applications that are – Concurrent – Distributed – Fault tolerant – Event-driven 4 Tuesday, 1 October 13
  • 5. @bantonsson@bantonsson What is Akka? • Has multiple tools – Actors – Futures – Dataflow – Remoting – Clustering 5 Tuesday, 1 October 13
  • 6. What are actors? Tuesday, 1 October 13
  • 7. @bantonsson@bantonsson What are actors? • Isolated lightweight event-based processes • Share nothing • Communicate through async messages • Each actor has a mailbox (message queue) • Location transparent (distributable) • Supervision-based failure management 7 Tuesday, 1 October 13
  • 8. @bantonsson@bantonsson What is an actor good for? • An island of sanity in a sea of concurrency • Everything inside the actor is sequential – Processes one message at a time • Very lightweight – Create millions – Create short lived • Inherently concurrent 8 Tuesday, 1 October 13
  • 9. @bantonsson@bantonsson Actors compared to Objects • Think of an Actor as an Object • You can't peek inside it • You don't call methods – You send messages (asynchronously) • You don't get return values – You receive messages (asynchronously) • The internal state is thread safe 9 Tuesday, 1 October 13
  • 10. Why should I care? Tuesday, 1 October 13
  • 11. The world is multicore! Tuesday, 1 October 13
  • 12. @bantonsson@bantonsson 12 Amdahl’s Law Tuesday, 1 October 13
  • 13. @bantonsson@bantonsson So what's the catch? • Really no catch • A different programming paradigm • All about tradeoffs – Some things are easier some harder • Think different 13 Tuesday, 1 October 13
  • 14. Getting into the flow Tuesday, 1 October 13
  • 15. @bantonsson@bantonsson Getting into the flow • Why do you need flow control? • How do you control the flow? • Do you really need all messages? 15 Tuesday, 1 October 13
  • 16. @bantonsson@bantonsson Why do you need flow control? • Function calls are blocking • Message sends are asynchronous • Possible problems – Produce jobs too fast – Many jobs need much CPU and/or Memory – External resources have limits – Unpredictable job patterns 16 Tuesday, 1 October 13
  • 17. @bantonsson@bantonsson Why do you need flow control? • Free flow of messages can lead to – Blocked from external resource – Actor mailbox backup – Slow system – Out of Memory 17 Tuesday, 1 October 13
  • 18. @bantonsson@bantonsson How do you control the flow? • Push with rate limiting – A fixed number of jobs per time unit • Push with acknowledgment – A fixed number of jobs can be in progress – New jobs are started a"er old jobs finish • Pull – New jobs are pulled as old are completed 18 Tuesday, 1 October 13
  • 19. @bantonsson@bantonsson Push with rate limiting • A timer sends ticks at fixed intervals • On every tick the master gets new tokens • When there are no tokens, jobs get queued • When there are tokens, start queued jobs 19 Tuesday, 1 October 13
  • 20. @bantonsson@bantonsson 20 Worker Master [0] Queue [0] Timer Worker 1. Work Tuesday, 1 October 13
  • 21. @bantonsson@bantonsson 21 Worker Master [0] Queue [1] Timer Worker 1. Work 2. Enqueue Tuesday, 1 October 13
  • 22. @bantonsson@bantonsson 22 Worker Master [5] Queue [1] Timer Worker 1. Work 2. Enqueue 3. Tick Tuesday, 1 October 13
  • 23. @bantonsson@bantonsson 23 Worker Master [4] Queue [0] Timer Worker 1. Work 2. Enqueue 4. Dequeue 3. Tick Tuesday, 1 October 13
  • 24. @bantonsson@bantonsson 24 Worker Master [4] Queue [0] Timer Worker 1. Work 2. Enqueue 4. Dequeue 3. Tick 5. Work Tuesday, 1 October 13
  • 25. @bantonsson@bantonsson Push with acknowledgement • A fixed number of jobs are started • Wait for ACK before starting more jobs • Jobs that can't be started are queued • When ACK arrives start queued job • To keep workers busy, send more than one job per worker – Use a high water mark to stop sending and a low water mark to start sending 25 Tuesday, 1 October 13
  • 26. @bantonsson@bantonsson 26 Worker Master [0] Queue [0] Worker 1. Work Tuesday, 1 October 13
  • 27. @bantonsson@bantonsson 27 Worker Master [0] Queue [1] Worker 1. Work 2. Enqueue Tuesday, 1 October 13
  • 28. @bantonsson@bantonsson 28 Worker Master [1] Queue [1] Worker 1. Work 2. Enqueue 3. Ack Tuesday, 1 October 13
  • 29. @bantonsson@bantonsson 29 Worker Master [0] Queue [0] Worker 1. Work 2. Enqueue 4. Dequeue 3. Ack Tuesday, 1 October 13
  • 30. @bantonsson@bantonsson 30 Worker Master [0] Queue [0] Worker 1. Work 2. Enqueue 4. Dequeue 5. Work 3. Ack Tuesday, 1 October 13
  • 31. @bantonsson@bantonsson 31 Worker Master [1] Queue [0] Worker 1. Work 2. Enqueue 4. Dequeue 5. Work 6. Ack 3. Ack Tuesday, 1 October 13
  • 32. @bantonsson@bantonsson Pull • Incoming jobs are queued • Workers ask for jobs • Jobs are handed out when available • Workers don't do active polling • Can lead to lag if jobs are small compared to cost of getting a new job – Use batching to counteract lag 32 Tuesday, 1 October 13
  • 33. @bantonsson@bantonsson 33 Worker Master Queue [0] Worker 1. Work Tuesday, 1 October 13
  • 34. @bantonsson@bantonsson 34 Worker Master Queue [1] Worker 1. Work 2. Enqueue Tuesday, 1 October 13
  • 35. @bantonsson@bantonsson 35 Worker Master Queue [1] Worker 1. Work 2. Enqueue 3. Work? Tuesday, 1 October 13
  • 36. @bantonsson@bantonsson 36 Worker Master Queue [0] Worker 1. Work 2. Enqueue 4. Dequeue 3. Work? Tuesday, 1 October 13
  • 37. @bantonsson@bantonsson 37 Worker Master Queue [0] Worker 1. Work 2. Enqueue 4. Dequeue 5. Work 3. Work? Tuesday, 1 October 13
  • 38. @bantonsson@bantonsson Do you really need all messages? • Group messages together – Batching • Discard/Aggregate messages – Scrubbing 38 Tuesday, 1 October 13
  • 39. @bantonsson@bantonsson Batching • Collect a number of messages before sending/processing them – A predefined number of messages or time • Useful for things like – Write behind – Database bulk insert/update – Heavyweight operations e.g. GUI rendering 39 Tuesday, 1 October 13
  • 40. @bantonsson@bantonsson Scrubbing • Discard or aggregate some messages – Predefined number of messages or time • Useful for things like – Financial market data – Statistics 40 Tuesday, 1 October 13
  • 41. Getting your message across Tuesday, 1 October 13
  • 42. @bantonsson@bantonsson Getting your message across • When is a message delivered? • The fallacy of guaranteed delivery • What Akka guarantees • Reliable messaging 42 Tuesday, 1 October 13
  • 43. @bantonsson@bantonsson When is a message delivered? • Function calls block until done • Message sends return immediately • Which is the right point? – Sent/Received Network? – Enqueued/Dequeued Mailbox? – Processed by Actor? • Do ACKing at the business level 43 Tuesday, 1 October 13
  • 44. @bantonsson@bantonsson Guaranteed delivery • From Enterprise Integration Patterns • Messaging system uses built-in store to persist • ACK everywhere – Producer to sender – Sender to receiver – Receiver to consumer 44 Tuesday, 1 October 13
  • 45. @bantonsson@bantonsson Lots of ACKs. What if I just... • Use Durable Mailboxes? – When is the message in the mailbox? – No guarantees that it ever got there – Still have to ACK to be certain 45 Tuesday, 1 October 13
  • 46. @bantonsson@bantonsson Lots of ACKs. What if I just... • Use an External Durable Message Queue – A SPOF/Bottleneck? – When is the message in the message queue? – The queue does ACKing internally – No guarantees that it ever gets out – Still have to ACK to be certain 46 Tuesday, 1 October 13
  • 47. @bantonsson@bantonsson Guaranteed delivery doesn't exist • Things break – Persistent store crashes – Network fails – Server goes down • Design for failure and resilience • Do ACKing at the business level 47 Tuesday, 1 October 13
  • 48. @bantonsson@bantonsson What Akka guarantees • At most once delivery – Message is only delivered once, if at all – The weakest guarantee • Ordered per actor sender-receiver pair – Actor A sends messages to actor B – If the messages are received by actor B, it will be in the order as sent by actor A 48 Tuesday, 1 October 13
  • 49. @bantonsson@bantonsson Other delivery guarantees • At least once – Message will eventually be delivered – Can happen multiple times • Exactly once – Message will eventually be delivered – Will only happen once • Have to add these yourself – They involve ACKing ;) 49 Tuesday, 1 October 13
  • 50. @bantonsson@bantonsson Reliable Messaging: At least once • Send with acknowledge – Keep sending until you get an ACK • Receive with re-request – When missing a message request it – Needs unique sequence numbers • Requires – Message store at sender (available/redundant) 50 Tuesday, 1 October 13
  • 51. @bantonsson@bantonsson 51 Sender Receiver Store Tuesday, 1 October 13
  • 52. @bantonsson@bantonsson 52 Sender Receiver Store 1. Message Tuesday, 1 October 13
  • 53. @bantonsson@bantonsson 53 Sender Receiver Store 2. Message Tuesday, 1 October 13
  • 54. @bantonsson@bantonsson 54 Sender Receiver3. Ack Store 2. Message Tuesday, 1 October 13
  • 55. @bantonsson@bantonsson 55 Sender Receiver3. Ack Store 4. Ack 2. Message Tuesday, 1 October 13
  • 56. @bantonsson@bantonsson Reliable Messaging: At least once • Use a pipe of actors before ACKing • Keep pipe free of side effects – Same message might come several times • ACK should be done at the business level 56 Tuesday, 1 October 13
  • 57. @bantonsson@bantonsson 57 Sender Check Inventory Authenticate ReceiverStore Tuesday, 1 October 13
  • 58. @bantonsson@bantonsson 58 Sender Check Inventory Authenticate ReceiverStore 1. Message Tuesday, 1 October 13
  • 59. @bantonsson@bantonsson 59 Sender Check Inventory Authenticate Receiver 2. Message Store Tuesday, 1 October 13
  • 60. @bantonsson@bantonsson 60 Sender Check Inventory Authenticate Receiver 2. Message 3. Message' Store Tuesday, 1 October 13
  • 61. @bantonsson@bantonsson 61 Sender Check Inventory Authenticate Receiver 4. Message'' 2. Message 3. Message' Store Tuesday, 1 October 13
  • 62. @bantonsson@bantonsson 62 Sender Check Inventory Authenticate Receiver 4. Message'' 2. Message 3. Message' 5. Ack Store Tuesday, 1 October 13
  • 63. @bantonsson@bantonsson 63 Sender Check Inventory Authenticate Receiver 4. Message'' 2. Message 3. Message' 5. Ack Store 6. Ack Tuesday, 1 October 13
  • 64. @bantonsson@bantonsson Receive with re-request • When missing a message request it • Requires – Uniquely identifiable sequence – Message store at sender (available/redundant) 64 Tuesday, 1 October 13
  • 65. @bantonsson@bantonsson 65 Sender Receiver Store Tuesday, 1 October 13
  • 66. @bantonsson@bantonsson 66 Sender Receiver Store 1. Message Tuesday, 1 October 13
  • 67. @bantonsson@bantonsson 67 Sender Receiver Store 2. Message Tuesday, 1 October 13
  • 68. @bantonsson@bantonsson 68 Sender Receiver Store 3. Message' Tuesday, 1 October 13
  • 69. @bantonsson@bantonsson 69 Sender Receiver Store 4. Message' Tuesday, 1 October 13
  • 70. @bantonsson@bantonsson 70 Sender Receiver5. Request Store 4. Message' Tuesday, 1 October 13
  • 71. @bantonsson@bantonsson 71 Sender Receiver Store 6. Message Tuesday, 1 October 13
  • 72. @bantonsson@bantonsson 72 Sender Receiver Store 6. Message 7. Message Tuesday, 1 October 13
  • 73. @bantonsson@bantonsson 73 Sender Receiver Store 8. Message' Tuesday, 1 October 13
  • 74. @bantonsson@bantonsson 74 Sender Receiver Store 8. Message' 9. Message' Tuesday, 1 October 13
  • 75. Tying it all together Tuesday, 1 October 13
  • 76. @bantonsson@bantonsson Tying it all together • Distributed Workers Example – Front Ends receive requests – Master receives work from Front Ends – Workers pull work from Master • Available as a Typesafe Activator template – Zero configuration setup – Code and tutorial 76 Tuesday, 1 October 13
  • 77. @bantonsson@bantonsson 77 Worker Master Worker Worker Worker Frontend Frontend Tuesday, 1 October 13
  • 78. @bantonsson@bantonsson Tying it all together • Further Goals – Elastic addition/removal of front end nodes – Elastic addition/removal of workers – Thousands of workers – Jobs should not be lost 78 Tuesday, 1 October 13
  • 79. @bantonsson@bantonsson 79 Master Worker Frontend Work Executor Tuesday, 1 October 13
  • 80. @bantonsson@bantonsson 80 Master Worker Frontend Work Executor RegisterWorker Tuesday, 1 October 13
  • 81. @bantonsson@bantonsson 81 Master Worker Frontend 1. Work Work Executor Tuesday, 1 October 13
  • 82. @bantonsson@bantonsson 82 Master Worker Frontend 1. Work 2. Work Work Executor Tuesday, 1 October 13
  • 83. @bantonsson@bantonsson 83 Master Worker Frontend 1. Work 2. Work 3. Ack Work Executor Tuesday, 1 October 13
  • 84. @bantonsson@bantonsson 84 Master Worker Frontend 1. Work 2. Work 3. Ack 4. OK Work Executor Tuesday, 1 October 13
  • 85. @bantonsson@bantonsson 85 Master Worker Frontend 1. Work 2. Work 3. Ack 4. OK 5. WorkIsReady Work Executor Tuesday, 1 October 13
  • 86. @bantonsson@bantonsson 86 Master Worker Frontend 1. Work 2. Work 3. Ack 4. OK 5. WorkIsReady 6. WorkerRequestWork Work Executor Tuesday, 1 October 13
  • 87. @bantonsson@bantonsson 87 Master Worker Frontend 1. Work 2. Work 3. Ack 4. OK 5. WorkIsReady 6. WorkerRequestWork 7. Work Work Executor Tuesday, 1 October 13
  • 88. @bantonsson@bantonsson 88 Master Worker Frontend 1. Work 2. Work 3. Ack 4. OK 5. WorkIsReady 6. WorkerRequestWork 7. Work Work Executor 8. Work Tuesday, 1 October 13
  • 89. @bantonsson@bantonsson 89 Master Worker Frontend Work Executor Tuesday, 1 October 13
  • 90. @bantonsson@bantonsson 90 Master Worker Frontend Work Executor 9. WorkComplete Tuesday, 1 October 13
  • 91. @bantonsson@bantonsson 91 Master Worker Frontend Work Executor 9. WorkComplete 10. WorkIsDone Tuesday, 1 October 13
  • 92. @bantonsson@bantonsson 92 Master Worker Frontend Work Executor 9. WorkComplete 10. WorkIsDone 11. Ack Tuesday, 1 October 13
  • 93. @bantonsson@bantonsson Cluster Technologies/Patterns • Distributed Pub/Sub Mediator – Publish and Subscribe to message flows • Cluster Singleton – HA singleton actor instance within the cluster • Cluster Client – Let other systems connect to the cluster 93 Tuesday, 1 October 13
  • 94. @bantonsson@bantonsson DistributedPubSubMediator 94 Frontend Master Mediator Mediator Tuesday, 1 October 13
  • 95. @bantonsson@bantonsson DistributedPubSubMediator 95 Frontend Master Mediator Mediator Put Tuesday, 1 October 13
  • 96. @bantonsson@bantonsson DistributedPubSubMediator 96 Frontend Master Mediator Mediator Send Tuesday, 1 October 13
  • 97. @bantonsson@bantonsson DistributedPubSubMediator 97 Frontend Master Mediator Mediator Send Tuesday, 1 October 13
  • 98. @bantonsson@bantonsson ClusterSingleton 98 ClusterSingleton Manager ClusterSingleton Manager Master Master (Standby) Tuesday, 1 October 13
  • 99. @bantonsson@bantonsson ClusterSingleton 99 ClusterSingleton Manager ClusterSingleton Manager Master Master (Standby) Tuesday, 1 October 13
  • 100. @bantonsson@bantonsson ClusterSingleton 100 ClusterSingleton Manager ClusterSingleton Manager Master Master Tuesday, 1 October 13
  • 101. @bantonsson@bantonsson ClusterClient & ClusterSingleton 101 Master Mediator Mediator Receptionist Master (Standby) Receptionist Cluster Client Cluster Client Worker Worker Tuesday, 1 October 13
  • 102. @bantonsson@bantonsson 102 Master Mediator Mediator Receptionist Master (Standby) Receptionist Cluster Client Cluster Client Worker Worker Cluster Client Worker Worker Frontend Frontend Tuesday, 1 October 13
  • 103. Resources Tuesday, 1 October 13
  • 104. @bantonsson@bantonsson Resources: Blog Posts • Push with rate limiting – Kaspar Fischer http://letitcrash.com/post/28901663062/throttling-messages-in-akka-2 • Pull – Derek Wyatt http://letitcrash.com/post/29044669086/balancing-workload-across-nodes-with- akka-2 – Michael Pollmeier http://www.michaelpollmeier.com/akka-work-pulling-pattern-to-throttle-work/ 104 Tuesday, 1 October 13
  • 105. @bantonsson@bantonsson Resources: Typesafe Activator • http://typesafe.com/platform/getstarted 105 Tuesday, 1 October 13
  • 106. @bantonsson@bantonsson Resources: Coursera Course • Principles of Reactive Programming by Martin Odersky, Erik Meijer and Roland Kuhn – Starts 4th of November 2013 – 7 weeks – Workload: 5-7 hours a week – Free as in free beer • https://www.coursera.org/course/reactive 106 Tuesday, 1 October 13
  • 107. Björn Antonsson bjorn.antonsson@typesafe.com @bantonsson Tuesday, 1 October 13