Actor Model pattern for concurrency

1,509 views
1,152 views

Published on

Actor model is a pattern to simplify writing efficient concurrent applications

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,509
On SlideShare
0
From Embeds
0
Number of Embeds
160
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Actor Model pattern for concurrency

  1. 1. Actor Model
  2. 2. Google thinks this is an Actor Model
  3. 3. Actor model is a pattern to simplify writing efficient concurrent applications
  4. 4. Concurrency: Writing applications where different parts could run in parallel
  5. 5. Traditional approach to concurrency Multiple threads and Shared Objects
  6. 6. void Session::onConnectionRequest(conn) { anvil.validateToken(conn.token); this.addConnection(conn); }
  7. 7. void Session::onConnectionRequest(conn) { anvil.validateToken(conn.token); this.addConnection(conn); } void Session::onDisconnectionRequest(conn) { this.removeConnection(conn); }
  8. 8. Publisher::onDisconnect() { scoped_lock lock; if (subscriber != NULL) { subscriber.setPublisher(NULL); } } Publisher::setSubscriber() { scoped_lock lock; subscriber = NULL; } Subscriber::onDisconnect() { scoped_lock lock; if (publisher != NULL) { publisher.setSubscriber(NULL); } } Subscriber::setPublisher() { scoped_lock lock; publisher = NULL; }
  9. 9. Traditional concurrency It is difficult to build efficient applications while maintaining consistent state
  10. 10. Fundamentally, the biggest challenge that SendGrid faces in development is concurrent programming. While what we do isn’t rocket science, doing it at a scale of over 500 million messages per day is extremely challenging (I’ve done rocket science, this is way harder).
  11. 11. Solution 1: ad-hoc solutions and fix bugs forever
  12. 12. Solution 2: rendition
  13. 13. Solution 3: Actor Model
  14. 14. Actor Model concurrency approach Asynchronous communication between objects Object state is only modified from one thread at a specific time
  15. 15. Multiple threads and Shared Objects
  16. 16. Actor Model
  17. 17. Example sealed trait SessionMessage case object ConnectMessage extends SessionMessage case object DisconnectMessage extends SessionMessage class Session extends Actor with ActorLogging { def receive = { case ConnectMessage => log.info("Connecting") case DisconnectMessage => log.info("Disconnecting") } } object Demo extends App { val system = ActorSystem("MySystem") val session = system.actorOf(Props[Session], name="session1") session ! ConnectMessage session ! DisconnectMessage }
  18. 18. Bonus Track supervision persistency distribution

×