Actor-based concurrency in a modern Java Enterprise

download Actor-based concurrency in a modern Java Enterprise

of 40

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of Actor-based concurrency in a modern Java Enterprise

  1. 1. Actor-based concurrency in a modern Java Enterprise Alexander Lukyanchikov
  2. 2. Agenda - Overview of concurrency models - Introduc5on to Akka - When to use it - How we use it - Akka with Java and Spring - Pros&Cons, tricky things - Live demo!
  3. 3. Free lunch is over
  4. 4. Amdahl's Law S - speedup - serial part of the algorithm p - number of the processors Fundamental limita5on of parallel compu5ng
  5. 5. Gunther's Law
  6. 6. Overview of concurrency models Threads - the basic unit of concurrency, the closest to hardware - has it's own stack (~ 1Mb) - shared mutable state should be synchronized - sync/locks/context switching are expensive - race condi5ons, starva5on, dead- and live-locks
  7. 7. Overview of concurrency models - helps to create, manage and u5lize the threads - s5ll have to think about - thread pool size - task queue size - overow strategy Thread pools
  8. 8. Overview of concurrency models - let to aTach callbacks to the asynchronous task - chaining mul5ple callbacks - control the thread/pool for each task - acceptEither(), thenCombine() and other helpful methods Future composiDon
  9. 9. Overview of concurrency models ReacDve Streams - dene a source and a consumer, library will take care of pushing the data - transform, combine, split and compose streams - non-blocking back pressure - async error handling
  10. 10. Overview of concurrency models - lightweight threads, scheduled at the applica5on level - no performance penalty for context switch - thread-like syntax - requires bytecode modica5on Fibers
  11. 11. Overview of concurrency models - extremely lightweigh, independent workers with state - communicate with other actors only by message-passing - can monitor child actors for failure and take measures - that's how OOP was ini5ally thought of Actor model
  12. 12. Akka - Actor model on JVM - inspired by Erlang - Scala and Java API - free and open-source - Scale UP (concurrency) - Scale OUT (Remo5ng + Cluster) - 50 million msg/sec on single machine - 2.5 million actors per GB of heap
  13. 13. Akka Actor - Actor is state, behavior, mailbox, child actors and supervision strategy - On receive it can: - send messages to other actors - create new actors - change the state - Each one has ActorRef and logical path - Message sending: tell & ask akka://my-system/user/my-actor/its-child/...
  14. 14. Akka Actor "Happens before" rules: - the send of the message to an actor happens before the receive of that message by the same actor - processing of one message happens before processing of the next message by the same actor. - Many actors share one thread - Subsequent actor invoca5ons may be on dierent threads
  15. 15. System design Pipes & Filters Content-based router Message endpoint RouDng slipResequencer ScaLer-gather Classic Enterprise Integra5on PaTerns to the rescue ReacDve Messaging PaLerns with the Actor Model: ApplicaDons and IntegraDon in Scala and Akka Vaughn Vernon, 2015
  16. 16. Let it crash! - Defensive programming is an an5-paTern - Actors should be organized in a hierarchy - Parent actor handle child actors failures - Using thrown excep5on, parent decides what to do: resume (skip faulty message), restart, stop or escalate the error - One-for-one strategy - All-for-one strategy - hooks: preStart(), preRestart(), postStop(), etc business-logic an error-handling ows are separated
  17. 17. Let it crash!
  18. 18. Dispatchers - Dispatcher is an abstrac5on over resources (thread pools) - 3 types: - Dispatcher - PinnedDispatcher - CallingThreadDispatcher - 2 executors: - thread-pool-executor - fork-join-executor - min/max number of threads and throughput are congurable I/OCPU-intensive default Bulkhead Pattern
  19. 19. Routers A router with specied dispatcher: Router types: - Pool (creates routees as child actors) - Group (sends messages to the specied path) Balancing strategies: - Round robin - Random - Smallest mailbox - Broadcast - ScaTer gather rst completed
  20. 20. ReacDve systems ReacDve programming vs ReacDve Systems ElasDc Resilent Message driven Responsive
  21. 21. Akka Remote provides loca=on transparency for peer-to-peer communica5on - one abstrac=on across all dimensions of scale Node A Node B akka.://@:/ - Java Serializa5on by default, Kryo/Protobuf/Your-own-serializer supported - SSL/TLS can be used as the remote transport - Actors can be deployed on remote host - Routers can use remote des5na5ons
  22. 22. Akka Cluster AP System, based on Amazon's Dynamo paper (Riak DB approach) - Communica5on via Gossip protocol - Vector clocks to reconcile/merge conicts - Consistent hashing for par55oning - No leader-elec5on process, it is just a role - Failure detector to nd unreachable nodes - Seed nodes to start a cluster provides membership service with no single point of failure Cluster Singleton Ensures that you have exactly one actor of a certain type running in the cluster Cluster Sharding Distributes actors across several nodes and supports interac8on with the actors using their logical iden8er without having to care about their physical loca8on Cluster Client Communica8on from an actor system that is not part of the cluster to actors running somewhere in the cluster Distributed Publish Subscribe Publish-subscribe and point-to-point messaging between actors in the cluster, sender doesn't have to know on which node the target actor is running Distributed Data Key-value store like API to share data between nodes in an the cluster
  23. 23. Akka is awesome! - Simple and powerful way to abstract from low-level concurrency issues - Ability to build reliable decentralized architecture and scale out with no code changes - Wonderful documenta5on & community
  24. 24. But. - Need a new mindset - Code complexity: actors communica5on graph might be tricky Find Usages from IDE won't help here - Loss of typesafety between actors you can live with it, with proper tesAng - Stacktraces lack meaning what was the message? what's the internal state? - Possible deadlocks bounded mailboxes, synchronizaAon between actors - Possible OutOfMemoryErrors unbounded mailboxes under load
  25. 25. Tricky things - Messages should be immutable - By default, delivery is not guaranteed* - Backpressure is important Work Pulling? Bounded mailbox? Something else? - Be careful with blocking opera5ons Avoid shared state & synchronizaAon, use separate dispatcher for I/O * Nobody Needs Reliable Messaging hTps://
  26. 26. When to use it - Akka's creators: almost everywhere - Transac5on processing (online gaming, nance, sta5s5cs) - Service backend (any industry, any app) - Snap-in concurrency/parallelism (any app) - Batch processing (any industry) - Communica5ons hub (telecom, web media, mobile media) - BI/datamining/general purpose crunching - Common sense: when performance and scalability is crucial - Handle large number of concurrent ac5vi5es - Be easily distributable & scalable over a network of machines - Well known success stories: Messaging systems, Search indexing, Web-crawling, Sol-real5me analy5cs, Game servers, etc
  27. 27. How we use it Communica5on hub / Messaging system - 500k concurrent websocket connec5ons - 8 c4.xlarge (1.5-2 overprovisioned) - Receive rate per server: ~ 200 messages per second - Send rate per server: ~ 1000 messages per second - Round-trip latency within a system - 80th pct: 20 ms - 90th pct: 50 ms - 95th pct: 200 ms - Akka Java API - Spring (DI, websockets, async controllers, etc)
  28. 28. Java Scala Scala vs Java API
  29. 29. Java Scala Scala vs Java API
  30. 30. Java Scala vs Java API
  31. 31. Scala Scala vs Java API
  32. 32. Scala is nice, nobody doubts But someDmes business prefers to sDck with Java. Akka Java API is too damn good to argue.
  33. 33. - Actor have a specic lifecycle + it's hidden behind the ActorRef Luckily Akka Extens5ons mechanism allows to use external DI framework Spring & Akka - Actor is just a @Prototype bean:
  34. 34. Spring & Akka Async Controllers
  35. 35. DEMO Let's get coding, nally!
  36. 36. Robot Control System
  37. 37. Let's keep it simple for the demo
  38. 38. Cluster Aware Router Distributed pub/sub Distribute across mulDple nodes Distributed data
  39. 39. Scale out the most overloaded service
  40. 40. Thank you! hLps://